2. Contents
1. Insertion Sort
Discussion on insertion sort
2. Insertion Sort Analysis
Analyzing time complexity of Insertion sort
3. Introduction to Divide and Conquer Approach
The basics of divide and conquer approach will be addressed.
4. Merge sort basic
Elementary discussion on merge sort.
6. Insertion Sort Pseudocode
Insertion Sort 2/10
Algorithm 3 pseudocode for Insertion sort
InsertionSort(A[ ])
1: for j = 2 to A.length do
2: key = A[j]
7. Insertion Sort Pseudocode
Insertion Sort 2/10
Algorithm 4 pseudocode for Insertion sort
InsertionSort(A[ ])
1: for j = 2 to A.length do
2: key = A[j]
3: i = j − 1
8. Insertion Sort Pseudocode
Insertion Sort 2/10
Algorithm 5 pseudocode for Insertion sort
InsertionSort(A[ ])
1: for j = 2 to A.length do
2: key = A[j]
3: i = j − 1
4: while i > 0 and A[i] > key do
9. Insertion Sort Pseudocode
Insertion Sort 2/10
Algorithm 6 pseudocode for Insertion sort
InsertionSort(A[ ])
1: for j = 2 to A.length do
2: key = A[j]
3: i = j − 1
4: while i > 0 and A[i] > key do
5: A[i + 1] = A[i]
10. Insertion Sort Pseudocode
Insertion Sort 2/10
Algorithm 7 pseudocode for Insertion sort
InsertionSort(A[ ])
1: for j = 2 to A.length do
2: key = A[j]
3: i = j − 1
4: while i > 0 and A[i] > key do
5: A[i + 1] = A[i]
6: i = i − 1
7: end while
11. Insertion Sort Pseudocode
Insertion Sort 2/10
Algorithm 8 pseudocode for Insertion sort
InsertionSort(A[ ])
1: for j = 2 to A.length do
2: key = A[j]
3: i = j − 1
4: while i > 0 and A[i] > key do
5: A[i + 1] = A[i]
6: i = i − 1
7: end while
8: A[i + 1] = key
9: end for
15. Divide and Conquer Approach
Divide and Conquer Approach 6/10
The divide-and-conquer approach breaks the problem into several
subproblems that are similar to the original problem but smaller in
size, solve the subproblems recursively, and then combine these
solutions to create a solution to the original problem.
The divide-and-conquer paradigm involves three steps at each level
of the recursion:
Divide the problem into a number of subproblems that are smaller
instances of the same problem.
Conquer the subproblems by solving them recursively. If the
subproblem sizes are small enough, however, just solve the
subproblems in a straightforward manner.
Combine the solutions to the subproblems into the solution for the
original problem. The merge sort algorithm closely follows the
divide-and-conquer paradigm.
17. Merge sort
Introduction to Merge Sort 8/10
Algorithm 9 Pseudocode for Merge Sort
MergeSort (A[ ], p, q, r)
1: n1 = q − p + 1; n2 = r − q
2: let L[1..n1 + 1] and R[1..n2 + 1] be new arrays
3: for i = 1 to n1 do
4: L[i] = A[p + i − 1]
5: end for
6: for j = 1 to n2 do
7: R[j] = A[q + j]
8: end for
9: L[n1 + 1] = ∞; R[n2 + 1] = ∞; i = 1; j = 1
10: for k = p to r do
11: if L[i] ≤ R[j] then
12: A[k] = L[i]; i = i + 1
13: else
14: A[k] = R[j]; j = j + 1
15: end if
16: end for