-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort-names.c
45 lines (41 loc) · 1.08 KB
/
sort-names.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int sort(char** names, int n);
int main() {
int n;
printf("Enter the number of students: ");
scanf("%d", &n);
char** names = (char**)malloc(n * sizeof(char*));
for (int i = 0; i < n; i++) {
printf("Enter the names of %d students:",i);
names[i] = (char*)malloc(50 * sizeof(char));
scanf("%s", names[i]);
}
int swaps = sort(names, n);
printf("The sorted names are:\n");
for (int i = 0; i < n; i++) {
printf("%s\n", names[i]);
}
printf("Number of swaps needed: %d\n", swaps);
for (int i = 0; i < n; i++) {
free(names[i]);
}
free(names);
return 0;
}
int sort(char** names, int n) {
int swaps = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (strcmp(names[i], names[j]) > 0) {
// swap names[i] and names[j]
char* temp = names[i];
names[i] = names[j];
names[j] = temp;
swaps++;
}
}
}
return swaps;
}