-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrsort.c
More file actions
60 lines (49 loc) · 1.36 KB
/
strsort.c
File metadata and controls
60 lines (49 loc) · 1.36 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
#include <stdio.h>
#include <string.h> //for strcmp()
#define WORDNUM 7 //how many words there are
#define SIZE 30 //how large the largest word can be - 29 letters
//+1 for the end of string char marker
void selectionSort(char words[WORDNUM][SIZE]);
int main()
{
int i;
char words[WORDNUM][SIZE]={
{"sierra"},
{"hotel"},
{"whiskey"},
{"romeo"},
{"bravo"},
{"juliet"},
{"alpha"}
};
printf("Original order\n");
for(i=0;i<WORDNUM;i++) {
printf("%s\n",words[i]);
}
printf("\nSorted order\n");
selectionSort(words);
for(i=0;i<WORDNUM;i++) {
printf("%s\n",words[i]);
}
printf("\n");
return 0;
}
void selectionSort(char words[WORDNUM][SIZE])
{
int i,j,n=WORDNUM,min;
char temp[SIZE];
for(i = 0; i < n-1 ; i++) {
min = i; //set min to the firt index
for(j = i+1; j < n; j++) {
if ((strcmp(words[j],words[min]))<0) {
min = j;
}
}
if( min != i ) {
//printf("i:%d min:%d word[i]:%c word[min]:%c\n",i,min,word[i],word[min]); getchar();
strcpy(temp,words[i]); //copy into temp, words[i]
strcpy(words[i],words[min]); //copy into words[i], words[min]
strcpy(words[min],temp); //copy into words[min],temp
}
}
}