Download presentation
Presentation is loading. Please wait.
1
Data Structures and Algorithms I Sorting
CMP 338 Data Structures and Algorithms I Sorting
2
Selection Sort Worst-case: ~N2/2 <'s; ~N <=>'s
public static void sort(Comparable[] a) int N = a.length for (int i=0; i<N; i++) int min=i for (int j=i+1; j<N; j++) if (a[j] < a[min]) min=j exch(a, i, min) Worst-case: ~N2/2 <'s; ~N <=>'s Average-case: ~N2/2 <'s; ~N <=>'s Best-case: ~N2/2 <'s; ~N <=>'s Not recommended unless <=>'s are very expensive
3
Insertion Sort Worst-case: ~N2/2 <'s; ~N2/2 <=>'s
public static void sort(Comparable[] a) int N = a.length for (int i=0; i<N; i++) for (int j=i; 0<j && a[j]<a[j-1]; j--) exch(a, j, j-1) Worst-case: ~N2/2 <'s; ~N2/2 <=>'s Average-case: ~N2/4 <'s; ~N2/4 <=>'s Almost sorted: ~cN <'s; ~c'N <=>'s Stable, acceptable, if N is small or a is almost sorted
4
Shell Sort Worst-case: ~cN2 <'s; ~c'N2 <=>'s
public static void sort(Comparable[] a) int N=a.length; h=1; while(h<N/3) h=3h+1 while(1<=h) for (int i=h; i<N; i++) for (int j=i; h<=j&&a[j]<a[j-h]; j-=h) exch(a, j, j-1) h /= 3 Worst-case: ~cN <'s; ~c'N2 <=>'s Average-case: ~cN <'s; ~N1.5/4 <=>'s Almost sorted: ~(c+k)N <'s; ~(c'+k)N <=>'s Acceptable, unless N is very big or sort called often
5
Merge Sort Worst-case: ~N lg N <'s; ~6N lg N array accesses
sort(Comparable[] a, int lo, int hi) if (hi<=lo) return; int mid = lo + (hi-lo)/2 sort(a, lo, mid) sort(a, mid+1, hi) merge(a, lo, mid, hi) Worst-case: ~N lg N <'s; ~6N lg N array accesses Average-case: ~N lg N <'s; ~6N lg N array accesses Almost sorted: ~N lg N <'s; ~6N lg N array accesses Stable; acceptable, unless scratch space is an issue
6
Merge merge(Comparable[] a, int lo, mid, hi) int i=lo; j=mid+1
for (int k=lo; k<=hi; k++) aux[k] = a[k] for (int k=lo; k<=hi; k++) if (mid<i) a[k]=aux[j++] else if (hi<j) a[k]=aux[i++] else if (aux[j]<aux[i]) a[k]=aux[j++] else a[k]=aux[i++]
7
Quick Sort Worst-case: ~ N2/2 <'s;
sort(Comparable[] a) StdRandom.shuffle(a); sort(a, 0, ||a||-1) sort(Comparable[] a, int lo, int hi) if (hi<=lo) return; int j = partition(a, lo, hi) sort(a, lo, j-1) sort(a, j+1, hi) Worst-case: ~ N2/2 <'s; Average-case: ~ 2N lg N <'s; ~ N lg N / 3 <=>'s Preferred, unless worst-case would be a disaster
8
Partition int partition(Comparable[] a, int lo, hi) int i=lo, j=hi+1
while (true); while (a[++i] < a[lo]) if (i==hi) break // necessary, why? while (a[lo] < a[--j]) if (j==lo) break // unnecessary, why? if (j<=i) exch(a, lo, j); return j exch(a, i, j)
9
Heap Sort Worst-case: ~ 2N lg N <'s; ~ N lg N <=>'s
sort(Comparable[] a) MaxPQ pq = new MaxPQ() for (int i=0; i<||a||; i++) pq.insert(a[i]) for (int i=||a||-1; 0<=i; i--) a[i] = pq.delMax() Worst-case: ~ 2N lg N <'s; ~ N lg N <=>'s Average-case: ~ 2N lg N <'s; ~ N lg N <=>'s Perfectly acceptable, no extra space required Quicksort and Merge sort may be slightly faster
10
Heap Sort in an Array Build heap
Find min element and swap with index 0 int N = ||a||-1 for j=N; 0<j; j-- (~ cN, why?) sink(j) Create sorted array while 0<N (~ cN lg N) exch(1, --N) sink(1)
11
Sink and Swim in an Array
Indices of children of node at index i: 2i and 2i+1 Root index is 1 (not 0) swim (int k) while i<k && a[k] < a[k/2] exch(k/2, k); k = k/2 sink(int k) while 2*k <= N j = 2*k if j<N && a[j] < a[j+1] j++ exch(j, k); k = j
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.