Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Sorting 7 2  9 4   2   4   7

Similar presentations


Presentation on theme: "Advanced Sorting 7 2  9 4   2   4   7"— Presentation transcript:

1 Advanced Sorting 7 2  9 4  2 4 7 9 7  2  2 7 9  4  4 9 7  7
Merge Sort 1/12/2018 5:57 AM Advanced Sorting 7 2   7  2  2 7 9  4  4 9 7  7 2  2 9  9 4  4 Merge Sort

2 Overview Non-Comparison Methods Comparison Methods Bucket Sort
Radix Sort Comparison Methods Quick Sort Merge Sort Merge Sort

3 Bucket Sort Bucket sort is a stable sorting algorithm. Approach:
i.e. the relative order of any two items with the same key is preserved. Approach: Set up m buckets where each bucket is responsible for an equal portion of the range of keys in the array. Place items in appropriate buckets. Sort items in each non-empty bucket using insertion sort. Concatenate sorted lists of items from buckets to get final sorted order.

4 Example of Bucket Sort The example uses an input array of 9 elements. Key values are in the range from 10 to 19. It uses an auxiliary array of linked lists which is used as buckets. Items are placed in appropriate buckets and links are maintained to point to the next element. Order of the two keys with value 15 is maintained after sorting.

5 Bucket Sort Pseudo code of Bucket sort is:

6 Bucket Sort Analysis of running time of Bucket sort:
Buckets are created based on the range of elements in the array. This is a linear time operation. Each element is placed in its corresponding bucket, which takes linear time. Insertion sort takes a quadratic time to run. Concatenating sorted lists takes a linear time. So, in general running time is O(n2). A more detailed mathematical analysis can show that, given the range of numbers, m, and an array size, n, running time is O(m+n) so long as m is order n. m is the range of numbers and n is the size of the array.

7 Example of Bucket Sort The example uses an input array of 9 elements. Key values are in the range from 10 to 19. It uses an auxiliary array of linked lists which is used as buckets. Items are placed in appropriate buckets and links are maintained to point to the next element. Order of the two keys with value 15 is maintained after sorting.

8 Overview Non-Comparison Methods Comparison Methods Bucket Sort
Radix Sort Comparison Methods Quick Sort Merge Sort Merge Sort

9 Radix Sort Radix: the base of a number system or logarithm.
Uses a stable sorting algorithm so is also stable Radix sort is a multiple pass distribution sort. It distributes each item to a bucket according to part of the item's key. After each pass, items are collected from the buckets, keeping the items in order, then redistributed according to the next most significant part of the key. For numbers, it sorts keys digit-by-digit, for strings it sorts character-by-character.

10 Example of Radix Sort Input is an array of 15 integers. For integers, the number of buckets is 10, from 0 to 9. The first pass distributes the keys into buckets by the least significant digit (LSD). When the first pass is done, we have the following.

11 Example of Radix Sort We collect these, keeping their relative order: Now we distribute by the next most significant digit, which is the highest digit in our example, and we get the following. When we collect them, they are in order.

12 Radix Sort Running-time of Radix sort is in Ө(d(n + k)), where:
n is instance size or number of elements that need to be sorted, k is the number of buckets that can be generated, and d is the number of digits in the element, or length of the keys. Pseudo code of Radix sort is:

13 Discussion Advantages
Radix and bucket sorts are stable, preserving existing order of equal keys. They work in linear time, unlike most other sorts. In other words, they do not slow down when large numbers of items need to be sorted. Most sorts run in O(n log n) or O(n2) time. The time to sort per item is constant, as no comparisons among items are made. With other sorts, the time to sort per time increases with the number of items. Radix sort is particularly efficient when you have large numbers of records to sort with short keys. Drawbacks Radix and bucket sorts do not work well when keys are very long, as the total sorting time is proportional to key length and to the number of items to sort. They are not “in-place”, using more working memory than a traditional sort.

