7 Sorting - Algorithms (Series Lecture)
7 Sorting - Algorithms (Series Lecture)
Sorting
Given a set (container) of n elements
E.g. array, set of words, etc.
Suppose there is an order relation that
can be set across the elements
Goal Arrange the elements in ascending
order
Start 1 23 2 56 9 8 10 100
End 1 2 8 9 10 23 56 100
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Largest
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Largest
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Largest
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Largest
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Largest
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
DONE!
Comparison
Data Movement
Sorted
At glance in Reverse Order
23 78 45 8 32 56 Original List
8 78 45 23 32 56 After pass 1
8 23 45 78 32 56 After pass 2
After pass 3
8 23 32 78 45 56
8 23 32 45 78 56 After pass 4
After pass 5
8 23 32 45 56 78
Algorithm
38 45
60 60
66 45
66 79 47 13 74 36 21 94 22 57 16 29 81
13
13
T (n) c1n c2 (n 1) c4 (n 1) c5 t j c6 t j 1 c7 t j 1 c8 (n 1)
n n n
j 2 j 2 j 2
74
Best Case Analysis
The array is already sorted while i > 0 and A[i] > key
A[i] key upon the first time the while loop test is run
(when i = j -1)
tj = 1
= an + b = (n)
T (n) c1n c2 (n 1) c4 (n 1) c5 t j c6 t j 1 c7 t j 1 c8 (n 1)
n n n
j 2 j 2 j 2
75
Worst Case Analysis
The array is in reverse sorted orderwhile i > 0 and A[i] > key
Always A[i] > key in while loop test
Have to compare key with all elements to the left of the j-th
position compare with j-1 elements tj = j
n
n(n 1) n
n(n 1) n
n(n 1)
using
j 1
j
2
j
j 2 2
1 ( j 1)
j 2 2
we have:
n(n 1) n( n 1) n(n 1)
T (n ) c1n c2 (n 1) c4 (n 1) c5 1 c6 c7 c8 (n 1)
2 2 2
an 2 bn c a quadratic function of n
j 2 j 2 j 2 76
Comparisons and Exchanges in
Insertion Sort
c6
n
do A[i + 1] A[i] j 2
(t j 1)
ii1
n2/2 exchanges c7
n
j 2
(t j 1)
A[i + 1] key
c8 n-1
77
Insertion Sort - Summary
Advantages
Good running time for almost sorted
arrays (n)
Disadvantages
(n2) running time in worst and average
case
n2/2 comparisons and exchanges
78
Divide-and-Conquer
Divide and Conquer is a method of algorithm design that
has created such efficient algorithms as Merge Sort.
In terms or algorithms, this method has three distinct
steps:
Divide: If the input size is too large to deal with in a
straightforward manner, divide the data into two or
more disjoint subsets.
Recur: Use divide and conquer to solve the
Result:
Merging (cont.)
X: Y:
3 10 23 54 5 25 75
Result:
1
Merging (cont.)
X: Y:
10 23 54 5 25 75
Result:
1 3
Merging (cont.)
X: Y:
10 23 54 25 75
Result:
1 3 5
Merging (cont.)
X: Y:
23 54 25 75
Result:
1 3 5 10
Merging (cont.)
X: Y:
54 25 75
Result:
1 3 5 10 23
Merging (cont.)
X: Y:
54 75
Result:
1 3 5 10 23 25
Merging (cont.)
X: Y:
75
Result:
1 3 5 10 23 25 54
Execution Example
Partition
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6
7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6
77 22 99 44 33 88 66 11
Merge Sort 88
Execution Example (cont.)
Recursive call, partition
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6
77 22 99 44 33 88 66 11
Merge Sort 89
Execution Example (cont.)
Recursive call, partition
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
77 22 99 44 33 88 66 11
Merge Sort 90
Execution Example (cont.)
Recursive call, base case
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
77 22 99 44 33 88 66 11
Merge Sort 91
Execution Example (cont.)
Recursive call, base case
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
77 22 99 44 33 88 66 11
Merge Sort 92
Execution Example (cont.)
Merge
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
77 22 99 44 33 88 66 11
Merge Sort 93
Execution Example (cont.)
Recursive call, , base case, merge
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
77 22 99 44 33 88 66 11
Merge Sort 94
Execution Example (cont.)
Merge
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
77 22 99 44 33 88 66 11
Merge Sort 95
Execution Example (cont.)
Recursive call, , merge, merge
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 6 8
722 7 9 4 4 9 3 8 3 8 6 1 1 6
77 22 99 44 33 88 66 11
Merge Sort 96
Execution Example (cont.)
Merge
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 6 8
722 7 9 4 4 9 3 8 3 8 6 1 1 6
77 22 99 44 33 88 66 11
Merge Sort 97
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 2i1 recursive calls
Thus, the total running time of merge-sort is O(n log n)
1 2 n/2
i 2i n/2i
Merge Sort 98
Merge-Sort Algorithm
Merge-Sort Algorithm
Mergesort - Analysis
Levels of recursive calls to mergesort, given an array of eight items
Mergesort - Analysis
2m level 0 : 1 merge (size 2m-1)
2m-1 2m-1
level 1 : 2 merges (size 2m-2)
= m*2m
= m*2m 2m 1
Using m = log n
= n * log2n n 1
O (n * log2n )
Mergesort Analysis
Mergesort is extremely efficient algorithm with respect to
time.
Both worst case and average cases are O (n * log2n )
7 4 9 6 2 2 4 6 7 9
4 2 2 4 7 9 7 9
22 99
7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6
22 9 4 4 9 33 88
99 44
2 4 3 1 2 4 7 9 3 8 6 1 1 3 8 6
22 9 4 4 9 33 88
99 44
2 4 3 1 2 4 7 3 8 6 1 1 3 8 6
11 9 4 4 9 33 88
99 44
2 4 3 1 1 2 3 4 3 8 6 1 1 3 8 6
11 4 3 3 4 33 88
99 44
2 4 3 1 1 2 3 4 7 9 7 1 1 3 8 6
11 4 3 3 4 88 99
99 44
2 4 3 1 1 2 3 4 7 9 7 1 1 3 8 6
11 4 3 3 4 88 99
99 44
2 4 3 1 1 2 3 4 7 9 7 17 7 9
11 4 3 3 4 88 99
99 44
1 n1
n1 1
Md.Shamsujjoha Quick-Sort 116