Initially
12 10 3 37 57 2 23 9
Pass 1 Pass 3
10 12 3 37 57 2 23 9 3 10 2 12 23 9 37 57
10 3 12 37 57 2 23 9 3 10 2 12 9 23 37 57
10 3 12 37 2 57 23 9
10 3 12 37 2 23 57 9 Pass 4
10 3 12 37 2 23 9 57 3 2 10 12 9 23 37 57
3 2 10 9 12 23 37 57
Pass 2
3 10 12 37 2 23 9 57 Pass 5
3 10 12 2 37 23 9 57 2 3 10 9 12 23 37 57
3 10 12 2 23 37 9 57 2 3 9 10 12 23 37 57
3 10 12 2 23 9 37 57
Initially Pass 5, (next = 2) 12 10 3 37 57 2 23 9 3 10 12 37 57 57 23 9 3 10 12 37 37 57 23 9 Pass 1, (next = 10) 3 10 12 12 37 57 23 9 12 12 3 37 57 2 23 9 3 10 10 12 37 57 23 9 10 12 3 37 57 2 23 9 3 3 10 12 37 57 23 9 2 3 10 12 37 57 23 9 Pass 2, (next = 3) Pass 6, (next = 23) 10 12 12 37 57 2 23 9 2 3 10 12 37 57 57 9 10 10 12 37 57 2 23 9 2 3 10 12 37 37 57 9 3 10 12 37 57 2 23 9 2 3 10 12 23 37 57 9 Pass 7, (next = 9) Pass 3, (next = 37) 2 3 10 12 23 37 57 57 3 10 12 37 57 2 23 9 2 3 10 12 23 37 37 57 2 3 10 12 23 23 37 57 2 3 10 12 12 23 37 57 Pass 4, (next = 57) 2 3 10 10 12 23 37 57 3 10 12 37 57 2 23 9 2 3 9 10 12 23 37 57
A = 12 10 3 37 57 2 23 9
PARTITION(A, 1, 8) pivot = 12
12 10 3 37 57 2 23 9
i j
12 10 3 37 57 2 23 9 9 10 3 12 57 2 23 37
i> < j i> < j
9 10 3 37 57 2 23 12 9 10 3 2 57 12 23 37
i j i j
9 10 3 37 57 2 23 12 9 10 3 2 57 12 23 37
i> < j i X j
9 10 3 12 57 2 23 37 9 10 3 2 12 57 23 37
i j ij.
Initially Pass 1 Pass 2
12 10 2
10 12 3
3 2 9
37 3 10
57 23 12
2 37 23
23 57 37
9 9 57
A = 12 10 3 37 57 2 23 9
merge merge merge merge
A = 10 12 3 37 2 57 9 23
merge merge
A = 3 10 12 37 2 9 23 57
merge
A = 2 3 9 10 12 23 37 57
So, after the recursive calls MERGE-SORT(A, 1, 4) and
MERGE-SORT(A, 5, 8), but before MERGE( A , 1, 4, 8), we have
A = 3 10 12 37 2 9 23 57
public class maxSortTest
{
// Sorts an array of Comparable objects in increasing
// order.
public static void maxSort(Comparable[] a)
{
Comparable temp;
int max;
for( int i = a.length-1; i >= 0; i--)
{
max = maxIndex(a, i);
temp = a[i];
a[i] = a[max];
a[max] = temp;
}
}
// Finds the index of the largest element in a[1..j]
static int maxIndex(Comparable[] a, int j)
{
int index = 0;
for( int i = 1; i <= j ; i++)
if ( a[i].compareTo(a[index]) > 0)
{
index = i;
}
return index;
}
public static void main(String[] args)
{
String[] test = { "the" , "quick", "brown" , "fox", "jumped",
"over" , "the", "lazy", "dog"};
maxSort(test);
for(int i = 0; i < test.length ; i++)
{
System.out.println(test[i]);
}
}
}
In the above test case, the program will print out
brown dog fox jumped lazy over quick the the
T(n) = T(n-1) + lg nso by repeated substitution
T(n) = lg n + lg(n-1) + ... + lg(2) + lg(1)
= Theta( n lg(n) )
The heap from two subheaps method (bottom up) has recurrence
T(n) = 2 T(n/2) + lg n
= Theta(n),
by the master theorem.
a) We can represent a d-ary heap as an array for any d, breadth first as we have done when d = 2. Let us assume we have an array A[1..n] representing an n-node d-ary heap. Then
b) A d-ary heap of height h has at most (1+d+d^2+...+d^h) elements, which is between d^h and 2*d^h for d > 1. This means a heap of size n has a height of Theta(log_d(n)).
(3 / lg 3) ---------- = 0.9464... (2 / lg 2)