13. Use the selection sort to sort the numbers in the int array i a. Use calls to System.
currentTimeMillis before and after the sort to determine the elapsed time:
long start = System.currentTimeMillis();
// do sort
long stop = System.currentTimeMillis();
System.currentTimeMillis returns the current time in milliseconds. Thus, to get elapsed
time in seconds, divide the difference in the start and stop times by 1000.0. Then round
the result to the nearest long value by calling Math.round:
long elapsed = Math.round((stop - start)/1000.0);
Determine the size of ia for which the elapsed time of the sort is about 100 seconds. Now
perform the same sort using the static sort method in the Arrays class in the java.util
package. Call this method with
Arrays.sort(ia);
What is the elapsed time for this sort? Is this sort significantly faster than the selection
sort?
13b) Implement and test a bubble sort. To sort n numbers, make n - 1 passes. On each pass,
compare every pair of adjacent numbers. If any pair is not in ascending order, switch the pair. On
the first pass, start from the first element (compare the first with the second, the second with the
third, and so on). On the second pass, again start from the first element but stop your compares
one slot from the end of the array. In each successive pass, stop one slot before the stopping
point of the previous pass. Use your bubble sort to sort 100, 23, 15, 23, 7, 23, 2, 311, 5, 8, and 3.
Display the sorted numbers.