-
Notifications
You must be signed in to change notification settings - Fork 16
/
7.c
44 lines (37 loc) · 765 Bytes
/
7.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 <string.h>
void swap(char **a, char **b)
{
char *temp = *a;
*a = *b;
*b = temp;
}
int strcmp(const char *a, const char *b)
{
return strcmp(a, b);
}
void sort(char **names, int n, int (*cmp)(const char *, const char *))
{
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (cmp(names[i], names[j]) > 0)
{
swap(&names[i], &names[j]);
}
}
}
}
int main()
{
char *names[] = {"John", "Jacob", "Michael", "David", "Peter"};
int n = sizeof(names) / sizeof(names[0]);
sort(names, n, strcmp);
for (int i = 0; i < n; i++)
{
printf("%s ", names[i]);
}
printf("\n");
return 0;
}