-
Notifications
You must be signed in to change notification settings - Fork 0
/
pthread_insertion.c
68 lines (61 loc) · 1.19 KB
/
pthread_insertion.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#define SIZE 10
int num1[SIZE];
struct timeval stop2,start2;
void insertion_sort()
{
int i,j,temp;
for(i = 1; i < SIZE; i++)
{
j = i - 1;
temp = num1[i];
while(j >= 0 && num1[j] > temp)
{
num1[j + 1] = num1[j];
j--;
}
num1[j + 1] = temp;
}
}
void fill_array(int size) {
int i;
srand(time(NULL));
for (i=0; i<size; i++)
num1[i] = rand() % 100;
}
void file_create(double start,double stop)
{
FILE *ptr;
ptr=fopen("Sequencial_Insertion.txt","a+");
if(ptr==NULL)
{
printf("Unable to Open File");
exit(1);
}
else
{
double total= (double)(stop-start);
fprintf(ptr,"Time Taken(Burst Time): %lf\n",total);
fprintf(ptr,".comTime Taken(Clock Time): %lu\n",(stop2.tv_sec-start2.tv_sec)*1000000+stop2.tv_usec-start2.tv_usec);
}
fclose(ptr);
}
int main()
{
fill_array(SIZE);
double Start1=clock();
gettimeofday(&start2,NULL);
insertion_sort();
gettimeofday(&stop2,NULL);
double stop1=clock();
for(int i=0;i<SIZE;i++)
{
printf("%d ",num1[i]);
}
putchar('\n');
file_create(Start1,stop1);
return 0;
}