14 Overview Non-Comparison Methods Comparison Methods Bucket Sort
Radix Sort Comparison Methods Merge Sort Quick Sort Merge Sort

15 Divide-and-Conquer Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S in two disjoint subsets S1 and S2 Recur: solve the subproblems associated with S1 and S2 Conquer: combine the solutions for S1 and S2 into a solution for S The base case for the recursion are subproblems of size 0 or 1 Merge-sort is a sorting algorithm based on the divide-and-conquer paradigm Like heap-sort It uses a comparator It has O(n log n) running time Unlike heap-sort It does not use an auxiliary priority queue It accesses data in a sequential manner (suitable to sort data on a disk) Merge Sort

16 Merge-Sort Merge-sort on an input sequence S with n elements consists of three steps: Divide: partition S into two sequences S1 and S2 of about n/2 elements each Recur: recursively sort S1 and S2 Conquer: merge S1 and S2 into a unique sorted sequence Algorithm mergeSort(S, C) Input sequence S with n elements, comparator C Output sequence S sorted according to C if S.size() > 1 (S1, S2)  partition(S, n/2) mergeSort(S1, C) mergeSort(S2, C) S  merge(S1, S2) Merge Sort

17 Merging Two Sorted Sequences
The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time Algorithm merge(A, B) Input sequences A and B with n/2 elements each Output sorted sequence of A  B S  empty sequence while A.isEmpty()  B.isEmpty() if A.first().element() < B.first().element() S.insertLast(A.remove(A.first())) else S.insertLast(B.remove(B.first())) while A.isEmpty() S.insertLast(A.remove(A.first())) while B.isEmpty() S.insertLast(B.remove(B.first())) return S Merge Sort

18 Merge-Sort Tree An execution of merge-sort is depicted by a binary tree each node represents a recursive call of merge-sort and stores unsorted sequence before the execution and its partition sorted sequence at the end of the execution the root is the initial call the leaves are calls on subsequences of size 0 or 1 7 2   7  2  2 7 9  4  4 9 7  7 2  2 9  9 4  4 Merge Sort

19 Execution Example Partition 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9
  7 2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Merge Sort

20 Execution Example (cont.)
Recursive call, partition   7 2  9 4  7 2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Merge Sort

21 Execution Example (cont.)
Recursive call, partition   7 2  9 4  7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Merge Sort

22 Execution Example (cont.)
Recursive call, base case   7 2  9 4  7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Merge Sort

23 Execution Example (cont.)
Recursive call, base case   7 2  9 4  7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Merge Sort

24 Execution Example (cont.)
Merge   7 2  9 4  7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Merge Sort

25 Execution Example (cont.)
Recursive call, …, base case, merge   7 2  9 4  7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Merge Sort

26 Execution Example (cont.)
Merge   7 2  9 4  7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Merge Sort

27 Execution Example (cont.)
Recursive call, …, merge, merge   7 2  9 4  7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Merge Sort

28 Execution Example (cont.)
Merge   7 2  9 4  7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Merge Sort

29 Analysis of Merge-Sort
The height h of the merge-sort tree is O(log n) at each recursive call we divide in half the sequence, The overall amount or work done at the nodes of depth i is O(n) we partition and merge 2i sequences of size n/2i we make 2i+1 recursive calls Thus, the total running time of merge-sort is O(n log n) depth #seqs size 1 n 2 n/2 i 2i n/2i Merge Sort

30 Overview Non-Comparison Methods Comparison Methods Bucket Sort
Radix Sort Comparison Methods Merge Sort Quick Sort Merge Sort

31 Quick-Sort Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm: Divide: pick a random element x (called pivot) and partition S into L elements less than x E elements equal x G elements greater than x Recur: sort L and G Conquer: join L, E and G x x L G E x Quick-Sort

