Download as pdf or txt
Download as pdf or txt
You are on page 1of 107

10/22/2021 1

 Given an array of length n,


 Search elements 0 through n-1 and select the smallest
 Swap it with the element in location 0
 Search elements 1 through n-1 and select the smallest
 Swap it with the element in location 1
 Search elements 2 through n-1 and select the smallest
 Swap it with the element in location 2
 Search elements 3 through n-1 and select the smallest
 Swap it with the element in location 3
 Continue in this fashion until there’s nothing left to search

Sorting & MST Friday, October 22, 2021 2


7 2 8 5 4

2 7 8 5 4

2 4 8 5 7

2 4 5 8 7

2 4 5 7 8

Sorting & MST Friday, October 22, 2021 3


Selection Sort 4

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort 5

5 1 3 4 6 2

Comparison

Data Movement

Sorted
6

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
7

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
8

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
9

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
10

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
11

Selection Sort

5 1 3 4 6 2

Largest

Comparison

Data Movement

Sorted
12

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted
13

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted
14

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted
15

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted
16

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted
17

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted
18

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted
19

Selection Sort

5 1 3 4 2 6

Largest

Comparison

Data Movement

Sorted
20

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted
21

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted
22

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted
23

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted
24

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted
25

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted
26

Selection Sort

2 1 3 4 5 6

Largest

Comparison

Data Movement

Sorted
27

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted
28

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted
29

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted
30

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted
31

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted
32

Selection Sort

2 1 3 4 5 6

Largest

Comparison

Data Movement

Sorted
33

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted
34

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted
35

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted
36

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted
37

Selection Sort

2 1 3 4 5 6

Largest

Comparison

Data Movement

Sorted
38

Selection Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted
39

Selection Sort

1 2 3 4 5 6
DONE!
Comparison

Data Movement

Sorted
Selection Sort: Algorithm

10/22/2021 40
10/22/2021 42
To insert 12, we need to
make room for it by moving
first 36 and then 24.

Sorting & MST Friday, October 22, 2021 43


Sorting & MST Friday, October 22, 2021 44
Sorting & MST Friday, October 22, 2021 45
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, insertion sort also
requires a single additional memory space.
It is Stable, as it does not change the relative order of elements
with equal keys
10/22/2021 46
10/22/2021 47
8 2 4 9 3 6

Sorting & MST Friday, October 22, 2021 48


8 2 4 9 3 6

Sorting & MST Friday, October 22, 2021 49


8 2 4 9 3 6

2 8 4 9 3 6

Sorting & MST Friday, October 22, 2021 50


8 2 4 9 3 6

2 8 4 9 3 6

Sorting & MST Friday, October 22, 2021 51


8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

Sorting & MST Friday, October 22, 2021 52


8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

Sorting & MST Friday, October 22, 2021 53


8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

Sorting & MST Friday, October 22, 2021 54


8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

Sorting & MST Friday, October 22, 2021 55


8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

Sorting & MST Friday, October 22, 2021 56


8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

Sorting & MST Friday, October 22, 2021 57


8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

2 3 4 6 8 9 done

Sorting & MST Friday, October 22, 2021 58


Example :

9 2 7 5 1 4 3 6

10/22/2021 59
Sorted
section

9 2 7 5 1 4 3 6

We start by dividing the array in two sections: a sorted 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.

10/22/2021 60
Sorted Item to
section position

9 2 7 5 1 4 3 6

The first element in the unsorted section is the next


element to be put into the correct position.

10/22/2021 61
Item to
position

9 2 7 5 1 4 3 6

We copy the element to be placed into another


variable so it doesn’t get overwritten.

10/22/2021 62
9 2 7 5 1 4 3 6

2
compare

If the previous position is more than the item being


placed, copy the value into the next position

10/22/2021 63
belongs here

9 9 7 5 1 4 3 6

If there are no more items in the sorted section to


compare with, the item to be placed must go at the front.

10/22/2021 64
2 9 7 5 1 4 3 6

10/22/2021 65
2 9 7 5 1 4 3 6

10/22/2021 66
Item to
position

2 9 7 5 1 4 3 6

10/22/2021 67
2 9 7 5 1 4 3 6

7
compare

10/22/2021 68
Copied from
previous position
belongs here

2 9 9 5 1 4 3 6

7
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.

10/22/2021 69
2 7 9 5 1 4 3 6

10/22/2021 70
2 7 9 5 1 4 3 6

10/22/2021 71
Item to
position

2 7 9 5 1 4 3 6

10/22/2021 72
2 7 9 5 1 4 3 6

5
compare

10/22/2021 73
2 7 9 9 1 4 3 6

5
compare

10/22/2021 74
belongs here

2 7 7 9 1 4 3 6

5
compare

10/22/2021 75
2 5 7 9 1 4 3 6

10/22/2021 76
2 5 7 9 1 4 3 6

10/22/2021 77
Item to
position

2 5 7 9 1 4 3 6

10/22/2021 78
2 5 7 9 1 4 3 6

1
compare

10/22/2021 79
2 5 7 9 9 4 3 6

1
compare

10/22/2021 80
2 5 7 7 9 4 3 6

1
compare

10/22/2021 81
2 5 5 7 9 4 3 6

1
compare

10/22/2021 82
belongs here

2 2 5 7 9 4 3 6

10/22/2021 83
1 2 5 7 9 4 3 6

10/22/2021 84
1 2 5 7 9 4 3 6

10/22/2021 85
Item to
position

1 2 5 7 9 4 3 6

10/22/2021 86
1 2 5 7 9 4 3 6

4
compare

10/22/2021 87
1 2 5 7 9 9 3 6

4
compare

10/22/2021 88
1 2 5 7 7 9 3 6

4
compare

10/22/2021 89
belongs here

1 2 5 5 7 9 3 6

4
compare

10/22/2021 90
1 2 4 5 7 9 3 6

10/22/2021 91
1 2 4 5 7 9 3 6

10/22/2021 92
Item to
position

1 2 4 5 7 9 3 6

10/22/2021 93
1 2 4 5 7 9 3 6

3
compare

10/22/2021 94
1 2 4 5 7 9 9 6

3
compare

10/22/2021 95
1 2 4 5 7 7 9 6

3
compare

10/22/2021 96
1 2 4 5 5 7 9 6

3
compare

10/22/2021 97
belongs here

1 2 4 4 5 7 9 6

3
compare

10/22/2021 98
1 2 3 4 5 7 9 6

10/22/2021 99
1 2 3 4 5 7 9 6

10/22/2021 100
Item to
position

1 2 3 4 5 7 9 6

10/22/2021 101
1 2 3 4 5 7 9 6

6
compare

10/22/2021 102
1 2 3 4 5 7 9 9

6
compare

10/22/2021 103
belongs here

1 2 3 4 5 7 7 9

6
compare

10/22/2021 104
1 2 3 4 5 6 7 9

10/22/2021 105
1 2 3 4 5 6 7 9

SORTED!

10/22/2021 106
10/22/2021 107

You might also like