Upload
marcus-stafford
View
221
Download
4
Embed Size (px)
Citation preview
Chapter 6Sorting
Agenda
• Swapping two values in array of int• Bubble sort O(n2)• Swapping/sorting an array of Object• Swapping/sorting a Vector of Object• Using Comparators • Selection Sort O(n2)• Insertion Sort O(n2) • Quicksort O(n log n) very efficient space• Merge Sort O(n log n) good for files• Radix Sort O(n) but not inefficient
Swapping Two Values—Array of Int
public static void swap(int [] data, int i, int j) { int temp; temp = data[i]; data[i] = data[j]; data[j] = temp; }
Bubble Sort, array of int public static void bubbleSort (int [] data, int n) // pre: 0 <= n <= data.length // post: values in data[0..n-1] in ascending order { int numSorted = 0; // number of values in order int index; // general index while (numSorted < n) { // bubble a large element to higher array index for (index = 1; index < n-numSorted; index++) {
if (data[index-1] > data[index]) swap(data,index-1,index); } // at least one more value in place numSorted++; } }
Swapping Two String objectsin Array
public static void swap(String [] data, int i, int j) // pre: 0 <= i,j < data.length // post: data[i] and data[j] are exchanged { String temp; temp = i; i = j; j = temp; }
Bubble Sort, Array of String public static void bubbleSort ( String [] data, int n) // pre: 0 <= n <= data.length // post: values in data[0..n-1] in ascending order { int numSorted = 0; // number of values in order int index; // general index while (numSorted < n) { // bubble a large element to higher array index for (index = 1; index < n-numSorted; index++) { if (data[index-1].compareTo( data[index])>0) swap(data,index-1,index); } // at least one more value in place numSorted++; } }
compareTo methodrequired by Comparable interface
• Standard method to compare two objects– Not in class Object however– each class must provide it– Returns a number <, == or > zero • Indicates obj1 < obj2, == obj2 or > obj2
String s1=input.next(), s2 = input.next();if (s1.compareTo(s2)<0)
System.out.println( s1 + “is less than ” + s2 );
if (s1.compareTo(s2)==0)System.out.println( s1 + “is same as ” + s2 );
if (s1.compareTo(s2)>0)System.out.println( s1 + “is greater than” + s2 );
Swapping Two (Generic) Objects in Array
public static <T> void swap(T[] data, int i, int j) // pre: 0 <= i,j < data.length // post: data[i] and data[j] are exchanged { T temp; temp = data[i]; data[i] = data[j]; data[j] = temp; }
Bubble Sort, Array of Generic Objects
public static <T extends Comparable> void bubbleSort (T[] data, int n) // pre: 0 <= n <= data.length // post: values in data[0..n-1] in ascending order { int numSorted = 0; // number of values in order int index; // general index while (numSorted < n) { // bubble a large element to higher array index for (index = 1; index < n-numSorted; index++) { if (data[index-1].compareTo( data[index])>0) swap(data,index-1,index); } // at least one more value in place numSorted++; } }
Comparison Summary
• int, char, double, float: use <,>,==• generic objects: use .compareTo() – If provided– Locked in to the way compareTo is written
• What if– Class doesn’t have compareTo ?– Don’t like order compareTo uses?• Use Comparator objects
Comparator Interface
• Requires compare method:
A comparator for Caseless String sort
WordFreq comparatorsclass CompareByString implements Comparator<Association<String, Integer>>{ public int compare(Association<String, Integer> a, Association<String,Integer> b){ String left = a.getKey(); String right = b.getKey(); return left.compareToIgnoreCase(right); }}
class CompareByInteger implements Comparator<Association<String, Integer>>{ public int compare(Association<String, Integer> a, Association<String,Integer> b){ Integer left = a.getValue(); Integer right = b.getValue(); return -left.compareTo(right); }}
Other Sort Algorithms• Two costly operations in sorting:– Comparing two objects– Swapping two objects– Different algorithms trade off these operations in
attempts to optimize for certain conditions
Selection Sort– find biggest swap
Selection Sort Pro/Con
• Pros: O(n) swaps• Cons: O(n2) compares O(n2) overall• Performance independent of ordering of data
Insertion Sort – put item in correct pos
Insertion Sort Pro/Con
• Cons: O(n2) compares and data movement O(n2) overall
• Pros: ordered or nearly-ordered data O(n)– Insertion sort is useful at tail end of Quicksort
Merge Sort core idea: merge
Full mergeSort algorithm (recursive)
Trace of mergeSortRecursive
Non-recursive mergeSort wrapper
Mergesort Pro/Con• Pro: useful when data are in large files too big
to load into memory– Files are split into manageable chunks, sorted,
then merged• Cons: extra overhead in memory needed for
temp array.
Quicksort core idea: partition
Full quickSort method (recursive)
Quicksort Pro/Con
• Pro: very fast, O(n log n) for random order• Con: terrible on nearly sorted, or reverse
sorted data O(n2)– Sometimes insertion sort used when size <= 20
Radix Sort
• See text…!