write a c program for insertion sort bubble sort and selection sort in a single
ID: 3906188 • Letter: W
Question
write a c program for insertion sort bubble sort and selection sort in a single program and the array size should be more than 100000 and the value of n i.e size should be given by us at the time of output and the values that are to be entered for n size should be random numbers that are to generated by the program for the given n size and for each sorting we must have the time also to be printed in the output individually and the total time for the entire program in the output to be printed.Do Not use pointers
Use pass by value
the output should be:
enter no of elements to be sorted n=
for bubble sort the
Start time=
Stop time=
The total time
for insertion sort
Start time=
Stop time=
The total time=
for selection sort
Start time =
Stop time=
The total time =
the total time for all the sorting= write a c program for insertion sort bubble sort and selection sort in a single program and the array size should be more than 100000 and the value of n i.e size should be given by us at the time of output and the values that are to be entered for n size should be random numbers that are to generated by the program for the given n size and for each sorting we must have the time also to be printed in the output individually and the total time for the entire program in the output to be printed.
Do Not use pointers
Use pass by value
the output should be:
enter no of elements to be sorted n=
for bubble sort the
Start time=
Stop time=
The total time
for insertion sort
Start time=
Stop time=
The total time=
for selection sort
Start time =
Stop time=
The total time =
the total time for all the sorting= write a c program for insertion sort bubble sort and selection sort in a single program and the array size should be more than 100000 and the value of n i.e size should be given by us at the time of output and the values that are to be entered for n size should be random numbers that are to generated by the program for the given n size and for each sorting we must have the time also to be printed in the output individually and the total time for the entire program in the output to be printed.
Do Not use pointers
Use pass by value
the output should be:
enter no of elements to be sorted n=
for bubble sort the
Start time=
Stop time=
The total time
for insertion sort
Start time=
Stop time=
The total time=
for selection sort
Start time =
Stop time=
The total time =
the total time for all the sorting= write a c program for insertion sort bubble sort and selection sort in a single program and the array size should be more than 100000 and the value of n i.e size should be given by us at the time of output and the values that are to be entered for n size should be random numbers that are to generated by the program for the given n size and for each sorting we must have the time also to be printed in the output individually and the total time for the entire program in the output to be printed.
Do Not use pointers
Use pass by value
the output should be:
enter no of elements to be sorted n=
for bubble sort the
Start time=
Stop time=
The total time
for insertion sort
Start time=
Stop time=
The total time=
for selection sort
Start time =
Stop time=
The total time =
the total time for all the sorting=
Explanation / Answer
#include<stdio.h>
#include<time.h>
double bubbleSort(int arr[], int n);
double insertionSort(int arr[], int n);
double selectionSort(int arr[], int n);
main()
{
int array[100000],c;
double tb,ti,ts;
int n= malloc(sizeof(n));
printf("Enter no of elements to be sorted n= ");
scanf("%d",&n);
for(c=n;c>0;c--)
{
array[c]=rand()%100000;
}
tb=bubbleSort(array, n);
for(c=n;c>0;c--)
{
array[c]=rand()%100000;
}
ti=insertionSort(array, n);
for(c=n;c>0;c--)
{
array[c]=rand()%100000;
}
ts=selectionSort(array, n);
printf(" The total time for all the processes=%lf",tb+ti+ts);
}
double bubbleSort(int array[], int n)
{
time_t start,end;
double tb;
int c,d,swap;
start=clock();
printf(" For bubble sort ");
printf(" Start time=%lld", start);
for(c=0;c<n-1;c++)
{
for(d=0;d<n-c-1;d++)
{
if(array[d]>array[d+1])
{
swap=array[d];
array[d]=array[d+1];
array[d+1]=swap;
}
}
}
end=clock();
printf(" End time=%lld", end);
tb=(difftime(end,start)/CLOCKS_PER_SEC);
printf(" The total time= %lf",tb);
return tb;
}
double insertionSort(int arr[], int n)
{
time_t start,end;
double ti;
start=clock();
printf(" For insertion sort ");
printf(" Start time=%lld", start);
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
end=clock();
printf(" End time=%lld", end);
ti=(difftime(end,start)/CLOCKS_PER_SEC);
printf(" The total time= %lf",ti);
return ti;
}
double selectionSort(int arr[], int n)
{
time_t start,end;
double ts;
start=clock();
printf(" For selection sort ");
printf(" Start time=%lld", start);
int i, j, min, temp;
for (i=0; i<n; i++)
{
min = i;
for (j=i+1; j<n; j++)
{
if (arr[j] < arr[min])
min = j;
}
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
end=clock();
printf(" End time=%lld", end);
ts=(difftime(end,start)/CLOCKS_PER_SEC);
printf(" The total time= %lf",ts);
return ts;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.