PPS Unit-V
PPS Unit-V
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:
Unit-V 3
Linear Search:
Unit-V 4
Linear Search:
18 23 49 57 32
0 1 2 3 4
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:
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
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
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
Unit-V 11
Binary Search: Example-2
120 158 196 254 287 312 354 435 468 481
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.
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.
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:
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