-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path18-Quick Sort.c
More file actions
88 lines (73 loc) · 2.01 KB
/
18-Quick Sort.c
File metadata and controls
88 lines (73 loc) · 2.01 KB
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
#include <stdio.h>
#include <string.h>
struct Student
{
char name[50];
float height;
int weight;
} student[10];
void swap(struct Student *a, struct Student *b)
{
struct Student temp;
temp = *a;
*a = *b;
*b = temp;
}
int partition(struct Student s[], int low, int high)
{
int i, pivot;
pivot = high;
i = low - 1;
for (int j = low; j <= high; j++)
{
if (strcmp(s[j].name, s[pivot].name) < 0)
{
i++;
swap(&s[i], &s[j]);
}
}
swap(&s[i + 1], &s[pivot]);
return i + 1;
}
void quickSort(struct Student s[], int low, int high)
{
if (low < high)
{
int partitionIndex = partition(s, low, high);
quickSort(s, low, partitionIndex - 1);
quickSort(s, partitionIndex + 1, high);
}
}
void main()
{
int i = 0, n;
char lastName[50];
FILE *fp;
printf("Student details from unsorted file:\n\n");
fp = fopen("unsorted.txt", "r");
while (!feof(fp))
{
fscanf(fp, "%s %s %f %d", student[i].name, lastName, &student[i].height, &student[i].weight);
strcat(student[i].name, " ");
strcat(student[i].name, lastName);
printf("Name: %s\tHeight: %.1f\tWeight: %d\n", student[i].name, student[i].height, student[i].weight);
i++;
}
n = i;
fclose(fp);
quickSort(student, 0, n - 1);
fp = fopen("sorted.txt", "w");
for (i = 0; i < n; i++)
fprintf(fp, "%s %.1f %d\n", student[i].name, student[i].height, student[i].weight);
// fwrite (&student[i], sizeof (student[i]), 1, fp);
fclose(fp);
printf("\nStudent details from sorted file:\n\n");
fp = fopen("sorted.txt", "r");
for (i = 0; i < n; i++)
{
fscanf(fp, "%s %s %f %d", student[i].name, lastName, &student[i].height, &student[i].weight);
// fread (&student[i], sizeof (student[i]), 1, fp);
printf("Name: %s %s\tHeight: %.1f\tWeight: %d\n", student[i].name, lastName, student[i].height, student[i].weight);
}
fclose(fp);
}