Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a c program for insertion sort bubble sort and selection sort in a single

ID: 3905410 • 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

the output should be:

enter no of elements to be sorted n=

for bubble sort the time elapsed=

for insertion sort time elapsed=

for selection sort time elapsed=

the total time for all the processes=

Explanation / Answer

Program

#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);
printf(" For bubble sort the time elapsed= %lf",tb);
for(c=n;c>0;c--)
{
array[c]=rand()%100000;
}
ti=insertionSort(array, n);
printf(" For insertion sort the time elapsed= %lf",ti);
for(c=n;c>0;c--)
{
array[c]=rand()%100000;
}
ts=selectionSort(array, n);
printf(" For selection sort the time elapsed= %lf",ts);


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();
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();
tb=(difftime(end,start)/CLOCKS_PER_SEC);
return tb;
}

double insertionSort(int arr[], int n)
{
time_t start,end;
double ti;

start=clock();
   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();
ti=(difftime(end,start)/CLOCKS_PER_SEC);
return ti;
}
double selectionSort(int arr[], int n)
{
time_t start,end;
double ts;
start=clock();

int i, j, min_idx;

    // One by one move boundary of unsorted subarray
    for (i = 0; i < n-1; i++)
    {
        // Find the minimum element in unsorted array
        min_idx = i;
        for (j = i+1; j < n; j++)
          if (arr[j] < arr[min_idx])
            min_idx = j;

        // Swap the found minimum element with the first element
        swap(&arr[min_idx], &arr[i]);
    }

end=clock();
ts=(difftime(end,start)/CLOCKS_PER_SEC);
return ts;
}

void swap(int *xp, int *yp)
{
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}

Output

Enter no of elements to be sorted n=10000
For bubble sort the time elapsed= 0.353487
For insertion sort the time elapsed= 0.088271
For selection sort the time elapsed= 0.168251

The total time for all the processes=0.610009