Commit | Line | Data |
---|---|---|
de32237f JB |
1 | #include <stdio.h> |
2 | #include <stdbool.h> | |
3 | ||
b77c2eee | 4 | //FIXME: Comment the code !!! |
de32237f | 5 | |
b77c2eee | 6 | void promptValue(int* addr) { |
de32237f JB |
7 | scanf("%d", addr); |
8 | } | |
9 | ||
fdfaa04d | 10 | // The efficiency of this swap alternative is debatable .. |
de32237f JB |
11 | void xorSwap (int *v1, int *v2) { |
12 | if (v1 != v2) { | |
13 | *v1 ^= *v2; | |
14 | *v2 ^= *v1; | |
15 | *v1 ^= *v2; | |
16 | } | |
17 | } | |
18 | ||
19 | void swap(int* v1, int* v2) { | |
2f54ac48 JB |
20 | int tmp = *v1; |
21 | *v1 = *v2; | |
22 | *v2 = tmp; | |
de32237f JB |
23 | } |
24 | ||
25 | void displayArray(int* array, int count) { | |
26 | for (int i = 0; i < count; i++) { | |
b77c2eee | 27 | printf("Value in array at index[%d]= %d\n", i, array[i]); |
de32237f JB |
28 | } |
29 | } | |
30 | ||
31 | bool sortFirst(int* array, int length) { | |
b77c2eee | 32 | bool rt = false; |
9bd61970 JB |
33 | // This loop could probably be replaced by a while loop with conditions |
34 | // on the array values permutation AND the iteration value, later ... | |
de32237f JB |
35 | for (int i = 0; i < length-1; i++) { |
36 | if (array[i] > array[i+1]) { | |
37 | swap(&array[i], &array[i+1]); | |
b77c2eee | 38 | //xorSwap(&array[i], &array[i+1]); |
9bd61970 | 39 | if (!rt) { rt = true; }; |
de32237f JB |
40 | } |
41 | } | |
b77c2eee JB |
42 | return rt; |
43 | } | |
44 | ||
45 | void sortArray(int* array, int length) { | |
46 | bool rt; | |
47 | do { | |
48 | rt = sortFirst(array, length); | |
49 | } while (rt); | |
de32237f JB |
50 | } |
51 | ||
52 | int main() { | |
b77c2eee | 53 | int tab_length = 10; |
39ba1592 JB |
54 | int tab[tab_length]; |
55 | for (int i = 0; i < tab_length; i++) { | |
56 | tab[i] = 0; | |
57 | } | |
b77c2eee JB |
58 | |
59 | for (int i = 0; i < tab_length; i++) { | |
60 | printf("Enter integer value at array's index[%d]? ", i); | |
5b3a72f6 JB |
61 | /* En langage C, une ligne doit être terminée par le caractère '\n'. Tant que */ |
62 | /* la ligne n'est pas terminée et que le tampon associé au fichier n'est pas plein, */ | |
63 | /* les caractères transmis ne seront pas effectivement écrits mais tout simplement */ | |
64 | /* placés dans le tampon. On peut cependant forcer le vidage de ce tampon à l'aide */ | |
65 | /* de la fonction fflush. */ | |
66 | fflush(stdout); | |
b77c2eee | 67 | promptValue(&tab[i]); |
de32237f | 68 | } |
b77c2eee JB |
69 | |
70 | printf("\nView array content unsorted:\n"); | |
71 | displayArray(tab, tab_length); | |
b77c2eee | 72 | printf("\nNow, sorting the array...\n"); |
26c30d17 | 73 | sortArray(tab, tab_length); |
b77c2eee JB |
74 | printf("\nView array content sorted:\n"); |
75 | displayArray(tab, tab_length); | |
de32237f JB |
76 | |
77 | return 0; | |
78 | } |