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 |
/** * File: sort.c * Author: Risto Heinsar * Created: 29.09.2014 * Last edit: 16.10.2015 * * Description: Program asks the user for 5 numbers and then sorts them in the * ascending order using bubble sort. It outputs the sorted array * in both ascending and descending order without resorting it in * opposing direction */ #include <stdio.h> #define SIZE 5 int main(void) { int i, j, numArray[SIZE], temp; // Read the numbers for (i = 0; i < SIZE; i++) { printf("Enter number %d\n> ", i + 1); scanf("%d", &numArray[i]); } // Sorting for (i = 0; i < SIZE - 1; i++) { for (j = 0; j < SIZE - 1 - i; j++) { if (numArray[j] > numArray[j + 1]) { temp = numArray[j]; numArray[j] = numArray[j + 1]; numArray[j + 1] = temp; } } } // Output in the ascending order printf("\nNumbers in the ascending order: "); for (i = 0; i < SIZE; i++) { printf("%d ", numArray[i]); } // Output in the descending order without sorting it again. printf("\nNumbers in the descending order: "); for (i = SIZE - 1; i >= 0; i--) { printf("%d ", numArray[i]); } return 0; } |