a.
Argue that
AŒq > AŒp
, so that
BŒp
D
0
and
BŒq
D
1
.
b.
To complete the proof of the 0-1 sorting lemma, prove that algorithm X fails to
sort array
B
correctly.
Now you will use the 0-1 sorting lemma to prove that a particular sorting algo-
rithm works correctly. The algorithm,
columnsort
, works on a rectangular array
of
n
elements. The array has
r
rows and
s
columns (so that
n
D
rs
), subject to
three restrictions:
r
must be even,
s
must be a divisor of
r
, and
r
2s
2
.
When columnsort completes, the array is sorted in
column-major order
: reading
down the columns, from left to right, the elements monotonically increase.
Columnsort operates in eight steps, regardless of the value of
n
. The odd steps
are all the same: sort each column individually. Each even step is a fixed permuta-
tion. Here are the steps:
1. Sort each column.
2. Transpose the array, but reshape it back to
r
rows and
s
columns. In other
words, turn the leftmost column into the top
r=s
rows, in order; turn the next
column into the next
r=s
rows, in order; and so on.
3. Sort each column.
4. Perform the inverse of the permutation performed in step 2.
210
Chapter 8
Sorting in Linear Time
10
14
5
8
7
17
12
1
6
16
9
11
4
15
2
18
3
13
(a)
4
1
2
8
3
5
10
7
6
12
9
11
16
14
13
18
15
17
(b)
4
8
10
12
16
18
1
3
7
9
14
15
2
5
6
11
13
17
(c)
1
3
6
2
5
7
4
8
10
9
13
15
11
14
17
12
16
18
(d)
1
4
11
3
8
14
6
10
17
2
9
12
5
13
16
7
15
18
(e)
1
4
11
2
8
12
3
9
14
5
10
16
6
13
17
7
15
18
(f)
5
10
16
6
13
17
7
15
18
1
4
11
2
8
12
3
9
14
(g)
4
10
16
5
11
17
6
12
18
1
7
13
2
8
14
3
9
15
(h)
1
7
13
2
8
14
3
9
15
4
10
16
5
11
17
6
12
18
(i)
Figure 8.5
The steps of columnsort.
(a)
The input array with 6 rows and 3 columns.
(b)
After
sorting each column in step 1.
(c)
After transposing and reshaping in step 2.
(d)
After sorting each
column in step 3.
(e)
After performing step 4, which inverts the permutation from step 2.
(f)
After
sorting each column in step 5.
(g)
After shifting by half a column in step 6.
(h)
After sorting each
column in step 7.
(i)
After performing step 8, which inverts the permutation from step 6. The array
is now sorted in column-major order.
5. Sort each column.
6. Shift the top half of each column into the bottom half of the same column, and
shift the bottom half of each column into the top half of the next column to the
right. Leave the top half of the leftmost column empty. Shift the bottom half
of the last column into the top half of a new rightmost column, and leave the
bottom half of this new column empty.
7. Sort each column.
8. Perform the inverse of the permutation performed in step 6.
Figure 8.5 shows an example of the steps of columnsort with
r
D
6
and
s
D
3
.
(Even though this example violates the requirement that
r
2s
2
, it happens to
work.)
c.
Argue that we can treat columnsort as an oblivious compare-exchange algo-
rithm, even if we do not know what sorting method the odd steps use.
Although it might seem hard to believe that columnsort actually sorts, you will
use the 0-1 sorting lemma to prove that it does. The 0-1 sorting lemma applies
because we can treat columnsort as an oblivious compare-exchange algorithm. A
Notes for Chapter 8
211
couple of definitions will help you apply the 0-1 sorting lemma. We say that an area
of an array is
Do'stlaringiz bilan baham: |