3. Insertion Sorting
•It is a simple Sorting algorithm which sorts the array by shifting elements one
• by one. Following are some of the important characteristics of Insertion Sort.
It has one of the simplest implementation
It is efficient for smaller data sets, but very inefficient for larger lists.
Insertion Sort is adaptive, that means it reduces its total number of steps if
given a partially sorted list, hence it increases its efficiency.
It is better than Selection Sort and Bubble Sort algorithms.
Its space complexity is less, like Bubble Sorting, inerstion sort also requires a
single additional memory space.
It is Stable, as it does not change the relative order of elements with equal keys
7. Insertion Sort
9 72 5 1 4 3 6
Sorted
section
We start by dividing the array in a section section and an unsorted section.
We put the first element as the only element in the sorted section, and
the rest of the array is the unsorted section.
8. Insertion Sort
9 72 5 1 4 3 6
Sorted
section
Item to
position
The first element in the unsorted section is the next
element to be put into the correct position.
9. Insertion Sort
9 7
2
5 1 4 3 6
Item to
position
2
We copy the element to be placed into another
variable so it doesn’t get overwritten.
10. Insertion Sort
9 7
2
5 1 4 3 62
compare
If the previous position is more than the item being placed,
copy the value into the next position
11. Insertion Sort
9 7
2
5 1 4 3 69
If there are no more items in the sorted section to compare
with, the item to be placed must go at the front.
belongs here
16. Insertion Sort
9
7
2 5 1 4 3 6
Copied from previous
position
9
compare
If the item in the sorted section is less than the item to place,
the item to place goes after it in the array.
belongs here
55. Merge Sorting
The Merge Sort Algorithm is based on a simple operation known as Merging.
It is combining of two ordered arrays to make one larger ordered array.
It is mainly based on divide and conquer method paradigm.
Divide the problem into a no of sub problems and similar sub problems of smaller size.
Conquer the sub problems and solve them recursively.
Solve the problems in straight manner and combine the solutions of sub-problem.
Obtain the solution for sub-problem.
56. Merge Sort (cont.)
• Merge Sort Algorithm:
1. Merge Sort (Overview):
1. Split array into two halves
2. Sort the left half (recursively)
3. Sort the right half (recursively)
4. Merge the two sorted halves
57. Merge Sort (cont.)
• Merge Sort Algorithm (cont.):
2. Merging two halves:
1. Access the first item from both halves
2. While neither half is finished
1. Compare the current items of both
2. Copy smaller current item to the output
3. Access next item from that input half
3. Copy any remaining from first half to output
4. Copy any remaining from second half to output
58. Merge Sort (cont.)
• Merging:
The key to Merge Sort is merging two sorted lists into one, such that if you
have two lists X (x1x2…xm) and Y(y1y2…yn) the resulting list is
Z(z1z2…zm+n)
Example:
L1 = { 99,6,86,15 } L2 = { 58,35,86,4,0}
merge(L1, L2) = {0,4,6,15,35,58,86,86,99}