Insertion Sort - Sorting Algorithms 2024

OlaGlal1 5 views 33 slides Jul 24, 2024
Slide 1
Slide 1 of 33
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33

About This Presentation

Insertion sort


Slide Content

Insertion Sort

90 70 23 18 13
Make sure all the elements in the
Orange Window
are Sorted

90 70 23 18 13
1.Start from Left to Right
2.Increase the window size by one
3.After each increase, Move elements
GREATER THAN the new element
one step forward.

6 9 18 27 13
For Example,
We need to move 18 & 27 one step
To make free space for the 13
6 9 18 27

90 70 23 18 13
Window is sorted.
→ Increase its size by one.

90 70 23 18 13
→ Set current to the newly added element.
→ compare current to all the elements to
its left one by one
Current ⇒ 70

90 70 23 18 13
Current < 90
Yes ⇒ copy 90 to the right
Current ⇒ 70

90 90 23 18 13
No more elements GREATER Than
Current
⇒ Add 70 to the place where ( all the
elements to its left are SMALLER )
Current ⇒ 70

70 90 23 18 13
all the elements in theOrange Window
are Sorted.
⇒Increase the window size by one
⇒ set “ Current “ to the new element
Current ⇒ 70

70 90 23 18 13
Current < 90
Yes ⇒ copy 90 to the right
Current ⇒ 23

70 90 90 18 13
Current < 70
Yes ⇒ copy 70 to the right
Current ⇒ 23

70 70 90 18 13
No more elements GREATER Than
Current
⇒ Add 23 to the place where ( all the
elements to its left are SMALLER )
Current ⇒ 23

23 70 90 18 13
all the elements in theOrange Window
are Sorted.
⇒Increase the window size by one
⇒ set “ Current “ to the new element
Current ⇒ 23

23 70 90 18 13
Current < 90
Yes ⇒ copy 90 to the right
Current ⇒ 18

23 70 90 90 13
Current < 70
Yes ⇒ copy 70 to the right
Current ⇒ 18

23 70 70 90 13
Current < 23
Yes ⇒ copy 23 to the right
Current ⇒ 18

23 23 70 90 13
No more elements GREATER Than
Current
⇒ Add 18 to the place where ( all the
elements to its left are SMALLER )
Current ⇒ 18

18 23 70 90 13
all the elements in theOrange Window
are Sorted.
⇒Increase the window size by one
⇒ set “ Current “ to the new element
Current ⇒ 18

18 23 70 90 13
Current < 90
Yes ⇒ copy 90 to the right
Current ⇒ 13

18 23 70 90 90
Current < 70
Yes ⇒ copy 70 to the right
Current ⇒ 13

18 23 70 70 90
Current < 23
Yes ⇒ copy 23 to the right
Current ⇒ 13

18 23 23 70 90
Current < 18
Yes ⇒ copy 18 to the right
Current ⇒ 13

18 18 23 70 90
No more elements GREATER Than
Current
⇒ Add 13 to the place where ( all the
elements to its left are SMALLER )
Current ⇒ 13

13 18 23 70 90
Reached the end of the array ⇒ Done

Sort only until the element “ 5 “
12, 11, 13, 5, 6

→ 12, 11, 13, 5, 6⇒ current = 12
→ 12, 11, 13, 5, 6⇒ current = 1111 < 12 YES
→ 12, 12, 13, 5, 6
→ 11, 12, 13, 5, 6
→ 11, 12, 13, 5, 6⇒ current = 1313 < 12 NO
→ 11, 12, 13, 5, 6 ⇒ current = 55 < 13 YES
→ 11, 12, 13, 13, 6⇒ current = 55 < 12 YES
→ 11, 12, 12, 13, 6⇒ current = 55 < 11 YES
→ 11, 11, 12, 13, 6
→ 5, 11, 12, 13, 6
12, 11, 13, 5, 6

→ 5, 11, 12, 13, 6 ⇒ current = 6
→ 5, 11, 12, 13, 6 ⇒ current = 66 < 13 YES
→ 5, 11, 12, 13, 13 ⇒ current = 66 < 12 YES
→ 5, 11, 12, 12, 13 ⇒ current = 66 < 11 YES
→ 5, 11, 11, 12, 13 ⇒ current = 66 < 5 NO
→ 5, 6, 11, 12, 13 ⇒ current = 6
12, 11, 13, 5, 6

Your Turn !
2, 5, 8, 12, 7

→ 2, 5, 8, 12, 7⇒ current = 2
→ 2, 5, 8, 12, 7⇒ current = 55 < 2 NO
→ 2, 5, 8, 12, 7⇒ current = 88 < 5 NO
→ 2, 5, 8, 12, 7⇒ current = 1212 < 8 NO
→ 2, 5, 8, 12, 7 ⇒ current = 77 < 12 YES
→ 2, 5, 8, 12, 12⇒ current = 77 < 8 YES
→ 2, 5, 8, 8, 12 ⇒ current = 77 < 5 NO
→ 2, 5, 7, 8, 12
2, 5, 8, 12, 7

2 Exit Points
1. Current is NOT less than compared
element.
2. We’ve reached the start
( we then increase the window size )

Problems with Insertion Sort

252421201915121097
252421201915121097
Current = 10
252524212019151297

252421201915121097
252421201915121097
102524212019151297
Current = 10
Tags