Presentation is loading. Please wait.

Presentation is loading. Please wait.

Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.

Similar presentations


Presentation on theme: "Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2."— Presentation transcript:

1 quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2

2

3

4

5 sortingquick sort Demo with cards? 1.Divide S into 3 parts as follows Select an element e Create L with all element is S less than e Create E with all element in S equal to e Create G with all element in S greater than e 2.Recur by quick sorting L and quick sorting G 3.Conquer by appending L to E to G Divide & Conquer (with quick sort)

6 sortingquick sort Demo with cards?

7 sortingquick sort

8 sortingquick sort

9 sortingquick sort

10 sortingquick sort

11 sortingquick sort

12 sortingquick sort

13 sortingquick sort

14 sortingquick sort

15 sortingquick sort

16 sortingquick sort

17 sortingquick sort Stopping condition

18 sortingquick sort Less than, Equal to, Greater than

19 sortingquick sort pivot

20 sortingquick sort Get the data sets L, E, and G

21 sortingquick sort Recurse & conquer

22 sortingquick sort

23 sortingquick sort.. and this happens if S is sorted!

24 sortingquick sort

25 sortingquick sort Two enhancements improve selection of pivot randomise “in place” sorting uses no more space and is fast

26 sorting A lower bound on comparison based sorting i.e. how good can it get?

27 sorting A lower bound on comparison based sorting Assume we are given a sequence S, of length n, to be sorted (no duplicates) an algorithm compares pairs of elements with two outcomes x < y TRUE x < y FALSE we can represent a comparison based sorting algorithm as a decision tree i.e. represent its behaviour internal node represents a decision left is TRUE, right is FALSE there are n! possible orderings of S there must be n! leaves in our decision tree one for each possible ordering of S running time of algorithm must be at least the height of the decision tree the height of the decision tree is at least log(n!) base 2 log(n!) is O(n.log(n))

28 sorting stable sorting

29 sortingstable sorting A sorting algorithm is stable if for any two entries x and y of S such that x.getKey() == y.getKey() and x precedes y in S before sorting x precedes y after S is sorted

30 sorting bucket sort aka pigeonhole sort

31 sorting bucket sort aka pigeonhole sort PIGEONHOLE SORT

32 sortingbucket sort (aka pigeonhole sort) Use the sey of an object as an index into a bucket array Assume keys are in range [0,N-1] Assume we have n object to sort insert object e into bucket[e.getKey()] Complexity is O(n+N)

33 sortingbucket sort (aka pigeonhole sort) Use the sey of an object as an index into a bucket array Assume keys are in range [0,N-1] Assume we have n object to sort insert object e into bucket[e.getKey()] Complexity is O(n+N) How might we do this in java? What data structures would we use? Is it stable? What kind of data sets would be suitable?

34 sorting radix sort We will describe by example

35 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on least significant digit

36 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on least significant digit

37 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89 Sort on least significant digit

38 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89 Sort on least significant digit

39 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89 28 Sort on least significant digit

40 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89 28 Sort on least significant digit

41 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89 28 81 Sort on least significant digit

42 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89 28 81 Sort on least significant digit

43 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89, 69 28 81 Sort on least significant digit

44 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89, 69 28 81 Sort on least significant digit

45 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89, 69 28 81 14 Sort on least significant digit

46 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89, 69 28 81 14 Sort on least significant digit

47 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89, 69 28 81, 31 14 Sort on least significant digit

48 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89, 69 28 81, 31 14 Sort on least significant digit

49 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89, 69, 29 28 81, 31 14 Sort on least significant digit

50 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89, 69, 29 28 81, 31 14 Sort on least significant digit

51 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89, 69, 29 28, 18 81, 31 14 Sort on least significant digit

52 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89, 69, 29 28, 18 81, 31 14 Sort on least significant digit

53 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89, 69, 29,39 28, 18 81, 31 14 Sort on least significant digit

54 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89, 69, 29,39 28, 18 81, 31 14 Sort on least significant digit

55 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89, 69, 29,39 28, 18 81, 31 14 17 Sort on least significant digit

56 sortingradix sort 5 6 7 8 9 0 1 2 3 4 89, 69, 29,39 28, 18 81, 31 14 17 Sorted on least significant digit!

57 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit

58 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit

59 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81

60 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81

61 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81 31

62 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81 31

63 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81 31 14

64 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81 31 14

65 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81 31 14, 17

66 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81 31 14, 17 28

67 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81 31 14, 17 28

68 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81 31 14, 17, 18 28

69 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81 31 14, 17, 18 28

70 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81, 89 31 14, 17, 18 28

71 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81, 89 31 14, 17, 18 28

72 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81, 89 31 14, 17, 18 28 69

73 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81, 89 31 14, 17, 18 28 69

74 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81, 89 31 14, 17, 18 28, 29 69

75 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81, 89 31 14, 17, 18 28, 29 69

76 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sort on next (second) significant digit 81, 89 31, 39 14, 17, 18 28, 29 69

77 sortingradix sort 5 6 7 8 9 0 1 2 3 4 Sorted on next (second) significant digit! 81, 89 31, 39 14, 17, 18 28, 29 69

78 sortingradix sort We can extend this to numbers of any length We can do this on strings Considering numbers to base b require b buckets require d = log(m) scans (d is number of digits) log is to base b m is largest number to be sorted each scan is of order n therefore O(d.n)

79 sorting comparison of algorithms

80 sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort

81 sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort

82 sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort

83 sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort

84 sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort

85 sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort

86 sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort

87 sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort

88 sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort

89 sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort

90

91

92

93

94 sorting Yes, but how did you sort out your CD’s?

95 not coveredsorting Sift sort Shell sort Bead sort

96 not coveredsorting Sift sort Shell sort Bead sort

97 not coveredsorting Sift sort Shell sort Bead sort

98 not coveredsorting

99

100

101

102


Download ppt "Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2."

Similar presentations


Ads by Google