In: Computer Science
JAVA Programming.
How Long is this Gonna Take?
Undergraduate students are surprised to learn that as much intellectual energy has been invested in sorting and searching as almost any other part of Computer Science. Think of Duke Energy's customer database—it’s huge. New customers have to be added, former ones deleted, bills must be sent out, customers send in their payments and inquire about their accounts. An efficient data organization is required for Duke to function at all. The first attack on organizing data involves sorting data elements into some order, and exploiting that order when trying to retrieve a particular element.
Hundreds of sorting algorithms have been developed, and like all sorting algorithms, Selection Sort accomplishes its task by making comparisons and data movements. We often compare algorithms by counting the number of comparisons and movements required—the fewer the better. This begs the question, how many comparisons and movements does the Selection Sort make? And, are these actions affected by the initial arrangement of data values in the array? This is the focus of this lab.
Objectives
By the end of this lab students should be able to
Collecting Sorting Data
Start with the SelectionSort class in the zip file attached to this item. Keep the name SelectionSort, and add a main method to it.
In your submission write some text describing the relationship between the number of comparisons of the various values of NUM_ELEMENTS. For example, what do we find if we divide the number of comparisons for 2000 elements by the number of comparisons for 1000 elements? What do we find if we divide the number of comparisons for 4000 elements by the number of comparisons for 2000 elements?
Epilog: As you can tell, Selection sort doesn’t scale very well. The number comparisons increase quadradically as a function of number of elements. There comes a point that, because of array size, it’s impractical to use Selection sort. The good news is there are hundreds of sorting algorithms. Some suffer from the same performance shortcomings as Selection sort, but others that are almost “magical” in that increasing the number of elements has minor impact on performance. If you’re interested, take a look at chapter 23 Sorting.
Reporting Sorting Data
Submit, in addition to your program, submit the following information in some understandable form (it doesn’t have to be this exact table, but your submission should contain this information).
1000 elements |
2000 elements |
4000 elements |
|
Comparison count lo2Hi |
|||
Comparison count hi2Lo |
|||
Comparison count random |
|||
Swap count lo2Hi |
|||
Swap count hi2Lo |
|||
Swap count random |
Increasing the number of elements from 1000 to 2000 increases the number of comparisons by a factor of |
Increase factor |
Increasing the number of elements from 2000 to 4000 increases the number of comparisons by a factor of |
Increase factor |
Grading Elements
SelectionSort.java public class SelectionSort { /** The method for sorting the numbers */ public static void selectionSort(double[] list) { for (int i = 0; i < list.length - 1; i++) { // Find the minimum in the list[i..list.length-1] double currentMin = list[i]; int currentMinIndex = i; for (int j = i + 1; j < list.length; j++) { if (currentMin > list[j]) { currentMin = list[j]; currentMinIndex = j; } } // Swap list[i] with list[currentMinIndex] if necessary; if (currentMinIndex != i) { list[currentMinIndex] = list[i]; list[i] = currentMin; } } } }