Additional Example of Selection and Insertion Sort
Additional Example of Selection and Insertion Sort
1
Sorting Algorithm
Simple Sorting ------------ done
Bubble Sorting ----------- done
Selection Sorting
Insertion Sorting
2
III. Selection Sort
Algorithm
}
Example: i=0,
j=1
Index =0
i
Value 5 1 3 4 6 2
Index 0 1 2 3 4 5
ind j
ex
6
Example: i=0,
j=2
Index =1
i
Value 5 1 3 4 6 2
Index 0 1 2 3 4 5
j
ind
ex
7
Example: i=0,
j=3
Index =1
i
Value 5 1 3 4 6 2
Index 0 1 2 3 4 5
ind j
ex
8
Example: i=0,
j=4
Index =1
i
Value 5 1 3 4 6 2
Index 0 1 2 3 4 5
ind
ex
j
9
Example: i=0,
j=5
Index =1
i
Value 5 1 3 4 6 2
Index 0 1 2 3 4 5
ind
ex
j
10
Example: i=0,
j=6
Index =1
i
Value 5 1 3 4 6 2 Over
Index 0 1 2 3 4 5
ind j
ex
Value 1 5 3 4 6 2
Index 0 1 2 3 4 5
11
Example: i=1,
j=2
Index =1
i
Value 1 5 3 4 6 2
Index 0 1 2 3 4 5
ind j
ex
12
Example: i=1,
j=3
Index =2
i
Value 1 5 3 4 6 2
Index 0 1 2 3 4 5
j
ind
ex
13
Example: i=1,
j=4
Index =2
i
Value 1 5 3 4 6 2
Index 0 1 2 3 4 5
ind j
ex
14
Example: i=1,
j=5
Index =1
Value 1 5 3 4 6 2
Index 0 1 2 3 4 5
ind j
ex
15
Example: i=1,
j=6
Index =5
i
Value 1 5 3 4 6 2 Over
Index 0 1 2 3 4 5
j
ind
ex
Swap(ArrayList[i], ArrayList[index])
Value 1 2 3 4 6 5
Index 0 1 2 3 4 5
16
Example: i=2,
j=3
Index =2
i
Value 1 2 3 4 6 5
Index 0 1 2 3 4 5
ind j
ex
17
Example: i=2,
j=4
Index =2
i
Value 1 2 3 4 6 5
Index 0 1 2 3 4 5
ind j
ex
18
Example: i=2,
j=3
Index =2
i
Value 1 2 3 4 6 5 Over
Index 0 1 2 3 4 5
ind j
ex
Swap(ArrayList[i], ArrayList[index])
Value 1 2 3 4 6 5
Index 0 1 2 3 4 5
19
Example: i=3,
j=4
Index =3
Value 1 2 3 4 6 5
Index 0 1 2 3 4 5
ind j
ex
20
Example: i=3,
j=5
Index =5
Value 1 2 3 4 6 5
Index 0 1 2 3 4 5
ind j
ex
21
Example: i=3,
j=6
Index =3
i
Value 1 2 3 4 6 5 Over
Index 0 1 2 3 4 5
ind j
ex
Swap(ArrayList[i], ArrayList[index])
Value 1 2 3 4 6 5
Index 0 1 2 3 4 5
22
Example: i=4,
j=5
Index =4
i
Value 1 2 3 4 6 5
Index 0 1 2 3 4 5
ind
ex
j
23
Example: i=4,
j=6
Index =5
i
Value 1 2 3 4 6 5 Over
Index 0 1 2 3 4 5
j
ind
ex
Swap(ArrayList[i], ArrayList[index])
Value 1 2 3 4 5 6
Index 0 1 2 3 4 5
24
Example: i=5,
j=6
Index =5
i
Value 1 2 3 4 5 6 Over
Index 0 1 2 3 4 5
ind j
ex
Swap(ArrayList[i], ArrayList[index])
Value 1 2 3 4 5 6
Index 0 1 2 3 4 5
25
Example: i= 6
Over
Value 1 2 3 4 5 6
Index 0 1 2 3 4 5
Sorting is Done
26
Exercise
Sort the following array list using selection Sort
algorithm (Show each steps)
8 10 9 5 4 1 2 3
27
Complexity Analysis
29
IV. Insertion Sort
Algorithm:
Insertion sort algorithm somewhat be similar to
Selection Sort and Bubble sort.
Array is imaginary divided into two parts - sorted one
and unsorted one.
31
Insertion sort works by inserting item into its proper
place in the list.
Insertion sort is simply like playing cards: To sort the
cards in your hand, you extract a card, shift the
remaining cards and then insert the extracted card in
the correct place.
This process is repeated until all the cards are in the
correct sequence.
Is over twice as fast as the bubble sort and is just as
easy to implement as the selection sort.
Advantage:
Relatively simple and easy to implement.
Disadvantage:
Inefficient for large lists.
32
C++ implementation
Step 1: - Pick 12
Put at first position
12
10
6 15 8 3
1 2 34
Insertion Sort Example
Step 2: - Pick 10
10 is less than 12
Shift to right 12 and
put 10 first
12
10 6 15 8 3 1 2
35
Insertion Sort Example
Step 3: - Pick 6
6 is less than all
then shift 10 and 12.
and then put 6 at
first
0 12
1
6 15
8 3 1
2
36
Insertion Sort Example
Step 4: - Pick 15
15 is greater than all
then put it at the
end
0 12
1
6
15 8 3 1
2
37
Insertion Sort Example
Step 5: - Pick 8
8 is greater than 6
and less than 10.
then shift 10, 12
2 15 1nd 15 and put b/n 6
1
10 and 10
6
8 3 1
2
38
Insertion Sort Example
Step 6: - Pick 3
3 is less than 6 then
shift 15,12,10,8 and
6 and put 3 before 6
1
10 1122 5
8
6
3 1 2
39
Insertion Sort Example
Step 8: - Pick 1
1 is less than all
then shift 15, 12,
10, 8, 6 and 3 and
put 1 before 3
1
10 1122 5
8
6
3
1 2
40
Insertion Sort Example
Step 9: - Pick 2
2 is less than 3 and greater
1 than then shift 15, 12,
10, 8, 6 and 3 and put b/n
1 and 3
8 10 1122 15
6
3
1
2 41
Insertion Sort Example
8 10 1122 15
6
3
2
1
42
Exercise : using insertion sort
7 -5 2 16 4
43
Complexity Analysis
44