04.09.09
IT 60101: Lecture #17
1
Foundation of Computing Systems
Lecture 17
Sorting Algorithms II
04.09.09
IT 60101: Lecture #17
2
Sorting by Exchange
Interchange (exchange) pairs of elements that are outof order until no more such pair exists.
Bubble sort
Shell sort
Quick sort
04.09.09
IT 60101: Lecture #17
3
Sorting by Exchange: Bubble Sort
04.09.09
IT 60101: Lecture #17
4
Bubble Sort: Illustration
04.09.09
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
04.09.09
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
04.09.09
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≤jn-i+1) location.
The average number of swaps in the i-th pass is
04.09.09
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
04.09.09
IT 60101: Lecture #17
9
Bubble Sort: Summary
Case
Comparisons
Movement
Memory
Remark
Case1
Input list is in sortedorder
Case2
Input list is sorted inreversed order
Case3
Input list is in randomorder
Run time, T(n)
Complexity
Remark
Best case
Worst case
Average case
04.09.09
IT 60101: Lecture #17
10
Bubble Sort: Refinements
Funnel sort
Cocktail sort
04.09.09
IT 60101: Lecture #17
11
Cocktail Sort
04.09.09
IT 60101: Lecture #17
12
Sorting by Exchange: Shell Sort
Sorting methods based on comparison
Comparisons and hence movements of data take place betweenadjacent entries only
This leads to a number of redundant comparisons and datamovements
A mechanism should be followed with which the comparisonscan take in long leaps instead of short
Donald L. Shell (1959)
Use increments:
04.09.09
IT 60101: Lecture #17
13
Shell Sort: Illustration
04.09.09
IT 60101: Lecture #17
14
Shell Sort: Illustration
04.09.09
IT 60101: Lecture #17
15
Shell Sort: Illustration
04.09.09
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
04.09.09
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 inputlist
Increments: 2t -1, …, 15, 7, 3, 1
Time complexity :                 (worst)                        (average)
O(n5/3)
04.09.09
IT 60101: Lecture #17
18
Increments in Shell Sort
3: ht  = (3t -1)/2 for 1≤ t ≤ l Where l is chosen as thesmallest integer such that             , n being the size of theinput list
Worst case time complexity is O(n3/2  )
Many more……….
04.09.09
IT 60101: Lecture #17
19
Complexity Analysis of Shell Sort
Case
Comparisons
Movement
Memory
Remark
Case 1
Input list is in sortedorder
Case 2
Input list is sorted inreverse order
Case 3
Input list is in randomorder
Run time, T(n)
Complexity
Remark
Best case
Worst case
Average case
04.09.09
IT 60101: Lecture #17
20
Sorting by Exchange: Quick Sort
Divide-and-Conquer
04.09.09
IT 60101: Lecture #17
21
Divide-and-Conquer in Quick Sort
04.09.09
IT 60101: Lecture #17
22
Partition Method in Quick Sort
04.09.09
IT 60101: Lecture #17
23
Partition Method in Quick Sort
04.09.09
IT 60101: Lecture #17
24
Partition Method: Illustration
04.09.09
IT 60101: Lecture #17
25
Partition Method: Illustration
04.09.09
IT 60101: Lecture #17
26
Partition Method: Illustration
04.09.09
IT 60101: Lecture #17
27
Partition Method: Illustration
04.09.09
IT 60101: Lecture #17
28
Partition Method: Illustration
04.09.09
IT 60101: Lecture #17
29
Quick Sort: Illustration
04.09.09
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) representsthe 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 ≤ nlnr < n
04.09.09
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(nn-1 C(n-1), with C(1) C(0) 0
M(n) = 0
04.09.09
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(nn-1 C(n-1), with C(1) C(0) 0
04.09.09
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
04.09.09
IT 60101: Lecture #17
34
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
35
Complexity Analysis: Summary
Case
Comparisons
Movement
Memory
Remark
Case 1
Input list is insorted order
Case 2
Input list issorted inreversed order
Case 3
Input list is inrandom order
Run time, T(n)
Complexity
Remark
Worst case
Worst case
Best/averagecase
04.09.09
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
04.09.09
IT 60101: Lecture #17
37
Lomuto’s quick sort
04.09.09
IT 60101: Lecture #17
38
Summary: Sorting by Exchange
Algorithm
Best case
Worst case
Average case
Bubble sort
Shell sort
Quick sort
04.09.09
IT 60101: Lecture #17
39
Summary: Sorting by Exchange
04.09.09
IT 60101: Lecture #17
40
Summary: Sorting by Exchange
04.09.09
IT 60101: Lecture #17
41
Summary: Sorting by Exchange