Foundation of Computing Systems IT60101:Foundation of Computing Systems 04.09.09 Foundation of Computing Systems Lecture 17 Sorting Algorithms II 04.09.09 IT 60101: Lecture #17 School of Information Technology: IIT Kharagpur
Sorting by Exchange Interchange (exchange) pairs of elements that are out of order until no more such pair exists. Bubble sort Shell sort Quick sort 04.09.09 IT 60101: Lecture #17
Sorting by Exchange: Bubble Sort 04.09.09 IT 60101: Lecture #17
Bubble Sort: Illustration 04.09.09 IT 60101: Lecture #17
Complexity Analysis of Bubble Sort Case 1: The input list is already in sorted order Number of comparisons Number of movements M (n) = 0 04.09.09 IT 60101: Lecture #17
Complexity Analysis of Bubble Sort Case 2: The input list is sorted but in reverse order Number of comparisons Number of movements 04.09.09 IT 60101: Lecture #17
Complexity Analysis of Bubble Sort Case 3: Elements in the input list are in random order Number of comparisons Number of movements Let pj be the probability that the largest element is in the unsorted part is in j-th (1≤j≤n-i+1) location. The average number of swaps in the i-th pass is 04.09.09 IT 60101: Lecture #17
Complexity Analysis of Bubble Sort Case 3: Elements in the input list are in random order Number of movements The average number of swaps in the i-th pass is The average number of movements 04.09.09 IT 60101: Lecture #17
Bubble Sort: Summary 04.09.09 IT 60101: Lecture #17 Case Comparisons Movement Memory Remark Case 1 Input list is in sorted order Case 2 Input list is sorted in reversed order Case 3 Input list is in random order Run time, T(n) Complexity Remark Best case Worst case Average case 04.09.09 IT 60101: Lecture #17
Bubble Sort: Refinements Funnel sort Cocktail sort 04.09.09 IT 60101: Lecture #17
Cocktail Sort 04.09.09 IT 60101: Lecture #17
Sorting by Exchange: Shell Sort Sorting methods based on comparison Comparisons and hence movements of data take place between adjacent entries only This leads to a number of redundant comparisons and data movements A mechanism should be followed with which the comparisons can take in long leaps instead of short Donald L. Shell (1959) Use increments: 04.09.09 IT 60101: Lecture #17
Shell Sort: Illustration 04.09.09 IT 60101: Lecture #17
Shell Sort: Illustration 04.09.09 IT 60101: Lecture #17
Shell Sort: Illustration 04.09.09 IT 60101: Lecture #17
Issues in Shell Sort Algorithm to be used to sort subsequences in shell sort Straight insertion sort Shell sort is better than the insertion sort Lower number of passes than n number of passes in insertion sort Deciding the values of increments Several choices have been made 04.09.09 IT 60101: Lecture #17
Increments in Shell Sort 1: Only two increments h and 1 h is approximately = Time complexity (average) = 2: ht = 2t -1 for 1≤ t ≤ log2n, where n is the size of the input list Increments: 2t -1, …, 15, 7, 3, 1 Time complexity : (worst) (average) O(n5/3) 04.09.09 IT 60101: Lecture #17
Increments in Shell Sort 3: ht = (3t -1)/2 for 1≤ t ≤ l Where l is chosen as the smallest integer such that , n being the size of the input list Worst case time complexity is O(n3/2 ) Many more………. 04.09.09 IT 60101: Lecture #17
Complexity Analysis of Shell Sort Case Comparisons Movement Memory Remark Case 1 Input list is in sorted order Case 2 Input list is sorted in reverse order Case 3 Input list is in random order Run time, T(n) Complexity Remark Best case Worst case Average case 04.09.09 IT 60101: Lecture #17
Sorting by Exchange: Quick Sort Divide-and-Conquer 04.09.09 IT 60101: Lecture #17
Divide-and-Conquer in Quick Sort 04.09.09 IT 60101: Lecture #17
Partition Method in Quick Sort 04.09.09 IT 60101: Lecture #17
Partition Method in Quick Sort 04.09.09 IT 60101: Lecture #17
Partition Method: Illustration 04.09.09 IT 60101: Lecture #17
Partition Method: Illustration 04.09.09 IT 60101: Lecture #17
Partition Method: Illustration 04.09.09 IT 60101: Lecture #17
Partition Method: Illustration 04.09.09 IT 60101: Lecture #17
Partition Method: Illustration 04.09.09 IT 60101: Lecture #17
Quick Sort: Illustration 04.09.09 IT 60101: Lecture #17
Complexity Analysis of Quick Sort Memory requirement Size of the stack = Number of comparisons Let, T(n) represents total time to sort n elements and P(n) represents the time for perform a partition of a list of n elements T(n) = P(n) +T(nl) +T(nr) with T(1) = T(0) = 0 where, nl = number of elements in the left sub list nr = number of elements in the right sub list and 0 ≤ nl, nr < n 04.09.09 IT 60101: Lecture #17
Complexity Analysis of Quick Sort Case 1: Elements in the list are in ascending order Number of comparisons Number of movements C(n) = n-1 + C(n-1), with C(1) = C(0) = 0 M(n) = 0 04.09.09 IT 60101: Lecture #17
Complexity Analysis of Quick Sort Case 2: Elements in the list are in reverse order Number of comparisons Number of movements C(n) = n-1 + C(n-1), with C(1) = C(0) = 0 04.09.09 IT 60101: Lecture #17
Complexity Analysis of Quick Sort Case 3: Elements in the list are in random order Number of comparisons with C(1) = C(0) = 0 04.09.09 IT 60101: Lecture #17
Complexity Analysis of Quick Sort Case 3: Elements in the list are in random order Number of Movements 04.09.09 IT 60101: Lecture #17
Complexity Analysis: Summary Case Comparisons Movement Memory Remark Case 1 Input list is in sorted order Case 2 Input list is sorted in reversed order Case 3 Input list is in random order Run time, T(n) Complexity Remark Worst case Best/average case 04.09.09 IT 60101: Lecture #17
Variations in Quick Sort Randomized quick sort Random sampling quick sort Singleton’s quick sort Multi-partition quick sort Leftist quick sort Lomuto’s quick sort 04.09.09 IT 60101: Lecture #17
Lomuto’s quick sort 04.09.09 IT 60101: Lecture #17
Summary: Sorting by Exchange Algorithm Best case Worst case Average case Bubble sort Shell sort Quick sort 04.09.09 IT 60101: Lecture #17
Summary: Sorting by Exchange 04.09.09 IT 60101: Lecture #17
Summary: Sorting by Exchange 04.09.09 IT 60101: Lecture #17
Summary: Sorting by Exchange 04.09.09 IT 60101: Lecture #17