CH-2
Simple Searching
and
sorting algorithms
1
Searching
The process of finding a particular element of an array
Types of Search
✓ Sequential or linear search
✓ Binary search
Why Search & Examples
Looking up a phone number
Accessing a Web site and
Checking the definition of a word in a dictionary
2
1. Linear Search (Sequential Search) Algorithm
Each element of an array is read one by one
sequentially and it is compared with the search key.
Let A be an array of having
Linear Search n elements,
(Sequential Search) Algorithm A[0],A[1],A[2],
...... A[n-1]. “item” is the element to be searched. Then this
algorithm will find the location “loc” of item in A.
Set loc = – 1, if the search is unsuccessful.
3
4
1. Input an array A of n elements and “item” to be searched
and initialize loc = – 1.
2. Initialize i = 0; and repeat through step 3 if (i < n) by
incrementing i by one.
3. If (item == A[i])
loc = i
GOTO step 4
4. If (loc >= 0)
Display “item is found and searching is successful”
5. else
Display “item is not found and searching is unsuccessful”
5
6. exit
int Linear_Search(int list[], int key)
{
int index=0;
int found=0;
do{
if(key==list[index])
found=1;
else
index++;
}while(found==0&&index<n);
if(found==0)
index=-1;
return index; 6
}
2. Binary Search Algorithm
☻ This searching algorithms works only on an ordered list.
The basic idea is:
• Locate midpoint of array to search
• Check if the value in the midpoint is the target value
if value at midpoint matched the desired target value
show flag indicating the presence of the item.
else
Determine if target is in lower half or upper half of an
array.
If in lower half, make this half the array to search
If in the upper half, make this half the array to search 7
8
Loop back to step 1 until the size of the array to
search is one, and this element does not match
the target element, in which case return –1.
The computational time for this algorithm is
proportional to log2 n
. Therefore the time complexity is O(log n)
9
int Binary_Search(int list[],int
key) else
{
left=mid+1;
int index=0;
int n=5; }
int left=0; }while(found==0 &&
int right=n-1; right>=left);
int found=0; if(found==0)
int mid; index=-1;
do{ else
mid=(left+right)/2;
index=mid;
if(key==list[mid])
found=1; return index;
else{ }
if(key<list[mid]) 10
right=mid-1;
int main()
{ cin>>x;
int num[]= {86,37,17,12,9,5,2}; f=0;
int i,x,f,S,E,M;
cout<<"Array: ";
S=0;
for(i=0; i<7; i++) E=6;
{
cout<<num[i]<<" "; return 0;
} }
cout<<"\n\nEnter number to search ";
11
while(S<=E)
{
else if(x<num[M])
M=(S+E)/2;
if(x==num[M])
{
{ S=M+1;
cout<<"Number found at index} "<<M;
f=1; }
break; if(f==0)
} {
else if(x>num[M]) cout<<"Number not found";
{
}
E=M-1;
} 12
Sorting Algorithms
☻ Sorting is one of the most important operations
performed by computers.
☻ Sorting is a process of reordering a list of items in either
increasing or decreasing order. The following are
simple sorting algorithms used to sort small-sized lists.
1) Insertion Sort
2) Selection Sort
Bubble Sort
13
3)
1. Insertion Sort
☻The insertion sort works just like its name suggests - it
inserts each item into its proper place in the final list.
☻The approach is the same approach that you use for
sorting a set of cards in your hand.
☻While playing cards, you pick up a card, start at the
beginning of your hand and find the place to insert the
new card, insert it and move all the others up one place.14
Basic Idea:
Find the location for an element and move all others up, and insert the element.
The process involved in insertion sort is as follows:
1. The left most value can be said to be sorted relative to itself. Thus, we don’t
need to do anything.
2. Check to see if the second value is smaller than the first one. If it is, swap
these two values. The first two values are now relatively sorted.
3. Next, we need to insert the third value in to the relatively sorted portion so
that after insertion, the portion will still be relatively sorted.
4. Remove the third value first. Slide the second value to make room for
insertion. Insert the value in the appropriate position.
5. Now the first three are relatively sorted.
6. Do the same for the remaining items in the list. 15
16
void insertion_sort(int list[]){
int temp;
for(int i=1;i<n;i++){
temp=list[i];
for(int j=i; j>0 && temp<list[j-1];j--)
{ // work backwards through the array finding
where temp should go
list[j]=list[j-1];
list[j-1]=temp;
}//end of inner loop
}//end of outer loop
}//end of insertion_sort
17
2. Selection Sort
Basic Idea:
Loop through the array from i=0 to n-1.
Select the smallest element in the array
from i to n
Swap this value with value at position i.
18
19
void selection_sort(int list[])
{
int i,j, smallest;
for(i=0;i<n;i++){
smallest=i;
for(j=i+1;j<n; j++){
if(list[j]<list[smallest])
smallest=j;
}//end of inner loop
temp=list[smallest];
list[smallest]=list[i];
list[i]=temp;
} //end of outer loop 20
}//end of selection sort
3. Bubble Sort
☻ Bubble sort is the simplest algorithm to
implement and the slowest algorithm on very
large inputs.
Basic Idea:
Loop through array from i=0 to n and swap adjacent
elements if they are out of order.
21
22
void bubble_sort(list[])
{
int i,j,temp;
for(i=0;i<n; i++){
for(j=n-1;j>i; j--){
if(list[j]<list[j-1]){
temp=list[j];
list[j]=list[j-1];
list[j-1]=temp;
}//swap adjacent elements
}//end of inner loop
}//end of outer loop
}//end of bubble_sort 23
End chapter
24