Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting algorithms: elementary advanced Sorting Data Structures and Algorithms in Java, Third EditionCh09 – 1.

Similar presentations


Presentation on theme: "Sorting algorithms: elementary advanced Sorting Data Structures and Algorithms in Java, Third EditionCh09 – 1."— Presentation transcript:

1 sorting algorithms: elementary advanced Sorting Data Structures and Algorithms in Java, Third EditionCh09 – 1

2 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

3 2 5 3 4 Insertion sort: example 2 5 1 3 4 5 2 5 3 4 5 2 5 3 4 2 5 21 2 5 4 5 3 1 2 3 5 5 4 1 2 3 4 5 1 11 3 1 2 5 4 5 3 4 1 2 3 5 5 4 Data Structures and Algorithms in Java, Third EditionCh09 – 3

4 Insertion sort: computational complexity 1 + 2 + … + (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

5 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

6 Selection sort: example 5 3 4 1 21 3 4 2 5 1 2 4 3 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 2 5 1 3 4 Data Structures and Algorithms in Java, Third EditionCh09 – 6

7 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

8 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

9 Bubble sort: example 2 3 4 1 5 5 3 4 1 2 1 2 4 3 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 2 5 1 3 4 1 2 3 4 5 Data Structures and Algorithms in Java, Third EditionCh09 – 9

10 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

11 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

12 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

13 Merging ordered arrays into one array 2568 1579 12556789 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

14 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

15 Mergesort: example 249557362 24955 7362 249 2495 62 223455679 5573 5 24 24955 24559 3726 2367 6273 24 Data Structures and Algorithms in Java, Third EditionCh09 – 15

16 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

17 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

18 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

19 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

20 Quicksort: example 456378 2 9 largest to the last cellpivot to the first cell 25 426789 53 partitioning pivot to final cell 2 5 23 78 pivot to the first cell 4 2 22 partitioningpivot to final cell 43 2 pivot to the first cell 2 partitioning 3 2 3 3 pivot to final cell 2 2 2 5 6 5678 8 7 6 6 2234556789 8 8 6 7

21 Quicksort: computational complexity n best case: partitioning into two even-size arrays Number of comparisons (approximations): n/2 n/4 1111 ………………. ……………………………….. 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

22 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

23 Heap sort: example, transforming data into a heap 463754 4 6 3 7 5 4 last nonleaf 3 7 5 4 463754 4 3 464753 7 5 4 3 474653 7 6 5 4 7 4 4 6 5 3 744653 7 6 4 4 5 3 764453 6 Data Structures and Algorithms in Java, Third EditionCh09 – 23

24 Heap sort: example (cont’d) sorting 7 6 4 4 5 3 764453 3 6 4 4 5 7 364457 6 3 4 4 5 634457 6 5 4 4 3 654437 3 5 4 4 6 354467 5 3 4 4 534467 5 4 4 3 544367 4 4 5 344567 Data Structures and Algorithms in Java, Third EditionCh09 – 24

25 Heap sort: example (cont’d) sorting 3 4 4 5 344567 4 3 4 434567 4 3 4 434567 4 3 434567 3 4 344567 Data Structures and Algorithms in Java, Third EditionCh09 – 25

26 80,000 ascendingrandomdescending insertionsort selectionsort bubblesort combsort Shellsort heapsort mergesort quicksort quicksort2 radixsort bitRadixsort radixsort2 bitRadixsort2 countingsort.11 56 m 8.09 52 m 6.90.67 1.32 3.63 2.19.93.77 10.98 28.40 1.16 1.83.22 29 m 2.73 67 m 21.31 87 m 9.62 6.52 2.75 4.56 3.35 2.04 1.92 10.82 31.04 1.26 2.42.57 29 m 36.13 56 m 49.94 83 m 6.68 3.10 1.59 3.35 2.20.99.82 10.00 29.00 1.15 1.98.20 Data Structures and Algorithms in Java, Third EditionCh09 – 26 Sorting methods: comparison of runtimes


Download ppt "Sorting algorithms: elementary advanced Sorting Data Structures and Algorithms in Java, Third EditionCh09 – 1."

Similar presentations


Ads by Google