/* * Bubble-sort program in C * * https://www.programiz.com/dsa/bubble-sort */ #include int data[] = { -2, 45, 0, 11, -9 }; void bubbleSort(int array[], int size) { /* Run loops two times: one for walking throught the array */ /* and the other for comparison */ int step; for (step = 0; step < size - 1; ++step) { int i; for (i = 0; i < size - step - 1; ++i) { /* To sort in descending order, change">" to "<" */ if (array[i] > array[i + 1]) { /* Swap if greater is at the rear position */ int temp = array[i]; array[i] = array[i + 1]; array[i + 1] = temp; } } } } /* Function to print the array */ void printArray(int array[], int size) { int i; for (i = 0; i < size; ++i) { printf("%d ", array[i]); } printf("\n"); } /* Entry point to program */ int main() { int size; size = sizeof(data) / sizeof(data[0]); printf("Unsorted array order:\n"); printArray(data, size); bubbleSort(data, size); printf("Sorted Array in Ascending Order:\n"); printArray(data, size); }