insertion sort

48
Insertion Sort 9 7 2 5 1 4 3 6

Upload: brett-duncan

Post on 18-Jul-2015

123 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Insertion Sort

Insertion Sort

9 72 5 1 4 3 6

Page 2: Insertion 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 3: Insertion 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 4: Insertion 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 5: Insertion 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 6: Insertion 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 7: Insertion Sort

Insertion Sort

9 72 5 1 4 3 6

Page 8: Insertion Sort

Insertion Sort

9 72 5 1 4 3 6

Page 9: Insertion Sort

Insertion Sort

9 72 5 1 4 3 6

Item to position

Page 10: Insertion Sort

Insertion Sort

9

7

2 5 1 4 3 67

compare

Page 11: Insertion 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 12: Insertion Sort

Insertion Sort

972 5 1 4 3 6

Page 13: Insertion Sort

Insertion Sort

972 5 1 4 3 6

Page 14: Insertion Sort

Insertion Sort

972 5 1 4 3 6

Item to position

Page 15: Insertion Sort

Insertion Sort

972

5

1 4 3 65

compare

Page 16: Insertion Sort

Insertion Sort

972

5

1 4 3 69

compare

Page 17: Insertion Sort

Insertion Sort

972

5

1 4 3 67

compare

belongs here

Page 18: Insertion Sort

Insertion Sort

972 5 1 4 3 6

Page 19: Insertion Sort

Insertion Sort

972 5 1 4 3 6

Page 20: Insertion Sort

Insertion Sort

972 5 1 4 3 6

Item to position

Page 21: Insertion Sort

Insertion Sort

972 5

1

4 3 61

compare

Page 22: Insertion Sort

Insertion Sort

972 5

1

4 3 69

compare

Page 23: Insertion Sort

Insertion Sort

972 5

1

4 3 67

compare

Page 24: Insertion Sort

Insertion Sort

972 5

1

4 3 65

compare

Page 25: Insertion Sort

Insertion Sort

972 5

1

4 3 62

belongs here

Page 26: Insertion Sort

Insertion Sort

972 51 4 3 6

Page 27: Insertion Sort

Insertion Sort

972 51 4 3 6

Page 28: Insertion Sort

Insertion Sort

972 51 4 3 6

Item to position

Page 29: Insertion Sort

Insertion Sort

972 51

4

3 64

compare

Page 30: Insertion Sort

Insertion Sort

972 51

4

3 69

compare

Page 31: Insertion Sort

Insertion Sort

972 51

4

3 67

compare

Page 32: Insertion Sort

Insertion Sort

972 51

4

3 6

belongs here

5

compare

Page 33: Insertion Sort

Insertion Sort

972 51 4 3 6

Page 34: Insertion Sort

Insertion Sort

972 51 4 3 6

Page 35: Insertion Sort

Insertion Sort

972 51 4 3 6

Item to position

Page 36: Insertion Sort

Insertion Sort

972 51 4

3

63

compare

Page 37: Insertion Sort

Insertion Sort

972 51 4

3

69

compare

Page 38: Insertion Sort

Insertion Sort

972 51 4

3

67

compare

Page 39: Insertion Sort

Insertion Sort

972 51 4

3

65

compare

Page 40: Insertion Sort

Insertion Sort

972 51 4

3

64

compare

belongs here

Page 41: Insertion Sort

Insertion Sort

972 51 43 6

Page 42: Insertion Sort

Insertion Sort

972 51 43 6

Page 43: Insertion Sort

Insertion Sort

972 51 43 6

Item to position

Page 44: Insertion Sort

Insertion Sort

972 51 43

6

6

compare

Page 45: Insertion Sort

Insertion Sort

972 51 43

6

9

compare

Page 46: Insertion Sort

Insertion Sort

972 51 43

6

7

compare

belongs here

Page 47: Insertion Sort

Insertion Sort

972 51 43 6

Page 48: Insertion Sort

Insertion Sort

972 51 43 6

SORTED!