0% found this document useful (0 votes)
8 views29 pages

PPS Unit-V

The document provides an overview of searching and sorting algorithms, focusing on linear and binary search methods, as well as selection, bubble, and insertion sort techniques. It explains the processes involved in each algorithm, their time complexities, and includes example code snippets for implementation. Additionally, it highlights the importance of sorting in organizing data efficiently.

Uploaded by

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

PPS Unit-V

The document provides an overview of searching and sorting algorithms, focusing on linear and binary search methods, as well as selection, bubble, and insertion sort techniques. It explains the processes involved in each algorithm, their time complexities, and includes example code snippets for implementation. Additionally, it highlights the importance of sorting in organizing data efficiently.

Uploaded by

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

Programming for Problem Solving

I B Tech CSE
Unit-V

B Pravalika
Assistant Professor
Department of Computer Science & Engineering
Bhoj Reddy Engineering College for Women

Unit-V 1
Unit-V 2
Searching:

Searching is the process to find the location of a target object in a list of


objects.

There are two basic searches:


Linear Search (Sequential Search)
Binary Search

Linear search can locate an item in any array.


Binary search requires the list (array) to be sorted.

Unit-V 3
Linear Search:

Linear search is used when the list is not ordered.


In linear search, we start searching for the target (key) from the beginning
of the list and continue until we find the target (key) or until we are sure
that it is not in the list.
If the target (key) is found in the list, we say that the search is successful
otherwise the search is unsuccessful.

The time complexity of linear search to search n elements is O(n).

Unit-V 4
Linear Search:

18 23 49 57 32
0 1 2 3 4

Key=23 Search is successful and the key is found at index 1

Key=42 Search is unsuccessful

Unit-V 5
Linear Search: Program
#include<stdio.h>
void linear(int a[ ], int n,int key);
void linear(int a[ ], int n,int key)
void main( )
{
{
int i,flag=0;
int a[20],n,i,key;
for(i=0;i<n;i++)
printf("\nEnter the no. of elements:");
{
scanf("%d",&n);
if(a[i]==key)
printf("\nEnter the elements:");
{
for(i=0;i<n;i++)
flag=1;
scanf("%d",&a[i]);
break;
printf("\nEnter the key to search:");
}
scanf("%d",&key);
}
linear(a,n,key);
if(flag==1)
}
printf("\nKey found at %d position",i+1);
else
printf("\nKey not found in the list");
}

Unit-V 6
Binary Search:

Binary search is used when the list is sorted.


The binary search starts by searching for the target (key) at the middle of
the list (array).
If the target (key) is at the middle, the search completes
Otherwise, if the target (key) is less than the value at middle, the search is
applied only to first half
Otherwise, the search is applied only to second half.

Unit-V 7
Binary Search:
Step-1: If(First>Last) Display Key not found Exit
Else Goto Step-2
Step-2: Calculate Mid= (First+Last)/2
Step-3: If ( Key == a[Mid] )
Display Key found at Mid position
Exit
Else Goto Step-4
Step-4: If(Key<a[Mid]) Last=Mid-1 Goto Step-1
Else First=Mid+1 Goto
Step-1

First Last

0 1 2 3 4 5 6 7 8
12 18 25 32 35 39 45 48 51

Key = 39
Unit-V 8
Binary Search:
First First Last

0 1 2 3 4 5 6 7 8
12 18 25 32 35 39 45 48 51

Key = 39 Mid= ( First + Last) / 2


If ( First > Last ) Display Key not found and Exit
Mid = ( 0 + 8 ) / 2
Mid = 4

If ( Key == a[Mid] ) Display Key found at Mid position


If ( Key < a[Mid] ) Last=Mid-1

If ( Key > a[Mid] ) First=Mid+1

Unit-V 9
Binary Search: Last
First Last

0 1 2 3 4 5 6 7 8
12 18 25 32 35 39 45 48 51

Key = 39 Mid= ( First + Last) / 2


If ( First > Last ) Display Key not found and Exit
Mid = ( 5 + 8 ) / 2
Mid = 6

If ( Key == a[Mid] ) Display Key found at Mid position


If ( Key < a[Mid] ) Last=Mid-1

If ( Key > a[Mid] ) First=Mid+1

