0% found this document useful (0 votes)
70 views19 pages

What Is Searching?: Terminate The Function

The document discusses different searching and sorting algorithms. It describes linear search, binary search, selection sort, bubble sort, and insertion sort. Linear search compares an element to each element in a list sequentially until a match is found. Binary search only works on sorted lists, and compares an element to the middle element at each step to narrow down the search range. Selection sort finds the minimum element and swaps it into the front at each step. Bubble sort repeatedly swaps adjacent elements that are out of order until the list is fully sorted. Insertion sort inserts each element into the sorted portion of the list by shifting larger elements to make room.

Uploaded by

swetha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views19 pages

What Is Searching?: Terminate The Function

The document discusses different searching and sorting algorithms. It describes linear search, binary search, selection sort, bubble sort, and insertion sort. Linear search compares an element to each element in a list sequentially until a match is found. Binary search only works on sorted lists, and compares an element to the middle element at each step to narrow down the search range. Selection sort finds the minimum element and swaps it into the front at each step. Bubble sort repeatedly swaps adjacent elements that are out of order until the list is fully sorted. Insertion sort inserts each element into the sorted portion of the list by shifting larger elements to make room.

Uploaded by

swetha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

DATA STRUCTURES UNIT – 3

What is Searching?
Searching is a process of finding a value in a list of values. In other words, searching is the process of
locating given value position in a list of values.

Linear Search Algorithm (Sequential Search Algorithm)


Linear search algorithm finds a given element in a list of elements with O(n) time complexity where n
is total number of elements in the list. This search process starts comparing search element with the
first element in the list. If both are matched, then result is element found otherwise search element is
compared with the next element in the list. Repeat the same until search element is compared with the
last element in the list, if that last element also does not match, then the result is "Element not found
in the list". That means, the search element is compared with element by element in the list.
Linear search is implemented using following steps...

 Step 1 - Read the search element from the user.


 Step 2 - Compare the search element with the first element in the list.
 Step 3 - If both are matched, then display "Given element is found!!!" and terminate the function
 Step 4 - If both are not matched, then compare search element with the next element in the list.
 Step 5 - Repeat steps 3 and 4 until search element is compared with last element in the list.
 Step 6 - If last element in the list also doesn't match, then display "Element is not found!!!" and
terminate the function.

Example
Consider the following list of elements and the element to be searched...

1
DATA STRUCTURES UNIT – 3
DATA STRUCTURES UNIT – 3

Binary Search Algorithm


Binary search algorithm finds a given element in a list of elements with O(log n) time complexity
where n is total number of elements in the list. The binary search algorithm can be used with only a
sorted list of elements. That means the binary search is used only with a list of elements that are
already arranged in an order. The binary search cannot be used for a list of elements arranged in
random order. This search process starts comparing the search element with the middle element in the
list. If both are matched, then the result is "element found". Otherwise, we check whether the search
element is smaller or larger than the middle element in the list. If the search element is smaller, then
we repeat the same process for the left sublist of the middle element. If the search element is larger,
then we repeat the same process for the right sublist of the middle element. We repeat this process
until we find the search element in the list or until we left with a sublist of only one element. And if
that element also doesn't match with the search element, then the result is "Element not found in the
list".

Binary search is implemented using following steps...

 Step 1 - Read the search element from the user.


 Step 2 - Find the middle element in the sorted list.
 Step 3 - Compare the search element with the middle element in the sorted list.
 Step 4 - If both are matched, then display "Given element is found!!!" and terminate the function.
 Step 5 - If both are not matched, then check whether the search element is smaller or larger than the
middle element.
 Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for the left
sublist of the middle element.
 Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for the right
sublist of the middle element.
 Step 8 - Repeat the same process until we find the search element in the list or until sublist contains
only one element.
 Step 9 - If that element also doesn't match with the search element, then display "Element is not found
in the list!!!" and terminate the function.

Example
Consider the following list of elements and the element to be searched...
DATA STRUCTURES UNIT – 3
DATA STRUCTURES UNIT – 3

