Created
May 20, 2021 09:06
-
-
Save czynot/4a19afbc142feb35eeb7c78895f275cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Insertion sort | |
// Number is 20000 to 100000, step 20000 | |
// Perform insertion sort | |
// Output execution time for each | |
#include<stdio.h> | |
#include<stdlib.h> | |
#include<time.h> | |
void sort(int n) | |
{ | |
int i, j, temp, *a; | |
double cpu_time_taken; | |
clock_t start,end; | |
printf("\n Number of input elements : %d",n); | |
a=(int*)malloc(sizeof(int)*n); | |
for(i = 0; i < n; i++) | |
a[i]=rand()%1000; | |
start=clock(); | |
for(i = 1; i < n; i++) | |
{ | |
temp = a[i]; | |
for(j = i; j > 0 && a[j-1] > temp; j--) | |
a[j] = a[j-1]; | |
a[j] = temp; | |
} | |
end=clock(); | |
cpu_time_taken=(double)(end-start)/CLOCKS_PER_SEC; | |
printf("\n CPU time taken: %f",cpu_time_taken); | |
} | |
int main() | |
{ | |
for(int i = 20000; i < 100001; i = i+20000) | |
sort(i); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment