- Click below to download Unit 2 programs in pdf file: -
Unit 2
/* 1. Write a program to implement sequential search. */
#include<stdio.h>
void main()
{
int x[10];
int i,se;
for(i=0;i<10;i++)
{
printf("Enter Value [%d]:",i);
scanf("%d",&x[i]);
}
printf("Enter Search Element:");
scanf("%d",&se);
for(i=0;i<10;i++)
{
if(x[i]==se)
{
printf("\nValue found on %d position", i+1);
break;
}
}
if(i==10)
{
printf("\nValue Does not Exists:");
}
}
/* 2. Write a program to implement binary search. */
#include<stdio.h>
void main()
{
int x[10];
int i,beg,end, mid,val;
printf("Enter Array elements in Sorted order:\n");
for(i=0;i<10;i++)
{
printf("Enter Value:");
scanf("%d",&x[i]);
}
printf("\nEnter Search Element:");
scanf("%d",&val);
beg=0;
end=9;
for(mid=(beg+end)/2;beg<=end;mid=(beg+end)/2)
{
if(x[mid]==val)
{
printf("\nValue found on %d position:",mid+1);
break;
}
else if(x[mid]<val)
{
beg=mid+1;
}
else
{
end=mid-1;
}
}
if(beg > end)
{
printf("\nValue does not Exists:");
}
}
/* 3. Write a program to implement bubble sort. */
#include<stdio.h>
void main()
{
int x[5];
int i,j,tmp;
for(i=0;i<5;i++)
{
printf("Enter Value:");
scanf("%d",&x[i]);
}
printf("\n Array Before Sorting:\n");
for(i=0;i<5;i++)
printf("\t%d",x[i]);
for(i=0;i<5;i++)
{
for(j=0;j<5-i;j++)
{
if(x[j]>x[j+1])
{
tmp=x[j];
x[j]=x[j+1];
x[j+1]=tmp;
}
}
}
printf("\n Array After Sorting:\n");
for(i=0;i<5;i++)
printf("\t%d",x[i]);
}
/* 4. Write a program to implement selection sort */
#include<stdio.h>
void main()
{
int x[5];
int i,j,tmp;
for(i=0;i<5;i++)
{
printf("Enter Value:");
scanf("%d",&x[i]);
}
printf("\n Array Before Sorting:\n");
for(i=0;i<5;i++)
printf("\t%d",x[i]);
for(i=0;i<4;i++)
{
for(j=i+1;j<5;j++)
{
if(x[i]>x[j])
{
tmp=x[i];
x[i]=x[j];
x[j]=tmp;
}
}
}
printf("\n Array After Sorting:\n");
for(i=0;i<5;i++)
printf("\t%d",x[i]);
}
/* 5. Write a program to implement merge sort */
#include<stdio.h>
void main()
{
int x[10],i,j,k,n;
printf("Enter Number of Elements you want to Enter:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Element for X[%d]",i);
scanf("%d",&x[i]);
}
printf("\nArray Before Sorting:\n");
for(i=0;i<n;i++)
printf("\t%d",x[i]);
merge_sort(x,0,n-1);
printf("\nArray After Sorting:\n");
for(i=0;i<n;i++)
printf("\t%d",x[i]);
}
void merge_sort(int *x, int beg, int end)
{
int mid;
if(beg<end)
{
mid=(beg+end)/2;
merge_sort(x,beg,mid);
merge_sort(x,mid+1,end);
mrg(x,beg,mid,end);
}
}
void mrg(int *x,int beg, int mid, int end)
{
int i=beg, j=mid+1, k=beg, temp[10];
while((i<=mid) && (j<=end))
{
if(x[i]<x[j])
{
temp[k]=x[i];
i++;
}
else
{
temp[k]=x[j];
j++;
}
k++;
}
if(i>mid)
{
while(j<=end)
{
temp[k]=x[j];
k++;
j++;
}
}
else
{
while(i<=mid)
{
temp[k]=x[i];
i++;
k++;
}
}
for(i=beg;i<k;i++)
{
x[i]=temp[i];
}
}
/* 6. Write a program to implement quick sort */
#include <stdio.h>
int partition (int a[], int start, int end)
{
int pivot = a[end];
int i = (start - 1);
for (int j = start; j <= end - 1; j++)
{
if (a[j] < pivot)
{
i++;
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i+1];
a[i+1] = a[end];
a[end] = t;
return (i + 1);
}
void quick(int a[], int start, int end)
{
if (start < end)
{
int p = partition(a, start, end);
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}
void printArr(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
int main()
{
int a[] = { 20, 14, 4, 18, 34, 64, 25, 33, 11};
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
quick(a, 0, n - 1);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}
/* 7. Write a program to implement insertion sort. */
#include<stdio.h>
void main()
{
int x[5];
int i,j,tmp;
for(i=0;i<5;i++)
{
printf("Enter Value:");
scanf("%d",&x[i]);
}
printf("\n Array Before Sorting:\n");
for(i=0;i<5;i++)
printf("\t%d",x[i]);
for(i=1;i<5;i++)
{
tmp=x[i];
for(j=i;j>0 && x[j-1]>tmp; j--)
x[j]=x[j-1];
x[j]=tmp;
}
printf("\n Array After Sorting:\n");
for(i=0;i<5;i++)
printf("\t%d",x[i]);
}
0 Comments