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

Lab Exercise - 3: Prog1:-Write A Program To Implement Bubble Sort

The document discusses four sorting algorithms: bubble sort, selection sort, binary search, and merge sort. For each algorithm, it provides pseudocode for the algorithm and a C program implementing that algorithm.

Uploaded by

kuku288
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 views6 pages

Lab Exercise - 3: Prog1:-Write A Program To Implement Bubble Sort

The document discusses four sorting algorithms: bubble sort, selection sort, binary search, and merge sort. For each algorithm, it provides pseudocode for the algorithm and a C program implementing that algorithm.

Uploaded by

kuku288
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/ 6

LAB EXERCISE - 3

Prog1:- Write a program to implement Bubble Sort.


BubbleSort(arr)
{
for(i arr.length-1 downto 0)
{ for(j 0 to i)
{
if(arr[j] > arr[j+1])
{
temp arr[j];
arr[j] arr[j+1];
arr[j+1] temp;
} } } }
#include<stdio.h>
#include<conio.h>
void main()
{int a[5],i,j,min,temp,n;
clrscr();
printf("Enter the number of elements in the array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i<n;i++)
{scanf("%d",&a[i]);}
for(i=n-1;i<n;i++)
{min=i;
for(j=(i+1);j<n;j++)
{ if(a[j]<a[min]) {
min=j;}
temp=a[i];
a[i]=a[min];
a[min]=temp;}}
printf("\nThe array after SELECTION SORT is: \n");
for(i=0;i<n;i++)
{printf("%d \t",a[i]);}
getch();}

Prog 2:- Write a program to perform Linear Search


Bool Linear Search(A, x)
{ for i 0 to (n-1)
{
if(A[i]=x)
return true
else
return false }}
#include<stdio.h>
#include<conio.h>
void main()
{ int i,n,num,flag=0,a[5];
clrscr();
printf("Enter number of elements: ");
scanf("%d",&n);
printf("Enter the numbers: ");
for(i=0;i<n;i++)
{ scanf("%d",&a[i]); }
printf("Enter the element to search");
scanf("%d",&num);
for(i=0;i<n;i++)
{ if(a[i]==num)
{ flag=1;
break;}}
if(flag==1)
printf("Element found at position %d",(i+1));
else
printf("Element not found..!!");
getch();}

Prog 3:- Write a program to perform Binary Search


Bool BinSearch(x, lb, ub)
{ low lb

high ub
m (low + high)/2
if(x=A[m])
return true
break
else if (x<A[m])
BinSearch( x,low,mid-1)
else
BinSearch(x, mid,+1,high) }
#include<stdio.h>
#include<conio.h>
int a[10],m;
int binsearch(int num,int lb,int ub)
{ int low,high,flag=0;
low=lb;
high=ub;
if(low<=high)
{ m=(low+high)/2;
if(num==a[m])
{ flag=1;
return flag; }
else if(num<a[m])
{ return binsearch(num,low,m-1); }
else
{ return binsearch(num,m+1,high); }
}
else
return flag; }
void main()
{int i,n,num,lb,ub,mid,res=0;
clrscr();
printf("Enter the size of the elements: ");
scanf("%d",&n);
printf("Enter the numbers in sorted order: ");
for(i=0;i<n;i++)
{ scanf("%d",&a[i]); }
printf("Enter the element to search: ");
scanf("%d",&num);
lb=0;

ub=n-1;
res=binsearch(num,lb,ub);
if(res==0)
printf("Number not found!!!");
else
printf("Number found at position %d",m+1);
getch();}
Output:-

Prog 4:- Write a program to implement Merge Sort.


Merge(L,R,A){
nl length(L)
nr length( R)
i 0,j 0,k 0
while(i<nl && j<nr) {
if (L[i]<=R[j]) {
A[k] L[i]
i i+1 }
else{ A[k] R[j]
j j+1 }
k k+1 }
while(i< nl){
A[k] L[i]
i i+1,k k+1}
while(j< nr){
A[k] R[j]
j j+1,k k+1}
}
MergeSort(A){
n length(A)
if(n<2) return
mid n/2
left array of size(mid)
right array of size(n-mid)
for i 0 to (mid-1)

left[i] A[i]
for i mid to (n-1)
right[i-mid] A[i]
MergeSort(left)
MergeSort(right)
Merge(left,right,A) }
#include<conio.h>
#include<stdio.h>
int a[10],size;
void mergesort(int a[],int);
void merge(int l[],int r[],int,int,int a[]);
void main()
{ int i;
clrscr();
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter the elements of the array: ");
for(i=0;i<size;i++)
{scanf("%d",&a[i]); }
mergesort(a,size);
printf("Sorted array after applying Merge Sort: ");
for(i=0;i<size;i++)
{printf("%d\t",a[i]); }
getch();}
void mergesort(int a[],int n)
{int left[50],ln,right[50],rn,mid,i;
if(n<2)
return;
mid=n/2;
ln=mid;
rn=n-mid;
for(i=0;i<=mid-1;i++)
{left[i]=a[i]; }
for(i=mid;i<=n-1;i++)
{right[i-mid]=a[i]; }
mergesort(left,ln);
mergesort(right,rn);
merge(left,right,ln,rn,a);}
void merge(int left[],int right[],int ln,int rn,int a[])

{int i,j,k;
i=0;
j=0;
k=0;
while(i<ln&&j<rn)
{if(left[i]<=right[j])
{ a[k]=left[i];
i=i+1;}
else
{a[k]=right[j];
j=j+1;}
k=k+1;
}while(i<ln)
{a[k]=left[i];
i=i+1;
k=k+1;
}
while(j<rn)
{
a[k]=right[j];
j=j+1;
k=k+1;
}
}

You might also like