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

33
Chapter 6 Sorting

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

Page 1: 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.

Chapter 6Sorting

Page 2: 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.

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

Page 3: 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.

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

Page 4: 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.

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

Page 5: 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.
Page 6: 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.

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

Page 7: 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.

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

Page 8: 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.

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 );

Page 9: 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.

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

Page 10: 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.

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

Page 11: 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.

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

Page 12: 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.

Comparator Interface

• Requires compare method:

Page 13: 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.

A comparator for Caseless String sort

Page 14: 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.

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

Page 15: 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.

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

Page 16: 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.

Selection Sort– find biggest swap

Page 17: 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.
Page 18: 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.

Selection Sort Pro/Con

• Pros: O(n) swaps• Cons: O(n2) compares O(n2) overall• Performance independent of ordering of data

Page 19: 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.

Insertion Sort – put item in correct pos

Page 20: 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.
Page 21: 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.

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

Page 22: 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.

Merge Sort core idea: merge

Page 23: 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.
Page 24: 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.

Full mergeSort algorithm (recursive)

Page 25: 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.

Trace of mergeSortRecursive

Page 26: 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.

Non-recursive mergeSort wrapper

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

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.

Page 28: 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.

Quicksort core idea: partition

Page 29: 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.
Page 30: 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.

Full quickSort method (recursive)

Page 31: 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.
Page 32: 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.

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

Page 33: 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.

Radix Sort

• See text…!