Unit 2 Searching and Sorting
Unit 2 Searching and Sorting
POLYTECHNIC, KOPARGAON
With NBA ACCREDIATED programs , Approved by AICTE, New Delhi,
Recognized by Govt. of Maharashtra, Affiliated to Maharashtra State Board of Technical Education, Mumbai,
ISO 9001:2015 Certified Institute
Name of Subject:- Data Structures Using 'C‘ MSBTE Subject Code:- 22317
𝑺𝒕𝒂𝒓𝒕 + 𝑬𝒏𝒅
𝑴𝒊𝒅𝒅𝒍𝒆=
2
• Initially, and.
• The key element 55 is to be compared with the Middle value.
• The value at index 6 is 48 and it is smaller than the target value i.e. (55).
• Now if key element 55 is compared with the value at middle, then it is found
that they are equal.
• The key element is searched successfully and it is in 7th location
5 < 6 No change 1 5 6 2 4 3
6 > 2 Interchange 1 5 6 2 4 3
Iteration 1
6 > 4 Interchange 1 5 2 6 4 3
6 > 3 Interchange 1 5 2 4 6 3
5 > 2 Interchange 1 5 2 4 3 6
5 > 3 Interchange 1 2 4 5 3 6
1 2 4 3 5 6
2 < 4 No change 1 2 4 3 5 6
1 2 3 4 5 6
1 2 3 4 5 6
Sanjivani K.B.P. Polytechnic, Kopargaon
Department of Computer Technology
Subject Faculty: Y. A. Pawar
Bubble Sort (Program)
1 11 8 13 21
Swapping
First Iteration
Sanjivani K.B.P. Polytechnic, Kopargaon
Department of Computer Technology
Subject Faculty: Y. A. Pawar
Step 1 Selection Sort
𝒊=𝟎 1 11 8 13 21 Min value at index 2
𝒊=𝟏
1 11 8 13 21 Min value at index 2
1 8 11 13 21
Swapping
Second Iteration
Sanjivani K.B.P. Polytechnic, Kopargaon
Department of Computer Technology
Subject Faculty: Y. A. Pawar
Selection Sort
Step 1
𝒊=𝟏
1 8 11 13 21 Min value at index 2
1 8 11 13 21 Already in Place
Third Iteration
Step 1
Fourth Iteration
5 1 6 2 4 3
1<5
6>1 6>5
1 5 6 2 4 3 No Change in order
1 2 3 4 5 6 Sorted Array
It decrements Rightpos until the item at that position is less than or equal to the pivot value
Leftpos Rightpos
Leftpos Rightpos
Leftpos Rightpos
Leftpos Rightpos
Rightpos
Leftpos
Eventually this stops when the two variables cross paths
The Partition algorithm finishes by swapping the pivot at position last with the item at
Leftpos
Sanjivani K.B.P. Polytechnic, Kopargaon
Department of Computer Technology
Subject Faculty: Y. A. Pawar
Quick Sort
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
Rightpos
Leftpos
Quicksort starts by partitioning the array. For the example above, the result of the first
partition is
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
The pivot value, 70, holds the same position it would if the array were sorted.
All items to the left of it are smaller and all items to the right of it are larger.
Sanjivani K.B.P. Polytechnic, Kopargaon
Department of Computer Technology
Subject Faculty: Y. A. Pawar
Quick Sort
• The pivot value, 70, holds the same position it would if the array were
sorted.
• All items to the left of it are smaller and all items to the right of it are
larger.
• Thus, the 70 is in the right spot and need not be considered further.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
Sorted Array
queue 0 1 2 3 4 5 6 7 8 9
150 12
100 901 82 23 55 77
queue 0 1 2 3 4 5 6 7 8 9
Group all the numbers from queue 0 to queue 9 in the order they have inserted & consider
the list for next step as input list
100, 150, 901, 82, 12, 23, 55, 77
901 55
100 12 23 150 77 82
queue 0 1 2 3 4 5 6 7 8 9
Group all the numbers from queue 0 to queue 9 in the order they have inserted & consider
the list for next step as input list
100, 901, 12, 23, 150, 55, 77, 82
Group all the numbers from queue 0 to queue 9 in the order they have inserted & consider
the list for next step as input list
12, 23, 55, 77, 82, 100, 150, 901
List got sorted in the increasing order