Insertion and merge sort
-
Upload
preetham-devisetty -
Category
Engineering
-
view
131 -
download
0
Transcript of Insertion and merge sort
INSERTION SORT
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
Waste slide
How Insertion Sorting Works
Insertion Sort
9 72 5 1 4 3 6
Example :
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.
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.
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.
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
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
Insertion Sort
9 72 5 1 4 3 6
Insertion Sort
9 72 5 1 4 3 6
Insertion Sort
9 72 5 1 4 3 6
Item to position
Insertion Sort
9
7
2 5 1 4 3 67
compare
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
Insertion Sort
972 5 1 4 3 6
Insertion Sort
972 5 1 4 3 6
Insertion Sort
972 5 1 4 3 6
Item to position
Insertion Sort
972
5
1 4 3 65
compare
Insertion Sort
972
5
1 4 3 69
compare
Insertion Sort
972
5
1 4 3 67
compare
belongs here
Insertion Sort
972 5 1 4 3 6
Insertion Sort
972 5 1 4 3 6
Insertion Sort
972 5 1 4 3 6
Item to position
Insertion Sort
972 5
1
4 3 61
compare
Insertion Sort
972 5
1
4 3 69
compare
Insertion Sort
972 5
1
4 3 67
compare
Insertion Sort
972 5
1
4 3 65
compare
Insertion Sort
972 5
1
4 3 62
belongs here
Insertion Sort
972 51 4 3 6
Insertion Sort
972 51 4 3 6
Insertion Sort
972 51 4 3 6
Item to position
Insertion Sort
972 51
4
3 64
compare
Insertion Sort
972 51
4
3 69
compare
Insertion Sort
972 51
4
3 67
compare
Insertion Sort
972 51
4
3 6
belongs here
5
compare
Insertion Sort
972 51 4 3 6
Insertion Sort
972 51 4 3 6
Insertion Sort
972 51 4 3 6
Item to position
Insertion Sort
972 51 4
3
63
compare
Insertion Sort
972 51 4
3
69
compare
Insertion Sort
972 51 4
3
67
compare
Insertion Sort
972 51 4
3
65
compare
Insertion Sort
972 51 4
3
64
compare
belongs here
Insertion Sort
972 51 43 6
Insertion Sort
972 51 43 6
Insertion Sort
972 51 43 6
Item to position
Insertion Sort
972 51 43
6
6
compare
Insertion Sort
972 51 43
6
9
compare
Insertion Sort
972 51 43
6
7
compare
belongs here
Insertion Sort
972 51 43 6
Insertion Sort
972 51 43 6
SORTED!
Merge Sort
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.
Merge Sort (cont.)• Merge Sort Algorithm:
1. Merge Sort (Overview):1. Split array into two halves2. Sort the left half (recursively)3. Sort the right half (recursively)4. Merge the two sorted halves
Merge Sort (cont.)• Merge Sort Algorithm (cont.):
2. Merging two halves:1. Access the first item from both halves2. While neither half is finished
1. Compare the current items of both2. Copy smaller current item to the output3. Access next item from that input half
3. Copy any remaining from first half to output4. Copy any remaining from second half to output
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}
Merge Sort (cont.)• Example Explanation ::
99 6 86 15 58 35 86 4 0
Merge Sort (cont.)• Example Explanation : :
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
Merge Sort (cont.)• Example Explanation :
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
86 1599 6 58 35 86 4 0
Merge Sort (cont.)• Example Explanation :
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
86 1599 6 58 35 86 4 0
99 6 86 15 58 35 86 4 0
Merge Sort (cont.)• Example Explanation :
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
86 1599 6 58 35 86 4 0
99 6 86 15 58 35 86 4 0
4 0
Merge Sort (cont.)• Example Explanation :
99 6 86 15 58 35 86 0 4
4 0Merging
Merge Sort (cont.)• Example Explanation :
99 6 86 15 58 35 86 0 4
Merging
15 866 99 35 58 0 4 86
Merge Sort (cont.)• Example Explanation :
Merging
15 866 99 58 35 0 4 86
6 15 86 99 0 4 35 58 86
Merge Sort (cont.)• Example Explanation :
Merging
6 15 86 99 0 4 35 58 86
0 4 6 15 35 58 86 86 99
Merge Sort (cont.)• Example Explanation :
99 6 86 15 58 35 86 4 0
0 4 6 15 35 58 86 86 99
Question :
Answer :