Insertion and merge sort

68
INSERTION SORT

Transcript of Insertion and merge sort

Page 1: Insertion and merge sort

INSERTION SORT

Page 2: Insertion and merge sort
Page 3: Insertion and merge 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

Page 4: Insertion and merge sort

Waste slide

Page 5: Insertion and merge sort

How Insertion Sorting Works

Page 6: Insertion and merge sort

Insertion Sort

9 72 5 1 4 3 6

Example :

Page 7: Insertion and merge sort

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.

Page 8: Insertion and merge sort

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.

Page 9: Insertion and merge sort

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.

Page 10: Insertion and merge sort

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

Page 11: Insertion and merge sort

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

Page 12: Insertion and merge sort

Insertion Sort

9 72 5 1 4 3 6

Page 13: Insertion and merge sort

Insertion Sort

9 72 5 1 4 3 6

Page 14: Insertion and merge sort

Insertion Sort

9 72 5 1 4 3 6

Item to position

Page 15: Insertion and merge sort

Insertion Sort

9

7

2 5 1 4 3 67

compare

Page 16: Insertion and merge sort

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

Page 17: Insertion and merge sort

Insertion Sort

972 5 1 4 3 6

Page 18: Insertion and merge sort

Insertion Sort

972 5 1 4 3 6

Page 19: Insertion and merge sort

Insertion Sort

972 5 1 4 3 6

Item to position

Page 20: Insertion and merge sort

Insertion Sort

972

5

1 4 3 65

compare

Page 21: Insertion and merge sort

Insertion Sort

972

5

1 4 3 69

compare

Page 22: Insertion and merge sort

Insertion Sort

972

5

1 4 3 67

compare

belongs here

Page 23: Insertion and merge sort

Insertion Sort

972 5 1 4 3 6

Page 24: Insertion and merge sort

Insertion Sort

972 5 1 4 3 6

Page 25: Insertion and merge sort

Insertion Sort

972 5 1 4 3 6

Item to position

Page 26: Insertion and merge sort

Insertion Sort

972 5

1

4 3 61

compare

Page 27: Insertion and merge sort

Insertion Sort

972 5

1

4 3 69

compare

Page 28: Insertion and merge sort

Insertion Sort

972 5

1

4 3 67

compare

Page 29: Insertion and merge sort

Insertion Sort

972 5

1

4 3 65

compare

Page 30: Insertion and merge sort

Insertion Sort

972 5

1

4 3 62

belongs here

Page 31: Insertion and merge sort

Insertion Sort

972 51 4 3 6

Page 32: Insertion and merge sort

Insertion Sort

972 51 4 3 6

Page 33: Insertion and merge sort

Insertion Sort

972 51 4 3 6

Item to position

Page 34: Insertion and merge sort

Insertion Sort

972 51

4

3 64

compare

Page 35: Insertion and merge sort

Insertion Sort

972 51

4

3 69

compare

Page 36: Insertion and merge sort

Insertion Sort

972 51

4

3 67

compare

Page 37: Insertion and merge sort

Insertion Sort

972 51

4

3 6

belongs here

5

compare

Page 38: Insertion and merge sort

Insertion Sort

972 51 4 3 6

Page 39: Insertion and merge sort

Insertion Sort

972 51 4 3 6

Page 40: Insertion and merge sort

Insertion Sort

972 51 4 3 6

Item to position

Page 41: Insertion and merge sort

Insertion Sort

972 51 4

3

63

compare

Page 42: Insertion and merge sort

Insertion Sort

972 51 4

3

69

compare

Page 43: Insertion and merge sort

Insertion Sort

972 51 4

3

67

compare

Page 44: Insertion and merge sort

Insertion Sort

972 51 4

3

65

compare

Page 45: Insertion and merge sort

Insertion Sort

972 51 4

3

64

compare

belongs here

Page 46: Insertion and merge sort

Insertion Sort

972 51 43 6

Page 47: Insertion and merge sort

Insertion Sort

972 51 43 6

Page 48: Insertion and merge sort

Insertion Sort

972 51 43 6

Item to position

Page 49: Insertion and merge sort

Insertion Sort

972 51 43

6

6

compare

Page 50: Insertion and merge sort

Insertion Sort

972 51 43

6

9

compare

Page 51: Insertion and merge sort

Insertion Sort

972 51 43

6

7

compare

belongs here

Page 52: Insertion and merge sort

Insertion Sort

972 51 43 6

Page 53: Insertion and merge sort

Insertion Sort

972 51 43 6

SORTED!

Page 54: Insertion and merge sort

Merge Sort

Page 55: Insertion and 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.

Page 56: Insertion and merge sort

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

Page 57: Insertion and merge sort

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

Page 58: Insertion and merge sort

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}

Page 59: Insertion and merge sort

Merge Sort (cont.)• Example Explanation ::

99 6 86 15 58 35 86 4 0

Page 60: Insertion and merge sort

Merge Sort (cont.)• Example Explanation : :

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

Page 61: Insertion and merge sort

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

Page 62: Insertion and merge sort

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

Page 63: Insertion and merge sort

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

Page 64: Insertion and merge sort

Merge Sort (cont.)• Example Explanation :

99 6 86 15 58 35 86 0 4

4 0Merging

Page 65: Insertion and merge sort

Merge Sort (cont.)• Example Explanation :

99 6 86 15 58 35 86 0 4

Merging

15 866 99 35 58 0 4 86

Page 66: Insertion and merge sort

Merge Sort (cont.)• Example Explanation :

Merging

15 866 99 58 35 0 4 86

6 15 86 99 0 4 35 58 86

Page 67: Insertion and merge sort

Merge Sort (cont.)• Example Explanation :

Merging

6 15 86 99 0 4 35 58 86

0 4 6 15 35 58 86 86 99

Page 68: Insertion and merge sort

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 :