-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex.10.10.c
82 lines (68 loc) · 1.72 KB
/
ex.10.10.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
#include <stdio.h>
#include <stdbool.h>
struct entry
{
char word[15];
char definition[50];
};
int compareStrings (const char s1[], const char s2[])
{
int i = 0;
while ( s1[i] == s2[i] && s1[i] != '\0' ) {
++i;
}
if ( s1[i] == s2[i] ) {
return 0;
} else if ( s1[i] < s2[i] ) {
return -1;
} else {
return 1;
}
}
// the notorious bubble sort
void sortDictionary (int length, struct entry dictionary[length])
{
int i, j;
bool swapped;
for ( i = 1; i < length - 1; ++i ) {
bool swapped = false;
for ( j = 0; j < length - i; ++j ) {
if ( compareStrings (dictionary[j].word, dictionary[j + 1].word) == 1 ) {
struct entry temp = dictionary[j];
dictionary[j] = dictionary[j + 1];
dictionary[j + 1] = temp;
swapped = true;
}
}
if ( !swapped ) {
return;
}
}
}
void printDictionary (int length, struct entry dictionary[length])
{
for ( int i = 0; i < length; ++i ) {
printf ("%-12s %s\n", dictionary[i].word, dictionary[i].definition);
}
}
int main (void)
{
struct entry dictionary[100] =
{
{ "acumen", "mentally sharp; keen" },
{ "abyss", "a bottomless pit" },
{ "agar", "a jelly made from seaweed" },
{ "aardvark", "a burrowing African mammal" },
{ "aerie", "a high nest" },
{ "ajar", "partially opened" },
{ "aigrette", "an ornamental cluster of feathers" },
{ "addle", "to become confused" },
{ "ahoy", "a nautical call of greeting" },
{ "affix", "to append; attach" }
};
printDictionary (10, dictionary);
printf ("\nSorting...\n\n");
sortDictionary (10, dictionary);
printDictionary (10, dictionary);
return 0;
}