diff options
Diffstat (limited to 'examples/bubsort.c')
| -rw-r--r-- | examples/bubsort.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/examples/bubsort.c b/examples/bubsort.c new file mode 100644 index 0000000..343219e --- /dev/null +++ b/examples/bubsort.c | |||
| @@ -0,0 +1,38 @@ | |||
| 1 | /* sort.c | ||
| 2 | |||
| 3 | Test program to sort a large number of integers. | ||
| 4 | |||
| 5 | Intention is to stress virtual memory system. | ||
| 6 | |||
| 7 | Ideally, we could read the unsorted array off of the file | ||
| 8 | system, and store the result back to the file system! */ | ||
| 9 | #include <stdio.h> | ||
| 10 | |||
| 11 | /* Size of array to sort. */ | ||
| 12 | #define SORT_SIZE 128 | ||
| 13 | |||
| 14 | int | ||
| 15 | main (void) | ||
| 16 | { | ||
| 17 | /* Array to sort. Static to reduce stack usage. */ | ||
| 18 | static int array[SORT_SIZE]; | ||
| 19 | |||
| 20 | int i, j, tmp; | ||
| 21 | |||
| 22 | /* First initialize the array in descending order. */ | ||
| 23 | for (i = 0; i < SORT_SIZE; i++) | ||
| 24 | array[i] = SORT_SIZE - i - 1; | ||
| 25 | |||
| 26 | /* Then sort in ascending order. */ | ||
| 27 | for (i = 0; i < SORT_SIZE - 1; i++) | ||
| 28 | for (j = 0; j < SORT_SIZE - 1 - i; j++) | ||
| 29 | if (array[j] > array[j + 1]) | ||
| 30 | { | ||
| 31 | tmp = array[j]; | ||
| 32 | array[j] = array[j + 1]; | ||
| 33 | array[j + 1] = tmp; | ||
| 34 | } | ||
| 35 | |||
| 36 | printf ("sort exiting with code %d\n", array[0]); | ||
| 37 | return array[0]; | ||
| 38 | } | ||
