Sorting: Based On Chapter 10 of Koffmann and Wolfgang
Sorting: Based On Chapter 10 of Koffmann and Wolfgang
Chapter Outline
How to use standard sorting methods in the Java API How to implement these sorting algorithms: Selection sort Bubble sort Insertion sort Shell sort Merge sort Heapsort Quicksort
public static <T> void sort (T[] a, Comparator<? super T> comp) // uses given Comparator
These also have versions giving a fromIndex/toIndex range of elements to sort
Chapter 10: Sorting 5
Conventions of Presentation
Write algorithms for arrays of Comparable objects For convenience, examples show integers These would be wrapped as Integer; or You can implement separately for int arrays
Generally use n for the length of the array Elements 0 through n-1
Selection Sort
A relatively easy to understand algorithm Sorts an array in passes Each pass selects the next smallest element At the end of the pass, places it where it belongs Efficiency is O(n2), hence called a quadratic sort Performs: O(n2) comparisons O(n) exchanges (swaps)
10
11
20
30
35
60
65
12
Bubble Sort
Compares adjacent array elements Exchanges their values if they are out of order Smaller values bubble up to the top of the array Larger values sink to the bottom
14
15
16
17
18
Insertion Sort
Based on technique of card players to arrange a hand Player keeps cards picked up so far in sorted order When the player picks up a new card Makes room for the new card Then inserts it in its proper place
20
21
22
23
24
25
26
28
29
30
31
33
Merge Sort
A merge is a common data processing operation: Performed on two sequences of data Items in both sequences use same compareTo Both sequences in ordered of this compareTo Goal: Combine the two sorted sequences in one larger sorted sequence Merge sort merges longer and longer sequences
34
35
Picture of Merge
36
Analysis of Merge
Two input sequences, total length n elements Must move each element to the output Merge time is O(n)
Must store both input and output sequences An array cannot be merged in place Additional space needed: O(n)
37
38
40
41
Heapsort
Merge sort time is O(n log n) But requires (temporarily) n extra storage items Heapsort Works in place: no additional storage Offers same O(n log n) performance Idea (not quite in-place): Insert each element into a priority queue Repeatedly remove from priority queue to array Array slots go from 0 to n-1
44
Heapsort Picture
45
46
47
Heapsort Code
public static <T extends Comparable<T>> void sort (T[] a) { buildHp(a); shrinkHp(a); } private static ... void buildHp (T[] a) { for (int n = 2; n <= a.length; n++) { int chld = n-1; // add item and reheap int prnt = (chld-1) / 2; while (prnt >= 0 && a[prnt].compareTo(a[chld])<0) { swap(a, prnt, chld); chld = prnt; prnt = (chld-1)/2 } } }
Chapter 10: Sorting 48
}
} private static ... void swap (T[] a, int i, int j) { T tmp = a[i]; a[i] = a[j]; a[j] = tmp; }
Chapter 10: Sorting 50
Heapsort Analysis
Insertion cost is log i for heap of size i Total insertion cost = log(n)+log(n-1)+...+log(1) This is O(n log n) Removal cost is also log i for heap of size i Total removal cost = O(n log n) Total cost is O(n log n)
51
Quicksort
Developed in 1962 by C. A. R. Hoare Given a pivot value: Rearranges array into two parts: Left part pivot value Right part > pivot value Average case for Quicksort is O(n log n) Worst case is O(n2)
52
Quicksort Example
53
54
Quicksort Code
public static <T extends Comparable<T>> void sort (T[] a) { qSort(a, 0, a.length-1); } private static <T extends Comparable<T>> void qSort (T[] a, int fst, int lst) { if (fst < lst) { int pivIndex = partition(a, fst, lst); qSort(a, fst, pivIndex-1); qSort(a, pivIndex+1, lst); } }
55
57
Partitioning Code
private static <T extends Comparable<T>> int partition (T[] a, int fst, int lst) { T pivot = a[fst]; int u = fst; int d = lst; do { while ((u < lst) && (pivot.compareTo(a[u]) >= 0)) u++; while (pivot.compareTo(a[d]) < 0) d++; if (u < d) swap(a, u, d); } while (u < d);
Chapter 10: Sorting 58
59
60
61
62
63
Chapter Summary
Three quadratic sorting algorithms: Selection sort, bubble sort, insertion sort Shell sort: good performance for up to 5000 elements Quicksort: average-case O(n log n) If the pivot is picked poorly, get worst case: O(n2) Merge sort and heapsort: guaranteed O(n log n) Merge sort: space overhead is O(n) Java API has good implementations
64
65