32 Partition We partition an input sequence as follows:
We remove, in turn, each element y from S and We insert y into L, E or G, depending on the result of the comparison with the pivot x Each insertion and removal is at the beginning or at the end of a sequence, and hence takes O(1) time Thus, the partition step of quick-sort takes O(n) time Algorithm partition(S, p) Input sequence S, position p of pivot Output subsequences L, E, G of the elements of S less than, equal to, or greater than the pivot, resp. L, E, G  empty sequences x  S.remove(p) while S.isEmpty() y  S.remove(S.first()) if y < x L.insertLast(y) else if y = x E.insertLast(y) else { y > x } G.insertLast(y) return L, E, G Quick-Sort

33 Quick-Sort Tree An execution of quick-sort is depicted by a binary tree Each node represents a recursive call of quick-sort and stores Unsorted sequence before the execution and its pivot Sorted sequence at the end of the execution The root is the initial call The leaves are calls on subsequences of size 0 or 1 4 2  2 4 7 9  7 9 2  2 9  9 Quick-Sort

34 Execution Example Pivot selection 7 2 9 4 3 7 6 1  1 2 3 4 6 7 8 9
2  2 9 4  4 9 3  3 8  8 9  9 4  4 Quick-Sort

35 Execution Example (cont.)
Partition, recursive call, pivot selection 2  2 9 4  4 9 3  3 8  8 9  9 4  4 Quick-Sort

36 Execution Example (cont.)
Partition, recursive call, base case    1  1 9 4  4 9 3  3 8  8 9  9 4  4 Quick-Sort

37 Execution Example (cont.)
Recursive call, …, base case, join 1  1 4 3  3 4 3  3 8  8 9  9 4  4 Quick-Sort

38 Execution Example (cont.)
Recursive call, pivot selection 1  1 4 3  3 4 8  8 9  9 9  9 4  4 Quick-Sort

39 Execution Example (cont.)
Partition, …, recursive call, base case 1  1 4 3  3 4 8  8 9  9 9  9 4  4 Quick-Sort

40 Execution Example (cont.)
Join, join 1  1 4 3  3 4 8  8 9  9 9  9 4  4 Quick-Sort

41 Worst-case Running Time
The worst case for quick-sort occurs when the pivot is the unique minimum or maximum element One of L and G has size n - 1 and the other has size 0 The running time is proportional to the sum n + (n - 1) + … Thus, the worst-case running time of quick-sort is O(n2) depth time n 1 n - 1 Quick-Sort

42 In-Place Quick-Sort Quick-sort can be implemented to run in-place
In the partition step, we use replace operations to rearrange the elements of the input sequence such that the elements less than the pivot have rank less than h the elements equal to the pivot have rank between h and k the elements greater than the pivot have rank greater than k The recursive calls consider elements with rank less than h elements with rank greater than k Algorithm inPlaceQuickSort(S, l, r) Input sequence S, ranks l and r Output sequence S with the elements of rank between l and r rearranged in increasing order if l  r return i  a random integer between l and r x  S.elemAtRank(i) (h, k)  inPlacePartition(x) inPlaceQuickSort(S, l, h - 1) inPlaceQuickSort(S, k + 1, r) Quick-Sort

43 In-Place Partitioning
Perform the partition using two indices to split S into L and E U G (a similar method can split E U G into E and G). Repeat until j and k cross: Scan j to the right until finding an element > x. Scan k to the left until finding an element < x. Swap elements at indices j and k j k (pivot = 6) j k Quick-Sort

44 Summary of Sorting Algorithms
Time Notes selection-sort O(n2) in-place slow (good for small inputs) insertion-sort quick-sort O(n log n) expected in-place, randomized fastest (good for large inputs) heap-sort O(n log n) fast (good for large inputs) merge-sort sequential data access fast (good for huge inputs) Quick-Sort


Download ppt "Advanced Sorting 7 2  9 4   2   4   7"

Similar presentations


Ads by Google