Java Reference
In-Depth Information
Display 12.7 Using the MergeSort Class
1 public class MergeSortDemo
2{
3
public static void main(String[] args)
4
{
5
double [] b = {7.7, 5.5, 11, 3, 16, 4.4, 20, 14, 13, 42};
6
System.out.println("Array contents before sorting:");
7
int i;
8
for (i = 0; i < b.length; i++)
9
System.out.print(b[i] + " ");
10
System.out.println();
11
MergeSort.sort(b, 0, b.length
1);
12
System.out.println("Sorted array values:");
13
for (i = 0; i < b.length; i++)
14
System.out.print(b[i] + " ");
15
System.out.println();
16
}
17
}
Sample Dialogue
Array contents before sorting:
7.7 5.5 11.0 3.0 16.0 4.4 20.0 14.0 13.0 42.0
Sorted array values:
3.0 4.4 5.5 7.7 11.0 13.0 14.0 16.0 20.0 42.0
Display 12.8 Quick Sort Realization of Sorting Pattern (part 1 of 3)
1 /**
2 Class that realizes the divide-and-conquer sorting pattern and
3 uses the quick sort algorithm.
4 */
5 public class QuickSort
6{
7
/**
8
Precondition: Interval a[begin] through a[end] of a have elements.
9
Postcondition: The values in the interval have
10
been rearranged so that a[begin] <= a[begin+1] <= ... <= a[end].
11
*/
(continued)
 
Search WWH ::




Custom Search