What is Sorting?
Sorting is the process of arranging a list of elements in a particular order (Ascending or Descending).

Selection Sort
Selection Sort algorithm is used to arrange a list of elements in a particular order (Ascending or
Descending). In selection sort, the first element in the list is selected and it is compared repeatedly
with all the remaining elements in the list. If any element is smaller than the selected element (for
ascending order), then both are swapped so that first position is filled with the smallest element in the
sorted order. Next, we select the element at a second position in the list and it is compared with all the
remaining elements in the list. If any element is smaller than the selected element, then both are
swapped. This procedure is repeated until the entire list is sorted.

Step by Step Process


The selection sort algorithm is performed using the following steps...

 Step 1 - Select the first element of the list (i.e., Element at first position in the list).
 Step 2 - Compare the selected element with all the other elements in the list.
 Step 3 - In every comparison, if any element is found smaller than the selected element (for ascending
order), then both are swapped.
 Step 4 - Repeat the same procedure with element in the next position in the list till the entire list is sorted.

Example
DATA STRUCTURES UNIT – 3
DATA STRUCTURES UNIT – 3

Program:
#include<iostream.h
> #include<conio.h>
void selection_sort (int list[],int
n); void main()
{
int n,i;
int
list[30];
clrscr();
cout<<"Enter no of
elements:"; cin>>n;
cout<<"Enter "<<n<<"
numbers:"; for(i=0;i<n;i++)
cin>>list[i];
selection_sort (list,n);
cout<<"After
sorting:\n";
for(i=0;i<n;i++)
cout<<list[i]<<endl;
getch();
}

void selection_sort (int list[],int n)


