Presentation is loading. Please wait.

Presentation is loading. Please wait.

Foundation of Computing Systems

Similar presentations


Presentation on theme: "Foundation of Computing Systems"— Presentation transcript:

1 Foundation of Computing Systems
IT60101:Foundation of Computing Systems Foundation of Computing Systems Lecture 17 Sorting Algorithms II IT 60101: Lecture #17 School of Information Technology: IIT Kharagpur

2 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 IT 60101: Lecture #17

3 Sorting by Exchange: Bubble Sort
IT 60101: Lecture #17

4 Bubble Sort: Illustration
IT 60101: Lecture #17

5 Complexity Analysis of Bubble Sort
Case 1: The input list is already in sorted order Number of comparisons Number of movements M (n) = 0 IT 60101: Lecture #17

6 Complexity Analysis of Bubble Sort
Case 2: The input list is sorted but in reverse order Number of comparisons Number of movements IT 60101: Lecture #17

7 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 IT 60101: Lecture #17

8 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 IT 60101: Lecture #17

9 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 IT 60101: Lecture #17

10 Bubble Sort: Refinements
Funnel sort Cocktail sort IT 60101: Lecture #17

11 Cocktail Sort IT 60101: Lecture #17

12 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: IT 60101: Lecture #17

13 Shell Sort: Illustration
IT 60101: Lecture #17

14 Shell Sort: Illustration
IT 60101: Lecture #17

15 Shell Sort: Illustration
IT 60101: Lecture #17

16 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 IT 60101: Lecture #17

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) IT 60101: Lecture #17

18 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………. IT 60101: Lecture #17

19 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 IT 60101: Lecture #17

20 Sorting by Exchange: Quick Sort
Divide-and-Conquer IT 60101: Lecture #17

21 Divide-and-Conquer in Quick Sort
IT 60101: Lecture #17

22 Partition Method in Quick Sort
IT 60101: Lecture #17

23 Partition Method in Quick Sort
IT 60101: Lecture #17

24 Partition Method: Illustration
IT 60101: Lecture #17

25 Partition Method: Illustration
IT 60101: Lecture #17

26 Partition Method: Illustration
IT 60101: Lecture #17

27 Partition Method: Illustration
IT 60101: Lecture #17

28 Partition Method: Illustration
IT 60101: Lecture #17

29 Quick Sort: Illustration
IT 60101: Lecture #17

30 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 IT 60101: Lecture #17

31 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 IT 60101: Lecture #17

32 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 IT 60101: Lecture #17

33 Complexity Analysis of Quick Sort
Case 3: Elements in the list are in random order Number of comparisons with C(1) = C(0) = 0 IT 60101: Lecture #17

34 Complexity Analysis of Quick Sort
Case 3: Elements in the list are in random order Number of Movements IT 60101: Lecture #17

35 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 IT 60101: Lecture #17

36 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 IT 60101: Lecture #17

37 Lomuto’s quick sort IT 60101: Lecture #17

38 Summary: Sorting by Exchange
Algorithm Best case Worst case Average case Bubble sort Shell sort Quick sort IT 60101: Lecture #17

39 Summary: Sorting by Exchange
IT 60101: Lecture #17

40 Summary: Sorting by Exchange
IT 60101: Lecture #17

41 Summary: Sorting by Exchange
IT 60101: Lecture #17


Download ppt "Foundation of Computing Systems"

Similar presentations


Ads by Google