04 CS351 Sorting Algorithms
04 CS351 Sorting Algorithms
Analysis of Algorithms
Sorting Algorithms
• Approach (techniques):
– Selection Sort - Incremental Technique (simple)
– Insertion Sort - Incremental Technique (simple)
– Merge Sort - Divide and conquer Technique
Sorting Algorithms:
• Insertion Sort
• Selection Sort
• Merge Sort
Insertion Sort
1. Loop:
Shift up all list elements > (X) 0 1 2 3 4
2. Insert (X) 11 13 15 17 19 ...... X=16
3. n=n+1 0 1 2 3 4 5
8 13 15 16 17 19
Performance:
0 1 2 3 4
The insertion costs:
11 13 15 17 19 ...... X=25
Worst case (all elements > X) ➔O(n)
Best case (larger list element <= X) ➔ 0 1 2 3 4 5
O(1) 11 13 15 17 19 25
Insertion Sort
input array
5 2 4 6 1 3
At each iteration, the array is divided in two sub-arrays:
5 2 4 6 1 3
sorted unsorted
sorted unsorted
Insertion Sort
Input list ➔ 5 2 4 6 1 3
Iteration Resulted
1 array
Iteration
5
Iteration
2
Iteration
Iteration 4
3
Insertion Sort
k
0 1 | 2 3 4 5
Iteration 1
k
0 1 2 | 3 4 5
Iteration 2
k
0 1 2 3 | 4 5
Iteration 3
Iteration 4 0 1 2 3
k4 | 5
Iteration 5 k
0 1 2 3 4 5
At the end of each pass k through the algorithm given above, the entries
in the sublist {a0; a1; … ; ak } are first k entries of the original list, but in
sorted order.
The Insertion-Sort Algorithm
key
key
K=5
Insertion Sort performance
At each step of insertion sort:
• The outer loop (K) will be executed
(n-1) times.
• Each time K is incremented (i.e each pass):
• list A[0, …, k-1] is already sorted.
• A[k] is inserted to the part of the list
A[0, …, k].
• The insertion requires [ 0 to K-1] or k
steps.
• In the worst case, the insertion takes k
steps.
5 2 4 6 1 3 K= 0
0 1 2 3 4 5
5 2 4 6 1 3 J=3 K= 1
0 1 2 3 4 5
Performance:
5 2 4 6 1 3 J=4 K= 4
The Algorithm costs:
➔O(n)
0 1 2 3 4 5
in all cases
5 2 4 6 1 3 J=5 K= 4
Swap the first item with the first smallest
item in the list 0 1 2 3 4 5
5 2 4 6 1 3 K= 0
0 1 2 3 4 5
1. min element is at k= 0 5 2 4 6 1 3 J =1 K= 1
2. Loop: j=1→ n-1
If A[j] < A[k] then k=j 0 1 2 3 4 5
3. Return k as location of min in 5 2 4 6 1 3 J=2 K= 1
A[0→n-1] 0 1 2 3 4 5
4. If (k != 0) 5 2 4 6 1 3 J=3 K= 1
Swap (A[0],A[K])
0 1 2 3 4 5
5 2 4 6 1 3 J=4 K= 4
Performance:
Swap() cost constant time 0 1 2 3 4 5
The algorithm costs: 5 2 4 6 1 3 J=5 K= 4
➔O(n)
in all cases 0 1 2 3 4 5
1 2 4 6 5 3 Swap (A[0],A[4])
Selection Sort
No need for
1st smallest 6th iteration
swap
• The outer loop (i) : represents the search space for the min elements
[1→n-1] , [2→n-1] , [3→n-1] ..... [n-2→ n-1]
size of the search space: n-1 n-2 n-3 ...... 1
to find 1st smallest 2nd smallest 3rd smallest (n-2) smallest
• Inner loop(j) : find the location of the min in the specified space
Selection-Sort
0 1 2 3 4 5 0 1 2 3 4 5
Iteration(1) 5 2 4 6 1 3 Iteration(4) 1 2 3 6 5 4
i= 0 , j(1→5) i= 3 , j(4→5)
K=4 swap K=5 swap
1 2 3 4 5 6
1 2 4 6 5 3
0 1 2 3 4 5 0 1 2 3 4 5
Iteration(2) Iteration(5) 1 2 3 4 5 6
1 2 4 6 5 3
i= 1 , j(2→5) i= 4 , j(5→5)
K=1 no swap K=4 no swap
1 2 4 6 5 3 1 2 3 4 5 6
0 1 2 3 4 5
Iteration(3) 1 2 4 6 5 3 0 1 2 3 4 5
i= 2 , j(3→5) The resulted
K=5 swap list after 5 1 2 3 4 5 6
1 2 3 6 5 4 iterations
Selection Sort Algorithm
Input: An array A[1..n] of n elements.
Output: A[1..n] sorted in nondecreasing order.
1. for i 0 to n - 2
2. k i
3. for j i + 1 to n-1 {Find the i th smallest element.}
4. if A[j] < A[k] then k j
5. end for The Worst case
6. if k i then swap( A[i] , A[k]) Analysis
7. end for
⚫ The outer loop iterates n-1 times.
⚫ The inner loop iterates n-i times
⚫ There is a comparison in each iteration.
⚫ The sort method executes swap() once on each iteration of its outer
loop
⚫ The total number of swap is O(n)
𝑛 − 𝑖 = 𝑛 − 1 + … . +3 + 2 + 1 =
𝑛 𝑛−1
⇒ 𝑂(𝑛2 )
The total cost of the
𝑖=1
2 selection sort is :
O(n2)
Selection Sort Algorithm
Input: An array A[1..n] of n elements.
Output: A[1..n] sorted in nondecreasing order. 11 13 15 17 19
1. for i 0 to n - 2
2. k i The Best case
3. for j i + 1 to n-1 {Find the i th smallest element.} Analysis
4. if A[j] < A[k] then k j List is already sorted
5. end for
6. if k i then swap( A[i] , A[k])
The total cost of the
7. end for
selection sort is :
⚫ The outer loop iterates n-1 times.
O(n2)
⚫ The inner loop iterates n-i times
⚫ There is a comparison in each iteration.
⚫ The sort method executes swap() once on each iteration of its outer
loop
⚫ The total number of swap is O(1)
midpoint
MERGE‐SORT (A, p, r)
if p < r //Check for base case
1. then mid ← [( p + r) / 2] //Divide
2. MERGE‐SORT (A,p, mid) //Conquer
3. MERGE‐SORT (A, mid+1, r) //Conquer
4. MERGE (A, p, mid, r) //Combine
left mid+1
right
p mid r
p mid mid+1 r
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
Initial 1 2 3 4 5 6 7 8
unsorted 5 4 1 2
list
2 7 3 6
2 5 4 7 1 3 2 6
1 2 3 4 5 6 7 8
2 4 5 7 1 2 3 6
p q r
....... 5 6 7 8 9 10 11 13 .......
A ....... 2 5 16 20 3 7 11 33 .......
L 2 5 16 20 ∞ R 3 7 11 33 ∞
1 2 3 4 5 1 2 3 4 5
Merge( A, 5,8,13)
p q r
....... 5 6 7 8 9 10 11 13 .......
A ....... 2 5 16 20 3 7 11 33 .......
L 2 5 16 20 ∞ R 3 7 11 33 ∞
1 2 3 4 5 1 2 3 4 5
i j
A ....... .......
....... 5 6 7 8 9 10 11 13 .......
k
Merge( A, 5,8,13)
p q r
....... 5 6 7 8 9 10 11 13 .......
A ....... 2 5 16 20 3 7 11 33 .......
L 2 5 16 20 ∞ R 3 7 11 33 ∞
1 2 3 4 5 1 2 3 4 5
i j
A ....... 2 .......
....... 5 6 7 8 9 10 11 13 .......
k
Merge( A, 5,8,13)
p q r
....... 5 6 7 8 9 10 11 13 .......
A ....... 2 5 16 20 3 7 11 33 .......
L 2 5 16 20 ∞ R 3 7 11 33 ∞
1 2 3 4 5 1 2 3 4 5
i j
A ....... 2 3 .......
....... 5 6 7 8 9 10 11 13 .......
k
Merge( A, 5,8,13)
p q r
....... 5 6 7 8 9 10 11 13 .......
A ....... 2 5 16 20 3 7 11 33 .......
L 2 5 16 20 ∞ R 3 7 11 33 ∞
1 2 3 4 5 1 2 3 4 5
i j
A ....... 2 3 5 .......
....... 5 6 7 8 9 10 11 13 .......
k
Merge( A, 5,8,13)
p q r
....... 5 6 7 8 9 10 11 13 .......
A ....... 2 5 16 20 3 7 11 33 .......
L 2 5 16 20 ∞ R 3 7 11 33 ∞
1 2 3 4 5 1 2 3 4 5
i j
A ....... 2 3 5 7 .......
....... 5 6 7 8 9 10 11 13 .......
k
Merge( A, 5,8,13)
p q r
....... 5 6 7 8 9 10 11 13 .......
A ....... 2 5 16 20 3 7 11 33 .......
L 2 5 16 20 ∞ R 3 7 11 33 ∞
1 2 3 4 5 1 2 3 4 5
i j
A ....... 2 3 5 7 11 .......
....... 5 6 7 8 9 10 11 13 .......
k
Merge( A, 5,8,13)
p q r
....... 5 6 7 8 9 10 11 13 .......
A ....... 2 5 16 20 3 7 11 33 .......
L 2 5 16 20 ∞ R 3 7 11 33 ∞
1 2 3 4 5 1 2 3 4 5
i j
A ....... 2 3 5 7 11 16 20 .......
....... 5 6 7 8 9 10 11 13 .......
k
Merge( A, 5,8,13)
p q r
....... 5 6 7 8 9 10 11 13 .......
A ....... 2 5 16 20 3 7 11 33 .......
L 2 5 16 20 ∞ R 3 7 11 33 ∞
1 2 3 4 5 1 2 3 4 5
i j
A ....... 2 3 5 7 11 16 20 33 .......
....... 5 6 7 8 9 10 11 13 .......
k
Merging performance
Ɵ(n1 + n2)=Ɵ(n)
Ɵ(n1 + n2)=Ɵ(n)
9 3 2 7 6 1 18 4
9 3 2 7 6 1 18 4
9 3 2 7 6 1 18 4
3 9 2 7 1 6 4 18
2 3 7 9 1 4 6 18
1 2 3 4 6 7 9 18
MGS(A,1,8) 5 2 4 7 1 3 2 6
MGS(A,1,4) MGS(A,5,8)
5 2 4 7 1 3 2 6
MGS(A,1,2) MGS(A,3,4) MGS(A,5,7) MGS(A,7,8)
5 2 4 7 1 3 2 6
MGS(A,1,1) MGS(A,2,2) MGS(A,3,3) MGS(A,4,4) MGS(A,5,5) MGS(A,6,6) MGS(A,7,7) MGS(A,8,8)
5 2 4 7 1 3 2 6
2 5 4 7 1 3 2 6
merge(A,1,1,2) merge(A,3,3,4) merge(A,5,5,6) merge(A,7,7,8)
2 4 5 7 1 2 3 6
merge(A,1,2,4) merge(A,5,6,8)
1 2 2 3 4 5 6 7
merge(A,1,4,8)
Analyzing merge sort
MERGE‐SORT (A, p, r) T(n)=?
if p < r //Check for base case
1. then mid ← [( p + r) / 2] //Divide 1
2. MERGE‐SORT (A,p, mid) //Conquer T(n/2)
3. MERGE‐SORT (A, mid+1, r) //Conquer T(n/2)
4. MERGE (A, p, mid, r) //Combine Ɵ(n)
h = logn.
𝒏
𝟐𝒌 × 𝒄 = 𝐜𝐧
𝟐𝒌
𝒏
2𝐥𝐨𝐠𝐧 × 𝒄 = 𝐜𝐧
𝟐𝒍𝒐𝒈𝒏
𝑻𝒐𝒕𝒂𝒍 = 𝒄 𝒏𝒍𝒐𝒈𝒏
𝑻𝒐𝒕𝒂𝒍 𝒓𝒖𝒏𝒏𝒊𝒏𝒈 𝒕𝒊𝒎𝒆 𝒊𝒔 Ɵ(𝒏𝒍𝒐𝒈𝒏)
𝑻𝒐𝒕𝒂𝒍 𝒔𝒑𝒂𝒄𝒆 𝒊𝒔 Ɵ(𝒏) The complexity is tightly
bounded to (nlogn)
Thank you