sorting algorithms: elementary advanced Sorting Data Structures and Algorithms in Java, Third EditionCh09 – 1
insertionsort(data[]) for i = 1 to data.length-1 tmp = data[i]; move all elements data[j] greater than tmp by one position ; place tmp in its proper position ; Insertion sort: algorithm Data Structures and Algorithms in Java, Third EditionCh09 – 2
Insertion sort: example Data Structures and Algorithms in Java, Third EditionCh09 – 3
Insertion sort: computational complexity … + (n – 2) + (n – 1) = n(n – 1) 2 best caseavg caseworst case comparisons movements n(n – 1) 2 n(n–1) 2 + 2(n–1) n 2 + n – 2 4 n 2 + 5n – 6 4 (ordered)(reversed)(random) n – 1 2(n – 1) best caseavg case worst case comparisons O(n)O(n)O(n2)O(n2)O(n2)O(n2) movements O(n)O(n)O(n2)O(n2)O(n2)O(n2) Data Structures and Algorithms in Java, Third EditionCh09 – 4
selectionsort(data[]) for i = 0 to data.length-2 select the smallest elements among data[i], …, data[data.length-1]; swap it with data[i]; Selection sort: algorithm Data Structures and Algorithms in Java, Third EditionCh09 – 5
Selection sort: example Data Structures and Algorithms in Java, Third EditionCh09 – 6
best caseavg caseworst case comparisons movements n(n – 1) 2 3(n–1) (ordered) (largest first, the rest is ordered) (random) 0 best caseavg case worst case comparisons O(n2)O(n2)O(n2)O(n2)O(n2)O(n2) movements 0O(n)O(n)O(n)O(n) n(n – 1) 2 n(n – 1) 2 c(n–1) Data Structures and Algorithms in Java, Third EditionCh09 – 7 Selection sort: computational complexity
Bubble sort: algorithm bubblesort(data[]) for i = 0 to data.length-2 for j = data.length-1 downto i+1 if elements in positions j and j-1 are out of order swap them ; Data Structures and Algorithms in Java, Third EditionCh09 – 8
Bubble sort: example Data Structures and Algorithms in Java, Third EditionCh09 – 9
Bubble sort: computational complexity best caseavg caseworst case comparisons movements 0 best caseavg caseworst case comparisons O(n2)O(n2)O(n2)O(n2)O(n2)O(n2) movements 0O(n2)O(n2)O(n2)O(n2) n(n – 1) 2 3n(n – 1) 4 (ordered) n(n – 1) 2 n(n – 1) 2 3n(n – 1) 2 (random) (reversed) Data Structures and Algorithms in Java, Third EditionCh09 – 10
Basic sorting algorithms: comparison insertion best caseavg case worst case comparisons O(n)O(n)O(n2)O(n2)O(n2)O(n2) movements O(n)O(n)O(n2)O(n2)O(n2)O(n2) selection best caseavg caseworst case comparisons O(n2)O(n2)O(n2)O(n2)O(n2)O(n2) movements 0O(n)O(n)O(n)O(n) bubble best caseavg caseworst case comparisons O(n2)O(n2)O(n2)O(n2)O(n2)O(n2) movements 0O(n2)O(n2)O(n2)O(n2) Data Structures and Algorithms in Java, Third EditionCh09 – 11
Divide-and-conquer sorting DCsorting(data[]) partition data[] into data1[] and data2[]; DCsorting(data1[]); DCsorting(data2[]); merge data1[] and data2[] into data[]; quicksort mergesort Data Structures and Algorithms in Java, Third EditionCh09 – 12
Merging ordered arrays into one array best caseavg caseworst case comparisons movements n – 1 n/2 cn – 1 n n n n elements Data Structures and Algorithms in Java, Third EditionCh09 – 13
mergesort(data[], first, last) if first < last mid = (first + last) / 2; mergesort(data[], first, mid); mergesort(data[], mid+1, last); merge(data[], first, last); Mergesort: algorithm partitioning merging Data Structures and Algorithms in Java, Third EditionCh09 – 14
Mergesort: example Data Structures and Algorithms in Java, Third EditionCh09 – 15
Mergesort: computational complexity C(n) = 2C(n/2) + n – 1 = 2(2C(n/4) + n/2 – 1) + n – 1 = 4C(n/4) + 2n – 3 = 4(2C(n/8) + n/4 – 1) + 2n – 3 = 8C(n/8) + 3n – 7... = 2 i C(n/2 i ) + in – (2 i – 1) = in – 2 i + 1 = nlgn – n + 1 = O(nlgn) assume that n = 2 i C(n) = 0 if n = 1 2C(n/2) + n – 1 otherwise Data Structures and Algorithms in Java, Third EditionCh09 – 16
Quicksort: algorithm quicksort(data[]) if data.length > 1 choose pivot; while there are elements left in data include element either in data1[] = { el : el ≤ pivot}; or in data2[] = { el : el ≥ pivot}; quicksort(data1[]); quicksort(data2[]); partitioning no merging Data Structures and Algorithms in Java, Third EditionCh09 – 17
Quicksort: implementation private > void quicksort(T[] data, int first, int last) { int lower = first + 1, upper = last; swap(data,first,(first+last)/2); T pivot = data[first]; while (lower <= upper) { while (pivot.compareTo(data[lower]) > 0) lower++; while (pivot.compareTo(data[upper]) < 0) upper--; if (lower < upper) swap(data,lower++,upper--); else lower++; } swap(data,upper,first); if (first < upper-1) quicksort(data,first,upper-1); if (upper+1 < last) quicksort(data,upper+1,last); } partitioning Data Structures and Algorithms in Java, Third EditionCh09 – 18
Quicksort: implementation, one pass p≤ pbc?de≥ p b p p≤ pbc?de≥ p b < p, e ≤ p p≤ pbc?de≥ p b ≥ p, e > p p≤ pbc?de≥ p b ≥ p, e ≤ p p≤ pec?db≥ p ≤ p≥ p a p f p first step last step intermediate step
Quicksort: example largest to the last cellpivot to the first cell partitioning pivot to final cell pivot to the first cell partitioningpivot to final cell 43 2 pivot to the first cell 2 partitioning pivot to final cell
Quicksort: computational complexity n best case: partitioning into two even-size arrays Number of comparisons (approximations): n/2 n/ ………………. ……………………………….. n + n + … + n = n–1 worst case: one array after partitioning is empty Number of comparisons: n–2 n–3 1 ………………………….. (n–1) + (n–2) + (n–3) + … + 1 = (n –1)n 2 nlgn Data Structures and Algorithms in Java, Third EditionCh09 – 21
Heap sort heapsort(data[]) transform data into a heap ; for i = data.length-1 downto 2 swap the root with the element in position i; restore the heap property for the tree data[0], …, data[i-1]; Data Structures and Algorithms in Java, Third EditionCh09 – 22
Heap sort: example, transforming data into a heap last nonleaf Data Structures and Algorithms in Java, Third EditionCh09 – 23
Heap sort: example (cont’d) sorting Data Structures and Algorithms in Java, Third EditionCh09 – 24
Heap sort: example (cont’d) sorting Data Structures and Algorithms in Java, Third EditionCh09 – 25
80,000 ascendingrandomdescending insertionsort selectionsort bubblesort combsort Shellsort heapsort mergesort quicksort quicksort2 radixsort bitRadixsort radixsort2 bitRadixsort2 countingsort m m m m m m m m Data Structures and Algorithms in Java, Third EditionCh09 – 26 Sorting methods: comparison of runtimes