|
The standard sequential representation of binary trees is discussed. Then heapsort is discussed conceptually and specified in terms of pseudocode. A programming assignment is issued to write a Java program based on the pseudocode.
|
pseudocode heapsort(a[],n)
// create initial heap
for i = floor(n/2) to 1 by -1 do
adjust(i,n)
end
// sort
for i = n-1 to 1 by -1 do
t = a[i+1]
a[i+1] = a[1]
a[1] = t
adjust(1,i)
end
end
pseudocode adjust(i,n)
a = a[i]
j = 2*i
while ( j <= n ) do
if ( j < n ) then
if ( a[j] < a[j+1] ) then
j = j + 1
end
end
if ( a > a[j] ) then
exit the loop
end
a[j/2] = a[j]
j = j*2
end
a[j/2] = a
end
Write an application program which generates 30 two digit integers, stores them in an array, and sorts the array by means of the heapsort algorithm. Arrange to display the elements of the array on one line (separating them by a space):
Proceed in the following way, working within a new Sorting project: