Sorting and Searching is one of the most vital topics in DSA. Storing and retrieving information is one of the most common applications of computers nowadays. According to time the amount of data and information stored and accessed via computer has turned to huge databases. So many techniques and algorithms have been developed to efficiently maintain and process information in databases. The process of looking up a particular data record in the database is called searching. The process of ordering the records in a database is called Sorting. Sorting and searching together constitute a major area of study in computational methods. Both of them are very important fields of study in data structure and algorithms. Let us discuss both the topics in detail here.
2. 1. i = 0
2. Read value of key to be searched.
3. if k( i ) == key
Display "Recode found at position"
go to step 7
4. increment k
5. if i < n
go to step 3
6. Display "No match found!"
7. Stop
Algorithm of Sequential / Linear Search
2
3. Binary Search
1. Accept Key
2. Top = 0, Bottom = n-1
3. m=(Top + Bottom) / 2
4. if (key==k(mid))
Display Record found at position mid.
go to step 8
5. if key< k(mid)
else
top=m+1
6. if (top<=bottom)
go to step 3
7. Display Record Found
8. Stop
3
4. Algorithm of Selection Sort
1. Start
2. Pass = 1
3. i = 0
4. j = i+1
5. if x[i] > x[j]
interchange x[i] and x[i+1]
6. j = j+1
7. if j < = n-1
Go to step 5
8. Pass = Pass + 1
9. if pass < = n-1
then i = i+1 and go to step 4
10. Stop
4
5. Algorithm of Radix Sort
This method is based on the value of actual digits in each position of the numbers being sorted.
Example: 132 has 2 in the unit's position , 3 in the ten's position and 1 in the hundred's position.
Start comparing from the most significant digits and move towards the last digit as long as the
digits are equal. if there is no match, the number with digit is the greater number.
This method used the above techniques.
1. The number are partitioned into ten groups based on their unit's digit.
2. Elements within individual groups are sorted on ten's digit.
3. The process is repeated till each sub group has been subdivided an most significant digit.
in the above method, a lot of partitions have to be created which complexity.
Comparing two numbers of equal length
5
9. 1. Start
2. A is an array of 'n' elements.
3. lb=0, ub = n-1 ( ub → upper bound, lb → lower bound)
4. if ( lb < ub ) i.e. if the array can be partitioned.
j = partition (A, lb, j-1) // j is the pivot position
Quicksort (A, lb, j-1) // sort the first partition
Quicksort (A, j+1, ub ) // sort the second partition
5. Stop
Algorithm of Quick Sort
9
10. Pivot position = up
First sub array = 23 7 48 32 18
Second partition = 82 62
[55] 7 48 32 18 23 82 62
Partition I Partition II
Pivot = A [lb] = 55
Move dn (82 is greater than pivot so stop
incrementing dn)
Move up (23 is less than pivot so stop
decrementing up)
Interchange A[dn] and A[up]
updn
Example of Quick Sort
[55] 7 48 32 18 23 82 62
[55] 7 48 32 18 23 82 62
23 7 48 32 18 [55] 82 62
Example : 55, 7, 48, 32, 18, 23, 82, 62
dn
dn
dn
up
up
up
10
11. 1. down = lb
2. up = ub
3. pivot = A[lb]
4. While (A[down] < = pivot && down < ub)
down ++
5. while (A[up] > pivot && up > lb)
up --
6. if (down < up)
interchange A [down] and A [up]
go to step 4
7. interchange A[up] and pivot
8. return up
9. Stop
Algorithm of Quick Sort (Partitions)
11
12. [23] 7 48 32 18
[23] 7 48 32 18
[23] 7 48 32 18
[23] 7 18 32 48
[23] 7 18 32 48
18 7 [23] 32 48
[23] 7 18 32 48
Partition I Partition II
Pivot = A [lb] = 23
Move dn (48 is greater than pivot - 23
so stop incrementing dn)
Move up (18 is less than pivot - 23
so stop decrementing up)
Interchange A[dn] and A[up]
Move dn
Move up
Interchange A[up] and pivot
updn
dn
dn
dn
dn
dn
up
up
up
up
dn up
up
Example of Quick Sort (Partitions)
Example: 55, 7, 48, 32, 18, 23, 82, 62
12
13. Algorithm of Insertions Sort
• In the first pass the 1st element 7 is compared with 0th element 15, 7 is inserted
at 0th place, since 7 is smaller than 15. The 0th element. i.e. 15 is moved one
position to the right.
• In the second pass, the 2nd element 22 is compared with 0th element 7. hence 7
is smaller than 22 then no change. Then 2nd element 22 is compared with 1st
element 15 and again no change.
• In the third pass 3rd element is compared with 0th element. As 3 is smaller than
7, 3 is inserted at 0th place and all the elements from 0th till 2nd position are
moved to right by one position.
• This procedure is repeated for all the element of array. In the last pass array gets
sorted.
13
15. Algorithm of Bubble Sort
1. Start
2. Pass = 1
3. i = 0
4. If x[i] > x(i + 1)
interchange x[i] and x[I + 1]
5. i = i + 1
6. If i<=n-1- Pass
go to Step 4
7. Pass =Pass + 1
8. If Pass < = n-1
go to Step 3
9. Stop
15