Sorting
Selection Sort
Selection sort is an algorithm that selects the smallest element
from an unsorted list in each iteration and places that element
at the beginning of the unsorted list.
It sorts an array by repeatedly finding the minimum element
(considering ascending order) from unsorted part and putting it
at the beginning. The algorithm maintains two subarrays in a
given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted. 2
Selection Sort
How Selection Sort Works?
1. Set the first element as minimum
2. Compare minimum with the second element. If the second
element is smaller than minimum, assign the second
element as minimum.
Compare minimum with the third element. Again, if the
third element is smaller, then assign minimum to the third
element otherwise do nothing. The process goes on until the
last element. 3
Selection Sort
3. After each iteration, minimum is placed in the front of the
unsorted list.
4. For each iteration, indexing starts from the first unsorted
element. Step 1 to 3 are repeated until all the elements are
placed at their correct positions.
4
Example #1
Sort the following given in ascending pattern
52 1 41 18 23 62 10 6
First iteration
52 1 41 18 23 62 10 6 52 1 41 18 23 62 10 6
52 1 41 18 23 62 10 6 52 1 41 18 23 62 10 6
52 1 41 18 23 62 10 6 52 1 41 18 23 62 10 6
52 1 41 18 23 62 10 6 1 52 41 18 23 62 10 6
5
Example #1
Second iteration 1 step
1 52 41 18 23 62 10 6 1 52 41 18 23 62 10 6
1 52 41 18 23 62 10 6 1 52 41 18 23 62 10 6
1 52 41 18 23 62 10 6 1 52 41 18 23 62 10 6
1 52 41 18 23 62 10 6 1 6 41 18 23 62 10 52
6
Example #1
Third iteration 2 steps
1 6 41 18 23 62 10 52 1 6 41 18 23 62 10 52
1 6 41 18 23 62 10 52 1 6 41 18 23 62 10 52
1 6 41 18 23 62 10 52 1 6 10 18 23 62 41 52
1 6 41 18 23 62 10 52
7
Example #1
Fourth iteration 3 steps Fourth iteration 3 steps
1 6 10 18 23 62 41 52 1 6 10 18 23 62 41 52
1 6 10 18 23 62 41 52 1 6 10 18 23 62 41 52
1 6 10 18 23 62 41 52 1 6 10 18 23 62 41 52
1 6 10 18 23 62 41 52 1 6 10 18 23 62 41 52
1 6 10 18 23 62 41 52
8
Example #1
Sixth iteration 3 steps Seventh iteration 4 steps
1 6 10 18 23 62 41 52 1 6 10 18 23 41 62 52
1 6 10 18 23 62 41 52 1 6 10 18 23 41 62 52
1 6 10 18 23 62 41 52 1 6 10 18 23 41 52 62
1 6 10 18 23 41 62 52
Therefore; 7 iteration and
5 steps 9
Example #2
Sort the following given in ascending pattern
33 8 95 0 54
First iteration Second iteration 1 step
33 8 95 0 54 33 8 95 0 54 0 8 95 33 54
33 8 95 0 54 0 8 95 33 54 0 8 95 33 54
33 8 95 0 54 0 8 95 33 54
33 8 95 0 54 0 8 95 33 54
10
Example #2
Sort the following given in ascending pattern
33 8 95 0 54
Third iteration 1 step Fourth iteration 2 steps
0 8 95 33 54 0 8 33 95 54
Therefore;
0 8 95 33 54 0 8 33 95 54 4 iteration and 3
steps
0 8 95 33 54 0 8 33 54 95
0 8 33 95 54
11
Example #3
Sort the following given in descending pattern
99 14 69 7 2 33 10
First iteration
99 14 69 7 2 33 10 99 14 69 7 2 33 10
99 14 69 7 2 33 10 99 14 69 7 2 33 10
99 14 69 7 2 33 10 99 14 69 7 2 33 10
99 14 69 7 2 33 10
12
Example #3
Sort the following given in descending pattern
99 14 69 7 2 33 10
Second iteration
99 14 69 7 2 33 10 99 14 69 7 2 33 10
99 14 69 7 2 33 10 99 14 69 7 2 33 10
99 14 69 7 2 33 10 99 69 14 7 2 33 10
99 14 69 7 2 33 10
13
Example #3
Sort the following given in descending pattern
99 14 69 7 2 33 10
Third iteration 1 step
99 69 14 7 2 33 10 99 69 14 7 2 33 10
99 69 14 7 2 33 10 99 69 33 7 2 14 10
99 69 14 7 2 33 10
99 69 14 7 2 33 10
14
Example #3
Sort the following given in descending pattern
99 14 69 7 2 33 10
Fourth iteration 2 steps
99 69 33 7 2 14 10 99 69 33 14 2 7 10
99 69 33 7 2 14 10
99 69 33 7 2 14 10
99 69 33 7 2 14 10
15
Example #3
Sort the following given in descending pattern
99 14 69 7 2 33 10
Fifth iteration 3 steps Sixth iteration 4 steps
99 69 33 14 2 7 10 99 69 33 14 10 7 2
99 69 33 14 2 7 10 99 69 33 14 10 7 2
99 69 33 14 2 7 10
Therefore; 6 iteration
and 4 step
99 69 33 14 10 7 2
16
Activity:
Sort the given below in ascending and descending
using Selection Sort
23 2 8 14 25 11 12 0
8 15 0 33 13 1 42 5 39 56
17