1 / 36

Sorting

Sorting. Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test scores in ascending numeric order Sorting a list of people alphabetically by last name

dacian
Télécharger la présentation

Sorting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test scores in ascending numeric order Sorting a list of people alphabetically by last name There are many algorithms, which vary in efficiency, for sorting a list of items 1

  2. Polymorphism in Sorting (Another Way to Do It) A class that implements the Comparable interface defines a compareTo method that returns the relative order of its objects We can use polymorphism to develop a generic sort for any set of Comparable objects The sorting method accepts as a parameter an array of Comparable objects That way, one method can be used to sort a group of People, or Books, or whatever as long as the class implements Comparable 2

  3. Comparable<T> Interface • Comparable<T> Interface requires one method: • int compareTo (T that) // compares self (“this”) to “that” • Returns an int value that is: • Negative when “this” object is less than “that” object • Zero when “this” object is equal to “that” object • Positive when “this” object is greater than “that” object • Most useful when there is only one logical or “inherent” ordering for the generic <T> objects 3

  4. Sorting with Comparable • Unlike sorting with Comparator, there is no need for a separate class containing the logic for comparison of two objects • The compareTo method in the class of the objects being sorted does the comparison • Example of usage: • “this” String = “Hello” and “that” String = “World” int order = “Hello”.compareTo(“World”); // value = -15

  5. Making County Objects Comparable • We could have used County objects that implemented Comparable in Project 2 • We would need to modify the County class • Add the implements Comparable clause • Add the compareTo() method code • This would limit us to one order of sorting • Arbitrarily, we could choose population as the inherent order for sorting

  6. Making County objects Comparable public class County implements Comparable<T> { private String name; private String state; private int population; . . . public int compareTo(County that) { return that.population - this.population; } }

  7. Sorting with Comparable The sorting method doesn't "care" what type of object it is sorting, it just needs to be able to call the compareTo method of that object That is guaranteed by using Comparable as the parameter type passed to the sorting method Each Comparable class has a compareTo method that determines what it means for one object of that class to be “less than another” 7

  8. Sorting with Comparable • Class header sort package with Comparable public class SortingAndSearching<T extends Comparable> Note: use of extends instead of implements • The class header for the objects to be sorted will be: public class MyClass implements Comparable • Comparable is an Interface so MyClass must implement Comparable - not extend it • An bit of an inconsistency in Java generics, but it reinforces the idea that implementing an interface has the same semantics as extending a superclass

  9. Sorting • Sorting is the process of arranging a list of items in a particular order • The sorting process is based on specific value(s) • Sorting a list of test scores in ascending numeric order • Sorting a list of people alphabetically by last name • Sequential sorts O(n2): Selection, Insertion, Bubble • Logarithmic sorts O(nlogn): Quick, Merge • Now we'll implement the sorts with polymorphic Comparable objects

  10. Selection Sort • The approach of Selection Sort: • Select a value and put it in its final place in the list • Repeat for all other values • In more detail: • Find the smallest value in the list • Switch it with the value in the first position • Find the next smallest value in the list • Switch it with the value in the second position • Repeat until all values are in their proper places

  11. Selection Sort • An example: original: 3 9 6 1 2 smallest is 1: 1 9 6 3 2 smallest is 2: 1 2 6 3 9 smallest is 3: 1 2 3 6 9 smallest is 6: 1 2 3 6 9 • Each time, the smallest remaining value is found and exchanged with the element in the "next" position to be filled

  12. Selection Sort (Comparable) public void selectionSort (T [] data) { int min; T temp; for (int index = 0; index < data.length-1; index++) { min = index; for (int scan = index+1; scan < data.length; scan++) { if (data[scan].compareTo(data[min])<0) min = scan; } swap( data, index, min ); } }

  13. Insertion Sort • The approach of Insertion Sort: • Pick any item and insert it into its proper place in a sorted sublist • Repeat until all items have been inserted • In more detail: • Consider the first item to be a sorted sublist (of one item) • Insert the second item into the sorted sublist, shifting the first item as needed to make room to insert the new addition • Insert the third item into the sorted sublist (of two items), shifting items as necessary • Repeat until all values are inserted into their proper positions

  14. Insertion Sort • An example: insert 9: 3 9 6 1 2 insert 6: 3 9 6 1 2 insert 3: 3 6 9 1 2 insert 2: 1 3 6 9 2 finished: 1 2 3 6 9

  15. Insertion Sort (Comparable) public void insertionSort (T[] data) { for (int index = 1; index < data.length; index++) { T key = data[index]; int position = index; /** Shift larger values to the right */ while (position > 0 && data[position-1].compareTo(key) > 0) { data[position] = data[position-1]; position--; } data[position] = key; } }

  16. Bubble Sort • Bubble sort algorithm sorts the values by repeatedly comparing neighboring elements • It swaps their position if they are not in order • Each pass through the algorithm moves the largest value to its final position • A pass may also reposition other elements • May be more efficient if looping is stopped when no changes were made on last pass

  17. Bubble Sort • An example of one pass: Original: 9 6 8 12 3 1 7 Swap 9 & 6: 6 9 8 12 3 1 7 Swap 9 & 8: 6 8 9 12 3 1 7 No swap: 6 8 9 12 3 1 7 Swap 12 & 3: 6 8 9 3 12 1 7 Swap 12 & 1: 6 8 9 3 1 12 7 Swap 12 & 7: 6 8 9 3 1 7 12 • Each pass moves largest to last position • Each pass can iterate one less time than last

  18. Bubble Sort (Comparable) public void bubbleSort (T[] data) { int position, scan; T temp; for (position = data.length - 1; position >= 0; position--) for (scan = 0; scan <= position - 1; scan++) if (data[scan].compareTo(data[scan+1]) > 0) swap( data, scan, scan + 1 ); }

  19. Comparing Sorts The Selection Sort, Insertion Sort and Bubble Sort algorithms are similar in efficiency They both have outer loops that scan all elements, and inner loops that compare the value of the outer loop with almost all values in the list Approximately n2 number of comparisons are made to sort a list of size n We therefore say that these sorts are of O(n2) Other sorts are more efficient: O(n log2 n) 19

  20. Quick Sort • The quick sort algorithm is a “divide and conquer” algorithm • It compares the data values to a partition element while partitioning into two sub-lists • Then, it recursively sorts the sub-lists on each side of the partition element • The recursion base case is a list containing only 1 element (which is inherently sorted) • A simplistic choice of the partition element is the first element, but that may not be best

  21. Quick Sort Initial Data (Select first element as the partition element) 90 65 7 305 120 110 8 Move all data below partition element value to the left Move all data above partition element value to the right 90 65 7 8 120 110 305 Split Point (Only a coincidence that it is in middle) Swap first element with element at the split point Partition element is now in its correct position 8 65 7 90 120 110 305 Next Pass Next Pass

  22. Quick Sort • If the list is already (or nearly) sorted, the first element is a poor choice as partition element • The two partitioned sub-lists are lopsided • One may contain only one element • The other may contain all the other elements • In this case, the quick sort is not so quick • A better choice might be the middle element of the list, but note that there is still an initial order for the elements that is “pathological”

  23. Merge Sort • Merge sort is another “divide and conquer” algorithm with a different division strategy • Cut the array of data in half • Sort the left half (recursively calling itself) • Sort the right half (recursively calling itself) • Merge the two sorted halves of the array • The merge process for two arrays that are already sorted is only O(n) and we perform O(log n) merges

  24. Merge Sort • All comparisons are in the merge process • Copy all elements into a workspace array • Copy them back into the original array • If there are elements remaining in both halves, compare first elements of each and copy one • If there are no elements remaining in left half, copy remainder of right half • If there are no elements remaining in right half, copy remainder of left half

  25. Seven Growth Functions • Seven functions g(n) that occur frequently in the analysis of algorithms (in order of increasing rate of growth relative to n): • Constant  1 • Logarithmic  log n • Linear  n • Log Linear  n log n • Quadratic  n2 • Cubic  n3 • Exponential  2n

  26. Growth Rates Compared

  27. Introduction to Project 3 • In Project 3, you will experiment with and observe the performance of various sorting algorithms applied to data in different states of organization • Some of the data will be organized randomly, some will be already sorted, and some will be sorted exactly in the opposite of the desired order • You will learn that different sort algorithms have different performance in these different situations

  28. Introduction to Project 3 • You will be provided the code for the class “SortingAndSearching” which contains multiple sorting methods each of which does a different sort algorithm on Comparable class objects • You will also be provided a main class that reads a file, sets up data from the file in an array for sorting, invokes the various sort methods, and prints a count of the number of compareTo calls • You will be provided with some test data files that present different situations for sorting

  29. Introduction to Project 3 • You will add code that counts the number of times compareTo is invoked • We will call this the “instrumentation” code • There are 3 possible places to put this code • In compareTo method of the Comparable class • In code of SortingAndSearching method(s) • In a “wrapper” class with a compareTo method that counts calls and calls compareTo of the Comparable class object that it “wraps”

  30. Introduction to Project 3 • If you have access to the source code for the Comparable class, you can count each time compareTo gets invoked by the sort • You need to add methods to reset and read the counter between different sorts • You can’t do this if you don’t have access to the source code OR • It’s difficult if you need to do this for many different classes that implement Comparable

  31. Introduction to Project 3 • To put instrumentation in the class that implements Comparable SortingClass is unaware of the methods that control the counting of CompareTo calls MainClass SortingClass Sort data ClassName Comparable Access resetCount() and getCount() methods before and after each sort Add instrumentation code here

  32. Introduction to Project 3 • If you have access to the source code for the Sorting class, you can count each time the sort code invokes a compareTo method • You need to add methods to reset and read the counter between different sorts • You can’t do this if you don’t have access to the source code, e.g. a sorting class in the standard class library

  33. Introduction to Project 3 • To put instrumentation in the Sorting class Sort data MainClass SortingClass Access resetCount() and getCount() methods before and after each sort Add instrumentation code here ClassName Comparable ClassName is unaware of the methods that control the counting of CompareTo calls 33

  34. Introduction to Project 3 • If you don’t have access to the source code for the class that implements Comparable or the library sorting class, you “wrap” the Comparable class in another class that controls access to the compareTo method • The “wrapper” class has methods to reset or read the counter between sorts • The main method wraps each Comparable class object in the arrays passed to sorting

  35. Introduction to Project 3 • To put instrumentation in a wrapper class SortingClass is unaware of the methods that control the counting of CompareTo calls MainClass SortingClass Sort data “Wrapper” Class Comparable Access resetCount() and getCount() methods before and after each sort Add instrumentation code here ClassName Comparable ClassName is unaware of the methods that control the counting of CompareTo calls

  36. Introduction to Project 3 • Because I want you to learn something about each sorting algorithm, I am asking you to do this project in the second way • You will “instrument” the SortingAndSearching code so that you can learn how and where the comparisons are made in sorting algorithm • Compare your results to the Big-O notation behavior that you expected for each algorithm on each data file and explain your observations

More Related