Sorting and Searching
Sorting and Searching
Sorting and Searching
SEARCHING
Searching
• The basic characteristics of any searching algorithm is that
• searching should be efficient,
• it should have less number of computations involved into it,
• as well as the space occupied by such a technique should be less.
• begin BubbleSort(list)
• Initially, the sorted sublist is empty and the unsorted sublist is the entire
input list.
• The algorithm proceeds by finding the smallest (or largest, depending on
sorting order) element in the unsorted sublist, exchanging it with the
leftmost unsorted element (putting it in sorted order), and moving the
sublist boundaries one element to the right.
Selection Sort
• Technique:
• Consider an array ARR with N elements. The selection sort takes N-1 passes to sort
the entire array and works as follows. First find the smallest value in the array and
place it in the first position. Then find the second smallest value in the array and
place it in the second position. Repeat this procedure until the entire array is sorted.
Therefore,
• In Pass 1, find the position POS of the smallest value in the array and then swap
ARR[POS] and ARR[0]. Thus, ARR[0] is sorted.
• In Pass 2, find the position POS of the smallest value in sub-array of N-1 elements.
Swap ARR[POS] with ARR[1]. Now, A[0] and A[1] is sorted
• In Pass 3, find the position POS of the smallest value in sub-array of N-2 elements.
Swap ARR[POS] with ARR[2]. Now, ARR[0], ARR[1] and ARR[2] is sorted.
• In Pass N-1, find the position POS of the smaller of the elements ARR[N-2] and
ARR[N-1]. Swap ARR[POS] and ARR[N-2] so that ARR[0], ARR[1], … , ARR[N-1] is
sorted.
Selection Sort
Algorithm of Selection Sort
1. BEGIN
2. Get input A[n]
3. Repeat through step 4 for i = 1 to n-1
4. Repeat for j = i + 1, … , n
5. If A[j] < A[i]
temp = A[i]
A[i] = A[min_index]
A[min_index] = temp
END
6. END
THANK YOU
ANY QUESTIONS???