You are on page 1of 8

CHAPTER 1

SEARCHING AND SORTING


SORTING
The process through which data are arranged according to their values either in ascending order
or descending order.
Ascending order : The elements are in increasing order.
10 , 20 , 30 , 45
Descending order : The elements are in decreasing order.
45 , 30 , 20 ,10
There are two sorting techniques that are discussed here
 Selection Sort
 Bubble Sort

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

Q) Sort the numbers 5 , 7 , 1 , 9 , 4 using selection sort

Pass 1 : Select a[0] and compare it with other elements


5 7 1 9 4
0 1 2 3 4
Step 1 : Compare a[0] with a [1]
5 < 7  No Change
5 7 1 9 4
Step 2 : Compare a[0] with a [2]
5 < 1  Exchange
1 7 5 9 4
Step 3 : Compare a[0] with a [3]
1 < 9  No Change
1 7 5 9 4
Step 4 : Compare a[0] with a [4]
1 < 4  No Change
1 7 5 9 4

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

Pass 2 : Select a[1] and compare it with other elements


1 7 5 9 4
0 1 2 3 4

Step 1 : Compare a[1] with a [2]


7 < 5  Exchange
1 5 7 9 4
Step 2 : Compare a[1] with a [3]
5 < 9  No Change
1 5 7 9 4
Step 3 : Compare a[1] with a [4]
5 < 4  Exchange
1 4 7 9 5
There is nothing to compare a[1] with. So we stop and this completes Pass 2.
After Pass 2, The smallest number in the unsorted list comes to its actual position

1 4 7 9 5
sorted unsorted

Lecture Notes by Nida Fatima Page 104


5.1 SEARCHING AND SORTING

Pass 3 : Select a[2] and compare it with other elements


1 4 7 9 5
0 1 2 3 4

Step 1 : Compare a[2] with a [3]


7 < 9  No Change
1 4 7 9 5
Step 2 : Compare a[2] with a [4]
7 < 5  Exchange
1 4 5 9 7

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

Pass 4 : Select a[3] and compare it with other elements


1 4 5 9 7
0 1 2 3 4

Step 1 : Compare a[3] with a [4]


9 < 7  Exchange
1 4 5 7 9

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

Thus the Numbers after sorting are 1 , 4 , 5 , 7 , 9.

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.

Q) Sort the numbers 5 , 7 , 1 , 9 using bubble sort


Pass 1 : Compare two elements at a time
5 7 1 9
0 1 2 3
Step 1 : Compare a[0] with a [1]
5 < 7  No Change
5 7 1 9
Step 2 : Compare a[1] with a [2]
7 < 1  Exchange
5 1 7 9
Step 3 : Compare a[2] with a [3]
7 < 9  No Change
5 1 7 9

This completes Pass 1.After Pass 1 ,biggest number comes to its actual position

5 1 7 9
unsorted sorted

Pass 2 : Compare two elements at a time


5 1 7 9
0 1 2 3
Step 1 : Compare a[0] with a [1]
5 < 1  Exchange
1 5 7 9
Step 2 : Compare a[1] with a [2]
5 < 7  No Change
1 5 7 9
This completes Pass 2.After Pass 2 ,biggest number comes to its actual position

1 5 7 9
unsorted sorted

Lecture Notes by Nida Fatima Page 106


5.1 SEARCHING AND SORTING

Pass 3 : Compare two elements at a time


1 5 7 9
0 1 2 3
Step 1 : Compare a[0] with a [1]
1 < 5  No Change
1 5 7 9
This completes Pass 3.After Pass 3 ,biggest number comes to its actual position

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

Example 1 : Search 6 in the list – key = 6


7 1 8 6 4 2 9
0 1 2 3 4 5 6

Step 1: Sort the list


1 2 4 6 7 8 9
0 1 2 3 4 5 6
Step 2 : Find mid
Low = 0
High= 6
Mid = (low + high) / 2
=3
Step 3 : Compare key with a[mid]
6 with 6
Case a : if key == a[mid]  key found

Example 2 : Search 2 in the list – key = 2


7 1 8 6 4 2 9
0 1 2 3 4 5 6

Step 1: Sort the list


1 2 4 6 7 8 9
0 1 2 3 4 5 6
Step 2 : Find mid
Low = 0
High= 6
Mid = (low + high) / 2
=3
Step 3 : Compare key with a[mid]
2 with 6
Case b : if key < a[mid]  Search in 1st half of list i.e. follow step 2 and 3

Lecture Notes by Nida Fatima Page 108


5.1 SEARCHING AND SORTING

The 1st half of the list is


1 2 4
0 1 2

Again Step 2 : Find mid


Low = 0
High= 2
Mid = (low + high) / 2
=1
Step 3 : Compare key with a[mid]
2 with 2
Case a : if key == a[mid]  key found

Example 3 : Search 7 in the list – key = 7


7 1 8 6
0 1 2 3

Step 1: Sort the list


1 6 7 8
0 1 2 3
Step 2 : Find mid
Low = 0
High= 3
Mid = (low + high) / 2
= 1.5  Mid = 1
Step 3 : Compare key with a[mid]
7 with 6
Case c : if key > a[mid]  Search in 2nd half of list i.e. follow step 2 and 3
The 1st half of the list is
7 8
2 3
Again Step 2 : Find mid
Low = 2
High= 3
Mid = (low + high) / 2
= 2.5
Mid = 2
Step 3 : Compare key with a[mid]
7 with 7
Case a : if key == a[mid]  key found

Page 109
5.1 SEARCHING AND SORTING

Lecture Notes by Nida Fatima Page 110

You might also like