Java Reference
In-Depth Information
them back to the array. When the algorithm is finished, clicking the Step button
displays a message to inform the user. Clicking the Reset button creates a new
random array for a new start.
F IGURE 23.21
The program animates radix sort.
*23.18
( Merge animation ) Write a program that animates the merge of two sorted lists.
Create two arrays, list1 and list2 , each of which consists of 8 random num-
bers from 1 to 999. The array elements are displayed, as shown in Figure 23.22a.
Clicking the Step button causes the program to move an element from list1 or
list2 to temp . Clicking the Reset button creates two new random arrays for
a new start. When the algorithm is finished, clicking the Step button displays a
message to inform the user.
(a)
(b)
F IGURE 23.22
The program animates a merge of two sorted lists. (b) The program animates a partition for quick sort.
*23.19
( Quick sort partition animation ) Write a program that animates the partition for
a quick sort. The program creates a list that consists of 20 random numbers from
1 to 999. The list is displayed, as shown in Figure 23.22b. Clicking the Step but-
ton causes the program to move low to the right or high to the left, or swap the
elements at low and high . Clicking the Reset button creates a new list of random
numbers for a new start. When the algorithm is finished, clicking the Step button
displays a message to inform the user.
*23.20
( Modify merge sort ) Rewrite the mergeSort method to recursively sort the first
half of the array and the second half of the array without creating new temporary
arrays, and then merge the two into a temporary array and copy its contents to the
original array, as shown in Figure 23.6b.
 
 
Search WWH ::




Custom Search