Hot questions from Sorting and Searching ( Chapter 5 and 6):
Sorting:
Bubble sort:
1) Sort the following list using bubble sort. List1 = [10, 9, 8, 6, 7, 5]
Ans:
Initial state of the list → [10, 9, 8, 6, 7, 5]
Pass 1:
Compare 10 with 9 → Swap 10 and 9 → [9, 10, 8, 6, 7, 5]
Compare 10 with 8 → Swap 10 and 8 → [9, 8, 10, 6, 7, 5]
Compare 10 with 6 → Swap 10 and 6 → [9, 8, 6, 10, 7, 5]
Compare 10 with 7 → Swap 10 and 7 → [9, 8, 6, 7, 10, 5]
Compare 10 with 5 → Swap 10 and 5 → [9, 8, 6, 7, 5, 10]
Pass 2:
Compare 9 with 8 → Swap 9 and 8 → [8, 9, 6, 7, 5, 10]
Compare 9 with 6 → Swap 9 and 6 → [8, 6, 9, 7, 5, 10]
Compare 9 with 7 → Swap 9 and 7 → [8, 6, 7, 9, 5, 10]
Compare 9 with 5 → Swap 9 and 5 → [8, 6, 7, 5, 9, 10]
Pass 3:
Compare 8 with 6 → Swap 8 and 6 → [6, 8, 7, 5, 9, 10]
Compare 8 with 7 → Swap 8 and 7 → [6, 7, 8, 5, 9, 10]
Compare 8 with 5 → Swap 8 and 5 → [6, 7, 5, 8, 9, 10]
Pass 4:
Compare 6 with 7 → No exchange → [6, 7, 5, 8, 9, 10]
Compare 7 with 5 → Swap 7 and 5 → [6, 5, 7, 8, 9, 10]
Pass 5:
Compare 6 with 5 → Swap 6 and 5 → [5, 6, 7, 8, 9, 10]
Final sorted list → [5, 6, 7, 8, 9, 10]
2) Consider a list numList = [4, 2, -6, 3, 9]. Perform bubble sort in this.
Ans:
Initial state of the list → [4, 2, -6, 3, 9]
Pass 1:
Compare 4 with 2 → Swap 4 and 2 → [2, 4, -6, 3, 9]
Compare 4 with -6 → Swap 4 and -6 → [2, -6, 4, 3, 9]
Compare 4 with 3 → Swap 4 and 3 → [2, -6, 3, 4, 9]
Compare 4 with 9 → No exchange → [2, -6, 3, 4, 9]
Pass 2:
Compare 2 with -6 → Swap 2 and -6 → [-6, 2, 3, 4, 9]
1
Compare 2 with 3 → No exchange → [-6, 2, 3, 4, 9]
Compare 3 with 4 → No exchange → [-6, 2, 3, 4, 9]
Pass 3:
Compare -6 with 2 → No exchange → [-6, 2, 3, 4, 9]
Compare 2 with 3 → No exchange → [-6, 2, 3, 4, 9]
Pass 4:
Compare -6 with 2 → No exchange → [-6, 2, 3, 4, 9]
Final sorted list → [-6, 2, 3, 4, 9]
Selection sort:
3) Sort the following list using selection sort. List1 = [10, 9, 8, 6, 7, 5]
Ans:
Initial state of the list → [10, 9, 8, 6, 7, 5]
Pass 1:
Min = 10, Flag = 0
Compare 10 with 9 → Min = 9, Flag = 1
Compare 9 with 8 → Min = 8
Compare 8 with 6 → Min = 6
Compare 6 with 7 → Min = 6
Compare 6 with 5 → Min = 5
Swap 10 and 5 → [5, 9, 8, 6, 7, 10]
Pass 2:
Min = 9, Flag = 0
Compare 9 with 8 → Min = 8, Flag = 1
Compare 8 with 6 → Min = 6
Compare 6 with 7 → Min = 6
Compare 6 with 10 → Min = 6
Swap 9 and 6 → [5, 6, 8, 9, 7, 10]
Pass 3:
Min = 8, Flag = 0
Compare 8 with 9 → Min = 8
Compare 8 with 7 → Min = 7, Flag = 1
Compare 7 with 10 → Min = 7
Swap 8 and 7 → [5, 6, 7, 9, 8, 10]
Pass 4:
Min = 9, Flag = 0
Compare 9 with 8 → Min = 8, Flag = 1
Compare 8 with 10 → Min = 8
Swap 9 and 8 → [5, 6, 7, 8, 9, 10]
Pass 5:
Min = 9, Flag = 0
Compare 9 with 10 → Min = 9
No Exchange → [5, 6, 7, 8, 9, 10]
2
Final sorted list → [5, 6, 7, 8, 9, 10]
4) Consider a list numList = [4, 2, -6, 3, 9] sort elements of list using selection sort method.
Ans:
Initial state of the list → [4, 2, -6, 3, 9]
Pass 1:
Min = 4, Flag = 0
Compare 4 with 2 → Min = 2, Flag = 1
Compare 2 with -6 → Min = -6
Compare -6 with 3 → Min = -6
Compare -6 with 9 → Min = -6
Swap 4 and -6 → [-6, 2, 4, 3, 9]
Pass 2:
Min = 2, Flag = 0
Compare 2 with 4 → Min = 2
Compare 2 with 3 → Min = 2
Compare 2 with 9 → Min = 2
No Exchange → [-6, 2, 4, 3, 9]
Pass 3:
Min = 4, Flag = 0
Compare 4 with 3 → Min = 3, Flag = 1
Compare 3 with 9 → Min = 3
Swap 4 and 3 → [-6, 2, 3, 4, 9]
Pass 4:
Min = 4, Flag = 0
Compare 4 with 9 → Min = 4
No Exchange → [-6, 2, 3, 4, 9]
Final sorted list → [-6, 2, 3, 4, 9]
Insertion sort:
5) Sort the following list using insertion sort. List1 = [10, 9, 8, 6, 7, 5]
Ans:
Initial state of the list → [10, 9, 8, 6, 7, 5]
Pass 1:
First element 10 is considered sorted. Next element is 9.
Compare 9 with 10 → Swap 9 and 10 → [9, 10, 8, 6, 7, 5]
Pass 2:
Next element to consider is 8.
Compare 8 with 10 → Swap 8 and 10 → [9, 8, 10, 6, 7, 5]
Compare 8 with 9 → Swap 8 and 9 → [8, 9, 10, 6, 7, 5]
Pass 3:
Next element to consider is 6.
3
Compare 6 with 10 → Swap 6 and 10 → [8, 9, 6, 10, 7, 5]
Compare 6 with 9 → Swap 6 and 9 → [8, 6, 9, 10, 7, 5]
Compare 6 with 8 → Swap 6 and 8 → [6, 8, 9, 10, 7, 5]
Pass 4:
Next element to consider is 7.
Compare 7 with 10 → Swap 7 and 10 → [6, 8, 9, 7, 10, 5]
Compare 7 with 9 → Swap 7 and 9 → [6, 8, 7, 9, 10, 5]
Compare 7 with 8 → Swap 7 and 8 → [6, 7, 8, 9, 10, 5]
Compare 7 with 6 → No Exchange → [6, 7, 8, 9, 10, 5]
Pass 5:
Next element to consider is 5.
Compare 5 with 10 → Swap 5 and 10 → [6, 7, 8, 9, 5, 10]
Compare 5 with 9 → Swap 5 and 9 → [6, 7, 8, 5, 9, 10]
Compare 5 with 8 → Swap 5 and 8 → [6, 7, 5, 8, 9, 10]
Compare 5 with 7 → Swap 5 and 7 → [6, 5, 7, 8, 9, 10]
Compare 5 with 6 → Swap 5 and 6 → [5, 6, 7, 8, 9, 10]
Final sorted list → [5, 6, 7, 8, 9, 10]
6) Consider a list numList = [4, 2, -6, 3, 9] sort elements of list using insertion sort method.
Ans:
Initial state of the list → [4, 2, -6, 3, 9]
Pass 1:
First element 4 is considered sorted. Next element is 2.
Compare 2 with 4 → Swap 2 and 4 → [2, 4, -6, 3, 9]
Pass 2:
Next element to consider is -6.
Compare -6 with 4 → Swap -6 and 4 → [2, -6, 4, 3, 9]
Compare -6 with 2 → Swap -6 and 2 → [-6, 2, 4, 3, 9]
Pass 3:
Next element to consider is 3.
Compare 3 with 4 → Swap 3 and 4 → [-6, 2, 3, 4, 9]
Compare 3 with 2 → No Exchange → [-6, 2, 3, 4, 9]
Pass 4:
Next element to consider is 9.
Compare 9 with 4 → No Exchange → [-6, 2, 3, 4, 9]
Final sorted list → [-6, 2, 3, 4, 9]
4
Searching:
Linear Search:
1) numList = [8, -4, 7, 17, 0, 2, 19] Search for the key, 17 in numList using linear search.
Ans:
n=7
The step-by-step process of linear search
After four comparisons, the algorithm finds the key 17
and will display ‘Element found at position 4’.
2) numList = [17, 8, -4, 7, 0, 2, 19] Search for the key, 17 in numList using linear search.
Ans:
n=7
The step-by-step process of linear search
After one comparison, the algorithm finds the key 17
and will display ‘Element found at position 1’.
3) List1 = [8, 16, 0, 3] Search for the key 10 in this using linear search
Ans:
n=4
The step-by-step process of linear search
5
Binary Search:
4) numList = [2, 3, 5, 7, 10, 11, 12, 17, 19, 23, 29, 31, 37, 41, 43]. Search for the key 7 using Binary
search.
Ans:
5) numList = [2, 3, 5, 7, 10, 11, 12, 17, 19, 23, 29, 31, 37, 41, 43]. Search for the key 11 using Binary
search.
Ans:
6) numList = [2, 3, 5, 7, 10, 11, 12, 17, 19, 23, 29, 31, 37, 41, 43]. Search for the key 2 using Binary
search.
6
7) numList = [2, 3, 5, 7, 10, 11, 12, 17, 19, 23, 29, 31, 37, 41, 43]. Search for the key 15 using Binary
search.
Ans: