0% found this document useful (0 votes)
53 views16 pages

Bubble Sort

This document contains C code implementations of several sorting algorithms: bubble sort, selection sort, insertion sort, quick sort, merge sort, and binary search. For each algorithm, the code generates random arrays of integers, sorts the arrays using the given algorithm, times the sorting process, and outputs the sorted array and elapsed time. It allows the user to continuously generate and sort new random arrays.

Uploaded by

Baljinder Singh
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)
53 views16 pages

Bubble Sort

This document contains C code implementations of several sorting algorithms: bubble sort, selection sort, insertion sort, quick sort, merge sort, and binary search. For each algorithm, the code generates random arrays of integers, sorts the arrays using the given algorithm, times the sorting process, and outputs the sorted array and elapsed time. It allows the user to continuously generate and sort new random arrays.

Uploaded by

Baljinder Singh
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/ 16

BUBBLE SORT

#include<stdio.h>
#include<time.h>
#define max 100000

void bubbleSort(int a[],int n)


{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
main()
{
FILE *fp;
float start,end;
int arr[max],n,i,opt;
do
{
printf("Enter the number of elements: ");
scanf("%d",&n);
fp=fopen("input.txt","w");

for(i=0;i<n;i++)
{
fprintf(fp,"%d ",rand()%10);
}
printf("%d numbers written to file successfully.",n);
fclose(fp);
fopen("input.txt","r");
for(i=0;i<n;i++)
{
fscanf(fp,"%d",&arr[i]);
}
start=clock();
bubbleSort(arr,n);
end=clock();
fp=fopen("input.txt","w");
for(i=0;i<n;i++)
{
fprintf(fp,"%d ",arr[i]);
}
printf("\nTotal time taken= %f seconds.",(endstart)/CLOCKS_PER_SEC);
printf("\nPress 1 to continue: ");
scanf("%d",&opt);
}
while(opt==1);

return 0;
}

SELECTION SORT:

#include<stdio.h>
#include<time.h>
#define max 100000

void selectionSort(int a[],int n)


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

main()
{
FILE *fp;
float start,end;
int arr[max],n,i,opt;
do
{
printf("Enter the number of elements: ");

scanf("%d",&n);
fp=fopen("input.txt","w");
for(i=0;i<n;i++)
{
fprintf(fp,"%d ",rand()%100);
}
printf("%d numbers written to file successfully.",n);
fclose(fp);
fopen("input.txt","r");
for(i=0;i<n;i++)
{
fscanf(fp,"%d",&arr[i]);
}
start=clock();
selectionSort(arr,n);
end=clock();
fp=fopen("input.txt","w");
for(i=0;i<n;i++)
{
fprintf(fp,"%d ",arr[i]);
}
printf("\nTotal time taken= %f seconds.",(endstart)/CLOCKS_PER_SEC);
printf("\nPress 1 to continue: ");
scanf("%d",&opt);
}
while(opt==1);
return 0;
}

INSERTION SORT:

#include<stdio.h>
#include<time.h>
# define max 100000

void InsertionSort(int a[],int n)


{
int i,j,temp;
for(i=0;i<n;i++)
{
temp=a[i];
j=i-1;
while((j>=0)&&(a[j]>temp))
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}

main()
{
FILE *fp;
float start,end;
int arr[max],n,i,opt;
do
{
printf("Enter the number of elements: ");
scanf("%d",&n);
fp=fopen("input.txt","w");

for(i=0;i<n;i++)
{
fprintf(fp,"%d ",rand()%100);
}
printf("%d numbers written to file successfully.",n);
fclose(fp);
fopen("input.txt","r");
for(i=0;i<n;i++)
{
fscanf(fp,"%d",&arr[i]);
}
start=clock();
InsertionSort(arr,n);
end=clock();
fclose(fp);
fp=fopen("output.txt","w");
for(i=0;i<n;i++)
{
fprintf(fp,"%d ",arr[i]);
}
fclose(fp);
printf("\nTotal time taken= %f seconds.",(endstart)/CLOCKS_PER_SEC);
printf("\nPress 1 to continue: ");
scanf("%d",&opt);
}
while(opt==1);
return 0;}
QUICK SORT:
#include<stdio.h>
#include<time.h>

# define max 500000

void QuickSort(int a[],int low,int high)


{
int m;
if(low<high)
{
m=partition(a,low,high);
QuickSort(a,low,m-1);
QuickSort(a,m+1,high);
}

int partition(int a[],int low, int high)


{
int i=low,j=high,pivot=a[low],temp;
while(i<j)
{
while(a[i]<=pivot)
i++;
while(a[j]>pivot)
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
a[low]=a[j];

a[j]=pivot;
return j;
}

main()
{
FILE *fp;
float start,end;
int arr[max],n,i,opt;
do
{
printf("Enter the number of elements: ");
scanf("%d",&n);
fp=fopen("input.txt","w");
for(i=0;i<n;i++)
{
fprintf(fp,"%d ",rand()%100);
}
printf("%d numbers written to file successfully.",n);
fclose(fp);
fopen("input.txt","r");
for(i=0;i<n;i++)
{
fscanf(fp,"%d",&arr[i]);
}
start=clock();
QuickSort(arr,0,n-1);
end=clock();
fclose(fp);
fp=fopen("output.txt","w");
for(i=0;i<n;i++)

{
fprintf(fp,"%d ",arr[i]);
}
fclose(fp);
printf("\nTotal time taken= %f seconds.",(endstart)/CLOCKS_PER_SEC);
printf("\nPress 1 to continue: ");
scanf("%d",&opt);
}
while(opt==1);
}

MERGE SORT:
#include<stdio.h>
#include<time.h>
#define max 500000

void MergeSort(int a[],int low, int high)


{
int m;
if(low<high)
{
m=(low+high)/2;
MergeSort(a,low,m);
MergeSort(a,m+1,high);
merge(a,low,high,m);
}
}

void merge(int a[],int low,int high, int m)


{
int i=low, j=m+1, k=low, c[max];
while(i<=m && j<=high)
{
if(a[i]<a[j])
c[k++]=a[i++];
else
c[k++]=a[j++];
}
while(i<=m)
c[k++]=a[i++];
while(j<=high)
c[k++]=a[j++];
for(i=low;i<k;i++)
a[i]=c[i];
}

main()

{
FILE *fp;
float start,end;
int arr[max],n,i,opt;
do
{
printf("Enter the number of elements: ");
scanf("%d",&n);
fp=fopen("input.txt","w");
for(i=0;i<n;i++)
{
fprintf(fp,"%d ",rand()%100);
}
printf("%d numbers written to file successfully.",n);
fclose(fp);
fopen("input.txt","r");
for(i=0;i<n;i++)
{
fscanf(fp,"%d",&arr[i]);
}
start=clock();
MergeSort(arr,0,n-1);
end=clock();
fclose(fp);
fp=fopen("output.txt","w");
for(i=0;i<n;i++)
{
fprintf(fp,"%d ",arr[i]);
}
fclose(fp);
printf("\nTotal time taken= %f seconds.",(end-

start)/CLOCKS_PER_SEC);
printf("\nPress 1 to continue: ");
scanf("%d",&opt);
}
while(opt==1);
}

BINARY SEARCH

#include<stdio.h>
#include<conio.h>
#include<math.h>
void binary(int

a[],int P,int R,int n);

int check(int a[],int q,int n);


void main()
{
int a[8],i,P,R,n;

printf("\n enter the elements in array : \n");


for(i=0;i<8;i++)
{
scanf("%d",&a[i]);
}
P=0;
R=7;
printf("\n enter the number to check :");
scanf("%d",&n);

binary(a,P,R,n);
}

void binary(int

a[],int P,int R,int n)

{
int q,i;
if(P<=R)
{
q=(P+R)/2;
i=check(a,q,n);
if(i==-1)
{
binary(a,P,q-1,n);
}
if(i==1)
{
binary(a,q+1,R,n);
}
}
else
{
printf("\n number not found");
}
}

int check(int a[],int q,int n)


{
if(a[q]==n)
{
printf("\n number found number is : %d \n at index :
%d",n,q);
return(0);

}
else if(a[q]>n)
{
return(-1);
}
else
{
return(1);
}
}

LINEAR SEARCH

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

void linear(int

a[],int P,int R);

void main()
{
clock_t ti;
FILE *fp;

int a[1000],i,P,R,n;

fp=fopen("new.txt","w");
for(i=0;i<1000;i++)
{
n=rand()%1000;

fprintf(fp,"%d\n",n);
}
fclose(fp);

fp=fopen("new.txt","r");
for(i=0;i<1000;i++)
{
fscanf(fp,"%d",&a[i]);
}
fclose(fp);

ti=clock();
P=0;
R=i-1;
linear(a,P,R);
ti=clock()-ti;

double total=((double)ti)/CLOCKS_PER_SEC;
printf("\n%lf",total);
}

void linear(int

a[],int P,int R)

{
int i=0,n,flag=0;

printf("\n enter the element to search : ");


scanf("%d",&n);
for(i=0;i<1000;i++)
{
if(a[i]==n)
{
printf("\n element found element is : %d \n and its
index is : %d",n,i);
flag=1;
}
}
if(flag==0)
{
printf("\n element not found");
}
}

You might also like