Professional Documents
Culture Documents
Selection Sort :
It is also called as push down sorting.
The 1st element of the list is selected.
It is compared repeatedly with all the elements in the list , if any element is found to be lesser
than the selected element , these are swapped.
The list is divided into two sublist sorted and unsorted by an imaginary line.
Each time we move the element to its actual (correct) position that completes one Pass.
If we have n elements, n-1 passes are needed.
Example :
Sorting numbers 2 , 5
2 5
As 2 < 5 No change
2 5
Sorting numbers 5 , 2
5 2
As 5 > 2 Swap
2 5
Hint : Selection Sort - 1. Select one box and compare the value with all the other values.
2. Smallest number comes to its actual position after each pass.
5.1 SEARCHING AND SORTING
There is nothing to compare a[0] with. So we stop and this completes Pass 1.
After Pass 1 Smallest number comes to its actual position
1 7 5 9 4
sorted unsorted
1 4 7 9 5
sorted unsorted
There is nothing to compare a[2] with. So we stop and this completes Pass 3.
After Pass 3, The smallest number in the unsorted list comes to its actual position
1 4 5 9 7
sorted unsorted
There is nothing to compare a[3] with. So we stop and this completes Pass 4.
After Pass 4, The smallest number in the unsorted list comes to its actual position
1 4 5 7 9
sorted unsorted
List that has a single number is always sorted ie in the above example the unsorted list contains
only 9 that means there is no need of sorting it
1 4 5 7 9
sorted
Page 105
5.1 SEARCHING AND SORTING
Bubble Sort :
The simplest and oldest sorting technique is Bubble Sort.This is the most inefficient algorithm.
The method takes two elements at a time. It compare these two elements.
if 1st element is less than 2nd element No change
if 1 element is greater than 2 element Ex change
st nd
After one complete pass the largest element comes to its actual position.
Hint : Bubble Sort - Biggest number comes to its actual position after each pass.
This completes Pass 1.After Pass 1 ,biggest number comes to its actual position
5 1 7 9
unsorted sorted
1 5 7 9
unsorted sorted
1 5 7 9
sorted
The Numbers after sorting are 1 , 5 , 7 , 9.
SEARCHING
Searching is process of finding to location of the key value in the list of element.
In case of array of searching means that we are searching value i.e. key value if found
location(index) is returned.
Example :
1 7 5 9
0 1 2 3
Key – 5
Returns – 2 (as it is found at position 2)
There are two searching techniques that are discussed here
Linear Searching (Sequential search)
Binary Search
Linear Search :
It is most simple searching method. It does not expect the list to be sorted.
Linear Search can even be done on unsorted list.
The key to be searched is compared with each element of list one by one.
If match exists the search is terminated.
If the end of list is reached, it means the search has failed and key is not found.
1 7 5 9 Key - 5
0 1 2 3
Step 1 : Compare key with a[0]
5 with 1 Doesn’t match , move forward.
Step 2 : Compare key with a[1]
5 with 7 Doesn’t match , move forward.
Step 3 : Compare key with a[2]
5 with 5 Matches , stop .
nd
Key is found at 2 position in array.
Page 107
5.1 SEARCHING AND SORTING
Binary Search :
Binary Search , algorithm expects list to be sorted.
Steps to be followed are
Step 1 : Sort the list using Bubble sort or Selection sort
Step 2 : Find mid ie mid = (low+high) / 2 /*low , mid , high are index*/
Step 3 : Compare key with a[mid]
case a : if key == a[mid] Key is found
case b : if key < a[mid] Search in 1st half of list i.e. follow step 2 and 3
case c : if key > a[mid] Search in 2nd half of list i.e. follow step 2 and 3
Page 109
5.1 SEARCHING AND SORTING