Unit-V 10
Binary Search: Last
First

0 1 2 3 4 5 6 7 8
12 18 25 32 35 39 45 48 51

Key = 39 Mid= ( First + Last) / 2


If ( First > Last ) Display Key not found and Exit
Mid = ( 5 + 5 ) / 2
Mid = 5

If ( Key == a[Mid] ) Display Key found at Mid position


If ( Key < a[Mid] ) Last=Mid-1

If ( Key > a[Mid] ) First=Mid+1

Unit-V 11
Binary Search: Example-2

120 158 196 254 287 312 354 435 468 481

If ( First > Last ) Display Key not found and Exit

If ( Key == a[Mid] ) Display Key found at Mid position


If ( Key < a[Mid] ) Last=Mid-1

If ( Key > a[Mid] ) First=Mid+1

Unit-V 12
Binary Search: Non-Recursive void nrbinary(int a[ ],int key,int first,int last)
{
int mid,flag=0;
int main ( )
while(first<=last)
{
{
int a[20],n,i,key,flag=0,first,last,mid;
mid=(first+last)/2;
printf("\nEnter the no. of elements:");
if(key==a[mid])
scanf("%d",&n);
{
printf("\nEnter elements in sorted order:");
printf("\nElement found at position
for(i=0;i<n;i++)
%d",mid+1);
scanf("%d",&a[i]);
flag=1;
printf("\nEnter the element to search:");
break;
scanf("%d",&key);
}
first=0;
else if(key>a[mid])
last=n-1;
first=mid+1;
nrbinary(a,key,first,last);
else
}
last=mid-1;
}
if(flag==0)
{
printf("\nElement not found in the list");
}
Unit-V} 13
Binary Search: Recursive void rbinary(int a[],int key,int first,int last)
{
int mid;
void main() if(first>last)
{ printf("\nElement not found");
int a[20],n,i,key,flag=0,first,last,mid; else
printf("\nEnter the no. of elements:"); {
scanf("%d",&n); mid = (first + last)/2;
printf("Enter elements in sorted order:"); if(key == a[mid])
for(i=0;i<n;i++) printf("The element is at
scanf("%d",&a[i]); position
printf("\nEnter the element to search:"); %d\n",mid+1);
scanf("%d",&key); else if(key < a[mid])
first=0; {
last=n-1; last = mid - 1;
rbinary(a,key,first,last); rbinary(a,key,first,last);
} }
else
{
first = mid + 1;
rbinary(a,key,first,last);
}
Unit-V 14
}
Sorting:

Sorting is the process through which data are arranged according to their
values in increasing or decreasing order.

Some sorting techniques:


Selection sort
Bubble sort
Insertion sort
Quick sort
Merge sort

One concept used by sorting techniques is swapping.

Unit-V 15
Selection sort:

In Selection sort, the list is divided into two sublists, sorted and unsorted.
In selection sort, we find the smallest element from the unsorted sublist and
swap it with the element at the beginning of the unsorted sublist.
After each selection and swapping, the wall between sorted and unsorted
sublists moves one element ahead.
Each time we move one element from unsorted sublist to sorted sublist, we
say that we have completed a sort pass.
A list of n elements requires atmost of n-1 passes to sort.

Unit-V 16
Selection sort: Example 1
Sorted sublist Unsorted sublist

23 78 45 8 32 56 Original list

8 78 45 23 32 56 After pass 1

8 23 45 78 32 56 After pass 2

8 23 32 78 45 56 After pass 3

8 23 32 45 78 56 After pass 4

8 23 32 45 56 78 After pass 5

Unit-V 17
Selection sort: Example 2

36 68 91 86 25 43 72 57 Original list

Unit-V 18
Selection sort: Program void selection(int a[ ], int n)
{
int i,j,k,temp,small;
printf("\nOriginal list: ");
for(k=0;k<n;k++)
int main( ) printf("%d ",a[k]);
{ for(i=0;i<n-1;i++)
int a[20],n,i; {
printf("\nEnter the no. of elements:"); small=i;
scanf("%d",&n); for(j=i+1;j<n;j++)
printf("\nEnter the elements:"); {
for(i=0;i<n;i++) if(a[small]>a[j])
scanf("%d",&a[i]); small=j;
selection(a,n); }
} temp=a[small];
a[small]=a[i];
a[i]=temp;
printf("\nAfter pass %d: ",i+1);
for(k=0;k<n;k++)
printf("%d ",a[k]);
}
Unit-V } 19
Bubble sort:

In Bubble sort, the list is divided into two sublist, sorted and unsorted.
In each pass, the smallest element is bubbled from unsorted sublist and
moved to the sorted sublist.
Or
In each pass, the biggest element is bubbled from unsorted sublist and
moved to the sorted sublist.

A list of n elements requires atmost of n-1 passes to sort.

Unit-V 20
Bubble sort: Example 1
23 78 45 8 32 56 Original list
23 78 8 45 32 56
23 8 78 45 32 56
8 23 78 45 32 56 After pass 1

8 23 32 78 45 56 After pass 2

8 23 32 45 78 56 After pass 3

8 23 32 45 56 78 After pass 4

8 23 32 45 56 78 After pass 5

Unit-V 21
Bubble sort: Example 2

36 68 91 86 25 43 72 57 Original list

Unit-V 22
Bubble sort: Program void bubble(int a[ ], int n)
{
int i,j,k,temp;
printf("\nOriginal list: ");
int main( ) for(k=0;k<n;k++)
{ printf("%d ",a[k]);
int a[20],n,i; for(i=0;i<n-1;i++)
printf("\nEnter the no. of elements:"); {
scanf("%d",&n); for(j=n-1;j>i;j--)
printf("\nEnter the elements:"); {
for(i=0;i<n;i++) if(a[ j ]<a[ j-1 ])
scanf("%d",&a[i]); {
bubble(a,n); temp=a[ j ];
} a[ j ]=a[ j-1 ];
a[ j-1 ]=temp;
}
}
printf("\nAfter pass %d: ",i+1);
for(k=0;k<n;k++)
printf("%d ",a[k]);
}
}
Unit-V 23
Insertion sort:

In Insertion sort, the list is divided into two sublist, sorted and unsorted.
In each pass, the first element of unsorted sublist is picked up and
transferred into the sorted sublist by inserting it at the appropriate place.
Insertion sort starts with one (first) element in the sorted sublist.
A list of n elements requires atmost of n-1 passes to sort.

Unit-V 24
Insertion sort: Example 1

23 78 45 8 32 56 Original list

23 78 45 8 32 56 After pass 1

23 45 78 8 32 56 After pass 2

8 23 45 78 32 56 After pass 3

8 23 32 45 78 56 After pass 4

8 23 32 45 56 78 After pass 5

Unit-V 25
Insertion sort: Example 2

36 68 91 86 25 43 72 57 Original list

Unit-V 26
void insertion(int a[ ], int n)
Insertion sort: Program {
int i,j,k,temp;
printf("\nOriginal list: ");
for(k=0;k<n;k++)
int main( ) printf("%d ",a[k]);
{ for(i=1;i<n;i++)
int a[20],n,i; {
printf("\nEnter the no. of elements:"); for(j=i;j>0;j--)
scanf("%d",&n); {
printf("\nEnter the elements:"); if(a[j]<a[j-1])
for(i=0;i<n;i++) {
scanf("%d",&a[i]); temp=a[j];
insertion(a,n); a[j]=a[j-1];
} a[j-1]=temp;
}
else
break;
}
printf("\nAfter pass %d: ",i);
for(k=0;k<n;k++)
printf("%d ",a[k]);
Unit-V } 27
}
Comparision:

Selection Bubble Insertion

23 78 45 8 32 56 23 78 45 8 32 56 23 78 45 8 32 56
Original

8 78 45 23 32 56 8 23 78 45 32 56 23 78 45 8 32 56 Pass 1

8 23 45 78 32 56 8 23 32 78 45 56 23 45 78 8 32 56 Pass 2

8 23 32 78 45 56 8 23 32 45 78 56 8 23 45 78 32 56 Pass 3

8 23 32 45 78 56 8 23 32 45 56 78 8 23 32 45 78 56 Pass 4

8 23 32 45 56 78 8 23 32 45 56 78 8 23 32 45 56 78 Pass 5

Unit-V 28
Thank You

UNIT-V 29

You might also like