Lecture 3
Lecture 3
(KCS503)
Lecture -3
A Sorting Problem
(Exhaustive Search Approach)
2 3 4
3 4 2 4 2 3
4 3 4 2 3 2
A Sorting Problem
(Exhaustive Search Approach)
How to generate the 24 permutation ?
1 2
2 3 4 1 3 4
3 4 2 4 2 3 3 4 1 4 1 3
4 3 4 2 3 2 4 3 4 1 3 1
A Sorting Problem
(Exhaustive Search Approach)
How to generate the 24 permutation ?
3 4
1 2 4 1 3 2
2 4 1 4 2 1 3 2 1 2 1 3
4 2 4 1 1 2 2 3 2 1 3 1
A Sorting Problem
(Exhaustive Search Approach)
An in-depth look at the analysis of the 24 permutations of four digits
A Sorting Problem
(Exhaustive Search Approach)
Let there be a set of four digits and note that there are multiple
possible permutations for the four digits. They are:
1234 2134 3124 41 23
1243 2143 3142 41 32
1324 2314 3214 42 13
1342 2341 3241 42 31
1423 2431 3412 43 12
1432 2413 3421 43 21
• There are 24 different permutations possible. (as shown
above)
• Only one of these permutations meets our criteria.
(i.e. A1 ≤ A2 ≤ …≤ An) . (1 2 3 4)
A Sorting Problem
(Exhaustive Search Approach)
How we do this ?
Step 1: Generate all the permutation and store it.
Step 2: Check all the permutation one by one and find which
permutation is satisfying the required condition (i.e. a1 ≤ a2 ≤
・ ・ ・ ≤ an).
Step 3: Once we get it , we got the victory.
A Sorting Problem
(Exhaustive Search Approach)
How we do this ?
Step 1: Generate all the permutation and store it.
Step 2: Check all the permutation one by one and find which
permutation is satisfying the required condition (i.e. a1 ≤ a2 ≤
・ ・ ・ ≤ an).
Step 3: Once we get it , we got the victory.
• For the first position in the sorted array, the whole array is traversed
from index 0 to 4 sequentially. After going through the entire array, it is
evident that 3 is the lowest value, with 7 being stored at the first
position.
• Thus, replace 7 with 3. At the end of the first iteration, the item with
the lowest value, in this case 3, at position 2 is most likely to be at the
top of the sorted list.
A Sorting Problem
(Selection Sort Approach)
Lets consider the following array as an example:
A [] = (7, 4, 3, 6, 5).
• 1st Iteration
A Sorting Problem
(Selection Sort Approach)
• For the second position, where 25 is present, again traverse the rest of
the array in a sequential manner.
• Using the traversal method, we determined that the value 12 is the
second-lowest in the array and thus should be placed in the second
position. So no need of swapping.
A Sorting Problem
(Selection Sort Approach)
Lets consider the following array as an example:
A [] = (7, 4, 3, 6, 5).
• 2nd Iteration
A Sorting Problem
(Selection Sort Approach)
• For the third position, where 7 is present, again traverse the rest of the
array in a sequential manner.
• Using the traversal method, we determined that the value 5 is the third-
lowest in the array and thus should be placed in the third position.
A Sorting Problem
(Selection Sort Approach)
Lets consider the following array as an example:
A [] = (7, 4, 3, 6, 5).
• 3rd Iteration
A Sorting Problem
(Selection Sort Approach)
• Similarly we execute it for fourth and fifth iteration and finally the sorted
array is looks like as below:
A Sorting Problem
(Selection Sort Algorithm)
SELECTION SORT(arr, n)