# Sorting Algorithms & Algorithm Design Techniques (PART 1)

June 16, 2020

## Transcript

PART 1
2. ### Sorting problem: Input: A sequence of numbers n {a1 ,

a2 , . . . , an } Output: A permutation (reordering ) of the input sequence such that {a′ 1 , a′ 2 , . . . , a′ n } a′ 1 ≤ a′ 2 ≤ a′ 3 ≤ . . . ≤ a′ n
3. ### Bubble Sort Brute Force Approach 1. Look at adjacent elements

2. Compare and swap 3. Apply multiple times until the sequence is sorted
4. ### Bubble Sort Brute Force Approach BUBBLE-SORT( ): 1. for up

to : 2. for up to 3. if : 4. swap( , ) A i = 0 n − 1 j = 0 n − i − 1 : A[j] > A[j + 1] A[j] A[j + 1]
5. ### Bubble Sort Brute Force Approach BUBBLE-SORT( ): 1. for up

to : 2. for up to 3. if : 4. swap( , ) A i = 0 n − 1 j = 0 n − i − 1 : A[j] > A[j + 1] A[j] A[j + 1] Cost c1 c2 c3 c4
6. ### Bubble Sort Brute Force Approach BUBBLE-SORT( ): 1. for up

to : 2. for up to 3. if : 4. swap( , ) A i = 0 n − 1 j = 0 n − i − 1 : A[j] > A[j + 1] A[j] A[j + 1] Cost c1 c2 c3 c4 Times n ? ? ?
7. ### Bubble Sort Brute Force Approach BUBBLE-SORT( ): 1. for up

to : 2. for up to 3. if : 4. swap( , ) A i = 0 n − 1 j = 0 n − i − 1 : A[j] > A[j + 1] A[j] A[j + 1] Cost c1 c2 c3 c4 Times n n + n − 1 + n − 2 + . . . + 1 n + n − 1 + n − 2 + . . . + 1 n + n − 1 + n − 2 + . . . + 1
8. ### Bubble Sort Brute Force Approach 1 + 1 + 3

+ 4 + . . . = n ∑ k=1 k = n(n + 1) 2
9. ### Bubble Sort Brute Force Approach n ∑ k=1 k =

n(n + 1) 2 = 1 2 (n2 + n)

11. ### Insertion Sort Incremental approach 1. Reduce problem from to a

single element problem 2. Set base case 3. Solve for a single element 4. Apply solution for all elements. n
12. ### Insertion Sort Incremental approach 1. Reduce problem from to a

single element problem: Insert a single element into a SORTED list 2. Set base case: Empty sequence or 1-element sequence 3. Solve for a single element: Insert element into correct position 4. Apply solution for all elements: Repeat. n
13. ### Insertion Sort Incremental approach Input: [9,1,3,8,2,5] Sorted Unsorted [] [9,1,3,8,2,5]

 [1,3,8,2,5] [1,9] [3,8,2,5] [1,3,9] [8,2,5] [1,3,8,9] [2,5] [1,2,3,8,9]  [1,2,3,5,8,9] []

15. ### INSERTION-SORT( ): 1. for up to : 2. 3. 4.

while and 5. 6. 7. A i = 1 n − 1 key ← A[i] j = i − 1 j > 0 A[j] > key A[j + 1] = A[j] j = j − 1 A[j + 1] = key Insertion Sort Incremental approach
16. ### INSERTION-SORT( ): 1. for up to : 2. 3. 4.

while and 5. 6. 7. A i = 1 n − 1 key ← A[i] j = i − 1 j > 0 A[j] > key A[j + 1] = A[j] j = j − 1 A[j + 1] = key Insertion Sort Incremental approach n 1 + 2 + . . . + n
17. ### Insertion Sort Incremental approach 1 + 2 + . .

. + n = n ∑ k=1 k = n(n + 1) 2 = 1 2 (n2 + n)

19. ### Merge sort Divide & Conquer approach • Divide the problem

into sub-problems. • Conquer the sub-problems by solving them recursively. If the sub-problems are small enough, solve them in a straight forward manner. • Combine the solutions to the subproblems into the solution for the original problem.
20. ### Merge sort Divide & Conquer approach • Divide: Divide the

array into two subarrays of elements each. • Conquer: Sort the two subarrays recursively using merge sort. • Combine: Merge the two sorted arrays to produce the answer n/2
21. ### Merge sort Divide & Conquer approach • Divide: Divide the

array into two subarrays of elements each. • Conquer: Sort the two subarrays recursively using merge sort. • Combine: Merge the two sorted arrays to produce the answer n/2
22. ### Merge sort Divide & Conquer approach Assume a MERGE(A, p,

q, r) procedure with time complexity exists. Where is an array, and and are indices such that , and the sub-arrays and are sorted. Θ(n) A p, q, r p ≤ q < r A[p, . . , q] A[q + 1,...,r]
23. ### MERGE-SORT( ): 1. if : 2. 3. MERGE-SORT( ) 4.

MERGE-SORT( ) 5. MERGE( ) A, p, r p < r q = ⌊(p + r)/2⌋ A, p, q A, q + 1,r A, p, q, r Merge sort Divide & Conquer approach
24. ### MERGE-SORT( ): 1. if : 2. 3. MERGE-SORT( ) 4.

MERGE-SORT( ) 5. MERGE( ) A, p, r p < r q = ⌊(p + r)/2⌋ A, p, q A, q + 1,r A, p, q, r Merge sort Divide & Conquer approach c1 c2 ? ? Θ(p + r)
25. ### MERGE-SORT( ): 1. if : 2. 3. MERGE-SORT( ) 4.

MERGE-SORT( ) 5. MERGE( ) A, p, r p < r q = ⌊(p + r)/2⌋ A, p, q A, q + 1,r A, p, q, r Merge sort Divide & Conquer approach c1 c2 ? ? Θ(n)
26. ### Merge sort Divide & Conquer approach Source: Thomas H. Cormen,

Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. 2009. Introduction to Algorithms, Third Edition (3rd. ed.). The MIT Press.
27. ### Merge sort Divide & Conquer approach Source: Thomas H. Cormen,

Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. 2009. Introduction to Algorithms, Third Edition (3rd. ed.). The MIT Press.

29. ### Divide & Conquer Recurrences { Θ(1) if n ≤ c

aT(n/b) + D(n) + C(n) otherwise} T(n) =
30. ### Recurrences { Θ(1) if n ≤ c aT(n/b) + D(n)

+ C(n) otherwise} T(n) = • is the number of sub-problems. • is the size of each sub problem. • is the tame it takes to solve one sub-problem. a n b T(n/b) Divide & Conquer • time to divide. • time to combine. D(n) C(n)
31. ### Recurrence of divide & conquer { Θ(1) if n ≤

1 2T(n/2) + Θ(n) n > 1 } T(n) = • We always divide array in 2 sub arrays • is the size of each sub problem. • is the tame it takes to solve one sub-problem. a = 2 n 2 T(n/2) Divide & Conquer • time to divide. • time to combine. D(n) = Θ(1) = C(n) = Θ(n) =