Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Sorting. Agenda Swapping two values in array of int Bubble sort O(n 2 ) Swapping/sorting an array of Object Swapping/sorting a Vector of Object.

Similar presentations


Presentation on theme: "Chapter 6 Sorting. Agenda Swapping two values in array of int Bubble sort O(n 2 ) Swapping/sorting an array of Object Swapping/sorting a Vector of Object."— Presentation transcript:

1 Chapter 6 Sorting

2 Agenda Swapping two values in array of int Bubble sort O(n 2 ) Swapping/sorting an array of Object Swapping/sorting a Vector of Object Using Comparators Selection Sort O(n 2 ) Insertion SortO(n 2 ) QuicksortO(n log n) very efficient space Merge SortO(n log n) good for files Radix SortO(n) but not inefficient

3 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; }

4 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++; }

5

6 Swapping Two String objects in 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; }

7 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++; }

8 compareTo method required by Comparable interfaceComparable Standard method to compare two objects – Not in class Object however – each class must provide it – Returns a number zero Indicates obj1 obj2 StringString 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 );

9 Swapping Two (Generic) Objects in Array public static 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; }

10 Bubble Sort, Array of Generic Objects public static 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++; }

11 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

12 ComparatorComparator Interface Requires compare method:

13 A comparator for Caseless String sort

14 WordFreq comparators class CompareByString implements Comparator > { public int compare(Association a, Association b){ String left = a.getKey(); String right = b.getKey(); return left.compareToIgnoreCase(right); } class CompareByInteger implements Comparator > { public int compare(Association a, Association b){ Integer left = a.getValue(); Integer right = b.getValue(); return -left.compareTo(right); }

15 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

16 Selection Sort– find biggest  swap

17

18 Selection Sort Pro/Con Pros: O(n) swaps Cons: O(n 2 ) compares  O(n 2 ) overall Performance independent of ordering of data

19 Insertion Sort – put item in correct pos

20

21 Insertion Sort Pro/Con Cons: O(n 2 ) compares and data movement  O(n 2 ) overall Pros: ordered or nearly-ordered data  O(n) – Insertion sort is useful at tail end of Quicksort

22 Merge Sort core idea: merge

23

24 Full mergeSort algorithm (recursive)

25 Trace of mergeSortRecursive

26 Non-recursive mergeSort wrapper

27 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.

28 Quicksort core idea: partition

29

30 Full quickSort method (recursive)

31

32 Quicksort Pro/Con Pro: very fast, O(n log n) for random order Con: terrible on nearly sorted, or reverse sorted data  O(n 2 ) – Sometimes insertion sort used when size <= 20

33 Radix Sort See text…!


Download ppt "Chapter 6 Sorting. Agenda Swapping two values in array of int Bubble sort O(n 2 ) Swapping/sorting an array of Object Swapping/sorting a Vector of Object."

Similar presentations


Ads by Google