{
int temp,i,j;
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{ if(list[i] >
list[j])
{
temp=list[i];
list[i]=list[j]
; list[j]=temp;

Output:
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in wrong order.

Example:
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm
does not swap them.

Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The
algorithm needs one whole pass without any swap to know it is sorted.

Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

Program:

#include<iostream.h>
#include<conio.h>
void bubble_sort(int list[30],int n);
void main()
{
int n,i;
int list[30];
cout<<"Enter No.of elements:";
cin>>n;
cout<<"Enter "<<n<<" numbers:";
for(i=0;i<n;i++)
cin>>list[i];
bubble_sort(list,n);
cout<<"After sorting:\n";
for(i=0;i<n;i++)
cout<<list[i]<<endl;
}

void bubble_sort (int list[30],int n)


{
int temp ;
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(list[j]>list[j+1])
{
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}
}
Insertion Sort
Insertion sort is based on the idea that one element from the input elements is consumed in
each iteration to find its correct position i.e, the position to which it belongs in a sorted array.

It iterates the input elements by growing the sorted array at each iteration. It compares the
current element with the largest value in the sorted array. If the current element is greater,
then it leaves the element in its place and moves on to the next element else it finds its correct
position in the sorted array and moves it to that position. This is done by shifting all the
elements, which are larger than the current element, in the sorted array to one position ahead.

How insertion sort algorithm works?

Explanation

Suppose you want to sort elements in ascending as in above figure. Then,

1. Step 1: The second element of an array is compared with the elements that appear before it
(only first element in this case). If the second element is smaller than first element, second
element is inserted in the position of first element. After first step, first two elements of an
array will be sorted.
2. Step 2: The third element of an array is compared with the elements that appears before it
(first and second element). If third element is smaller than first element, it is inserted in the
position of first element. If third element is larger than first element but, smaller than second
element, it is inserted in the position of second element. If third element is larger than both the
elements, it is kept in the position as it is. After second step, first three elements of an array
will be sorted.

3. Step 3: Similarly, the fourth element of an array is compared with the elements that appears
before it (first, second and third element) and the same procedure is applied and that element
is inserted in the proper position. After third step, first four elements of an array will be
sorted.

Another Example:

Since 7 is the first element and has no other element to be compared with, it remains at its
position. Now when moving towards 4, 7 is the largest element in the sorted list and greater
than 4. So, move 4 to its correct position i.e. before 7. Similarly with 5, as 7 (largest element
in the sorted list) is greater than 5, we will move 5 to its correct position. Finally for 2, all the
elements on the left side of 2 (sorted list) are moved one position forward as all elements are
greater than 2 and then 2 is placed in the first position. Finally, the given array will result in a
sorted array.
Insertion Sort Program:

#include<iostream.h>
#include<conio.h>
void insertion_sort ( int a[ ] , int n);
void main()
{
int list[30],n,i;
clrscr();

cout<<"Enter No.of elements:";


cin>>n;

cout<<"Enter "<<n<<" numbers:";


for(i=0;i<n;i++)
cin>>list[i];

insertion_sort(list,n);

cout<<"After sorting:\n";
for(i=0;i<n;i++)
cout<<list[i]<<endl;

getch();
}

void insertion_sort ( int a[ ] , int n)


{
int temp,pos,i;
for( i = 0 ;i < n ; i++ ) {
/* storing current element whose left side is checked for its
correct position .*/

int temp = a[ i ];
int pos = i;

/* check whether the adjacent element in left side is greater or


less than the current element. */

while( pos > 0 && temp < a[ pos -1]) {

// moving the left side element to one position forward.


a[ pos ] = a[ pos-1];
pos= pos - 1;

}
// moving current element to its correct position.
a[ pos ] = temp;
}
}
Output:
Quick Sort
Quick sort is a fast sorting algorithm used to sort a list of elements. Quick sort algorithm is
invented by C. A. R. Hoare.

The quick sort algorithm attempts to separate the list of elements into two parts and then sort
each part recursively. That means it use divide and conquer strategy. In quick sort, the partition
of the list is performed based on the element called pivot. Here pivot element is one of the
elements in the list.

The list is divided into two partitions such that "all elements to the left of pivot are smaller
than the pivot and all elements to the right of pivot are greater than or equal to the pivot".

Step by Step Process


In Quick sort algorithm, partitioning of the list is performed using following steps...
Step 1 - Consider the first element of the list as pivot (i.e., Element at first position in the list).
Step 2 - Define two variables i and j. Set i and j to first and last elements of the list respectively.
Step 3 - Increment i until list[i] > pivot then stop.
Step 4 - Decrement j until list[j] < pivot then stop.
Step 5 - If i < j then exchange list[i] and list[j].
Step 6 - Repeat steps 3,4 & 5 until i > j.
Step 7 - Exchange the pivot element with list[j] element.

Example
Merge Sort
Merge sort is one of the most efficient sorting algorithms. It works on the principle of Divide
and Conquer. Merge sort repeatedly breaks down a list into several sublists until each sublist
consists of a single element and merging those sublists in a manner that results into a sorted list.

The concept of Divide and Conquer involves three steps:

1. Divide the problem into multiple small problems.


2. Conquer the subproblems by solving them. The idea is to break down the problem into
atomic subproblems, where they are actually solved.
3. Combine the solutions of the subproblems to find the solution of the actual problem.
mid = (0+5)/2 = 2

mid = (0+2)/2 = 1 mid = (3+5)/2 = 4


As one may understand from the image above, at each step a list of size M is being divided
into 2 sublists of size M/2, until no further division can be done. To understand better, consider a
smaller array A containing the elements (9,7,8).

At the first step this list of size 3 is divided into 2 sublists the first consisting of
elements (9,7) and the second one being (8). Now, the first list consisting of elements (9,7) is
further divided into 2 sublists consisting of elements (9) and (7) respectively.

As no further breakdown of this list can be done, as each sublist consists of a maximum
of 1 element, we now start to merge these lists. The 2 sub-lists formed in the last step are then
merged together in sorted order using the procedure mentioned above leading to a new list (7,9).
Backtracking further, we then need to merge the list consisting of element (8) too with this list,
leading to the new sorted list (7,8,9).

You might also like