Download presentation
Presentation is loading. Please wait.
Published byBraedon Pullam Modified over 10 years ago
1
Stupid Columnsort Tricks Geeta Chaudhry Tom Cormen Dartmouth College Department of Computer Science
2
Columnsort Sorts N numbers Organized as r s mesh Divisibility restriction: s must divide r Height restriction: r ≥ 2s 2 8 steps 1.Sort each column 2.Transpose 3.Sort each column 4.Untranspose 5.Sort each column 6.Shift down 1/2 column 7.Sort each column 8.Shift up 1/2 column
3
Proof of Correctness Columnsort is oblivious Use 0-1 Principle: If an oblivious algorithm sorts all input sets consisting solely of 0s and 1s, then it sorts all input sets with arbitrary values. After step 3, the mesh consists of Clean rows of 0s at the top Clean rows of 1s at the bottom ≤ s dirty rows between the clean rows
4
Proof of Correctness (continued) After step 4, the mesh consists of Clean columns of 0s on the left Clean columns of 1s on the right A dirty area of size ≤ s 2 between the clean columns r ≥ 2s 2 ==> s 2 ≤ r/2 ==> the dirty area is at most 1/2 a column large
5
Proof of Correctness (continued) If, entering step 5, the dirty area is at most 1/2 a column large, then steps 5–8 complete the sorting If the dirty area fits in a single column, step 5 cleans it, and steps 6–8 leave the mesh clean If the dirty area spans two columns, then it’s in the bottom half of one column and the top half of the next column. Step 5 does not change this Step 6 gets the dirty area into one column Step 7 cleans it Step 8 moves all values back to where they belong
6
Removing the Divisibility Restriction Step 1: Sort each column Each column has ≤ 1 0 1 transition ≤ s 0 1 transitions There may be a 1 0 transition going from one column to the next ≤ s–1 1 0 transitions Step 2: Transpose Within rows, ≤ s 0 1 transitions, ≤ s–1 1 0 transitions
7
Divisibility Restriction (continued) After step 2, let X = dirty rows with one 0 1 transition, no 1 0 Y = dirty rows with one 1 0 transition, no 0 1 Z = all other dirty rows (≥ 1 0 1 and ≥ 1 1 0) Number of dirty rows = |X| + |Y| + |Z| All other rows are clean Claim: max(|X|, |Y|) + |Z| ≤ s Every row of X, Z contains ≥ 1 0 1 => |X| + |Z| ≤ s Every row of Y, Z contains ≥ 1 1 0 => |Y| + |Z| ≤ s–1 max(|X|, |Y|) = |X| => max(|X|, |Y|) + |Z| ≤ s max(|X|, |Y|) = |Y| => max(|X|, |Y|) + |Z| ≤ s–1 In either case, max(|X|, |Y|) + |Z| ≤ s
8
Divisibility Restriction (continued) After step 3 Clean rows of 0s move to the top Clean rows of 1s move to the bottom Pair up the min(|X|, |Y|) pairs of rows with one row in X and other row in Y more 0s than 1s 000000001111 111110000000 000000000000 111110001111 more 1s than 0s 000111111111 111111110000 000111110000 111111111111 equal 0s and 1s 000011111111 111100000000 000000000000 111111111111 from X: from Y:
9
Divisibility Restriction (continued) In all cases, ≥ 1 clean row is formed ≥ min(|X|, |Y|) new clean rows are created Dirty rows remaining ≤ |X| + |Y| + |Z| – min(|X|, |Y|) |X| + |Y| – min(|X|, |Y|) = max(|X|, |Y|) ==> Dirty rows remaining ≤ max(|X|, |Y|) + |Z| ≤ s From here, it’s the same as the original proof Dirty area size ≤ s 2 ≤ r/2 (half a column) after step 4 Steps 5–8 clean up the dirty area
10
Subblock Distribution Divide up the mesh into s 1/2 s 1/2 subblocks Each subblock contains s values Add two steps between steps 3 and 4 Step 3.1: Perform any fixed permutation that moves all values in each subblock into all s columns Step 3.2: Sort each column The resulting algorithm is subblock columnsort Works with relaxed height restriction of r ≥ 4s 3/2 (assuming the divisibility restriction)
11
Subblock Columnsort Correctness After step 3, the line dividing 0s and 1s goes left to right and bottom to top (southwest to northeast) Never turns back to the left Never turns back toward the bottom To show, suffices to show that after step 2, # of 0s in each column ≥ # of 0s in column to its right How could a column have # of 0s < # of 0s in column to its right? There would have to be a 1 0 transition in a row But divisibility restriction => there are no 1 0 transitions in rows after step 2
12
Subblock Columnsort Correctness (continued) After step 3.1, the number of 0s in any two columns differs by ≤ 2s 1/2 The dirty area is confined to an area s rows high and s columns wide In subblocks, s 1/2 s 1/2 Dividing line passes through ≤ s 1/2 + 1 subblocks vertically and ≤ s 1/2 subblocks horizontally ==> ≤ 2s 1/2 subblocks total Don’t double-count the 1 extra subblock Step 3.1 distributes each subblock to all s columns All clean subblocks distribute their 0s and 1s uniformly to each column Number of 0s between any two columns differs by ≤ number of dirty subblocks = 2s 1/2
13
Subblock Columnsort Correctness (continued) After step 4, the mesh consists of Clean columns of 0s on the left Clean columns of 1s on the right A dirty area of size ≤ 2s 3/2 between the clean columns After sorting in step 3.2, the dirty area is ≤ 2s 1/2 rows high and ≤ s columns wide Size of dirty area is ≤ 2s 3/2
14
Subblock Columnsort Correctness (continued) Finish up by observing that r ≥ 4s 3/2 is equivalent to 2s 3/2 ≤ r/2 Now the dirty area is at most 1/2 a column Steps 5–8 clean up the dirty area Can remove the divisibility restriction at the cost of tightening the height restriction to r ≥ 6s 3/2
15
Slabpose Columnsort Another variation on columnsort Loosens height restriction to r ≥ 4s 3/2 Has 10 steps Partitions the mesh into vertical slabs
16
Conclusion Removed and relaxed restrictions on columnsort Divisibility restriction: s divides r Height restriction: r ≥ 2s 2 Divisibility restriction is not necessary Subblock columnsort With divisibility restriction: r ≥ 4s 3/2 Without divisibility restriction: r ≥ 6s 3/2 Slabpose columnsort: r ≥ 4s 3/2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.