-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.c
More file actions
55 lines (51 loc) · 980 Bytes
/
selectionSort.c
File metadata and controls
55 lines (51 loc) · 980 Bytes
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
# include <stdio.h>
# include <stdlib.h>
void selection_sort(int *arr, int len)
{
int small, temp, i, j, pos;
for(i = 0 ; i < len ; i++)
{
small = arr[i];
pos = i;
for(j = i + 1 ; j < len ; j++)
{
if(small > arr[j])
{
small = arr[j];
pos = j;
}
}
temp = arr[pos];
arr[pos] = arr[i];
arr[i] = temp;
}
}
int main(int argc, char *argv[])
{
if(argc != 3)
{
printf("[*]Usage: selection_sort <Input File Name> <Number of elements>\n");
return 0;
}
int len = atoi(argv[2]), *arr, i;
FILE *fp, *fw;
fp = fopen(argv[1], "r");
fw = fopen("sorted_file.txt", "w");
/*int len;
fseek(fp, 0, SEEK_END);
len = ftell(fp);
rewind(fp);
printf("Length of file: %d\n\n", len);
int arr[len];
int i;*/
arr = (int *)malloc(len * sizeof(int));
for(i = 0 ; i < len ; i++)
fscanf(fp, "%d", &arr[i]);
selection_sort(arr, len);
for(i = 0 ; i < len ; i++)
fprintf(fw, "%d\n", arr[i]);
free(arr);
fclose(fp);
fclose(fw);
return 0;
}