forked from arjunm052/Sorting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelection.c
89 lines (66 loc) · 1.46 KB
/
Selection.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include<time.h>
void swap(long int*,long int*);
void selectionsort(long int arr[],long int n)
{
long int i,j,min;
for(i=0;i<n;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(arr[j]<arr[min])
min=j;
}
swap(&arr[min],&arr[i]);
}
}
void swap(long int *x,long int *y)
{
long
int temp = *x;
*x = *y;
*y = temp;
}
int main ()
{
clock_t t;
FILE *fp;
FILE *fp2;
fp = fopen("data.txt","r");
fp2 = fopen("selection-sort.txt","w");
long int count = 0;
char c;
c = getc(fp);
// Extract characters from file and store in character c
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n') // Increment count if this character is newline
count = count + 1;
fclose(fp);
fp = fopen("data.txt","r");
printf("Linecount: %d\n",count);
long int size = count;
long int arr[size];
long int i;
for(i=0;i<size;i++)
{
fscanf(fp,"%ld",&arr[i]);
}
printf("Destination File : selection-sort.txt\n");
t = clock();
selectionsort(arr,size);
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC;
printf("Sorting took %f seconds to execute \n", time_taken);
t = clock();
for(i=0;i<size;i++)
{
fprintf(fp2,"%ld\n",arr[i]);
}
t = clock() - t;
double time_taken2 = ((double)t)/CLOCKS_PER_SEC;
printf("Writing took %f seconds to execute \n", time_taken2);
fclose(fp);
fclose(fp2);
return 0;
}