Bubble sort Program using C Language
Data Structure Sorting (Article) Sorting (Program)
21
Given Input:
Expected Output:
Program:
#include
#include
#define MAX 10
int list[MAX] = {9,8,7,6,5,4,3,2,1,0};
void display() {
int i;
printf("[");
// navigate through all items
for(i = 0; i < MAX; i++) {
printf("%d ",list[i]);
}
printf("]\n");
}
void bubbleSort() {
int temp;
int i,j;
bool swapped = false;
// loop through all numbers
for(i = 0; i < MAX-1; i++) {
swapped = false;
// loop through numbers falling ahead
for(j = 0; j < MAX-1-i; j++) {
printf(" Items compared: [ %d, %d ] ", list[j],list[j+1]);
// check if next number is lesser than current no
// swap the numbers.
// (Bubble up the highest number)
if(list[j] > list[j+1]) {
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
swapped = true;
printf(" => swapped [%d, %d]\n",list[j],list[j+1]);
}else {
printf(" => not swapped\n");
}
}
// if no number was swapped that means
// array is sorted now, break the loop.
if(!swapped) {
break;
}
printf("Iteration %d#: ",(i+1));
display();
}
}
main() {
printf("Input Array: ");
display();
printf("\n");
bubbleSort();
printf("\nOutput Array: ");
display();
}
Output:
Output below
Explanation:
Input Array: [9 8 7 6 5 4 3 2 1 0 ]
Items compared: [ 9, 8 ] => swapped [8, 9]
Items compared: [ 9, 7 ] => swapped [7, 9]
Items compared: [ 9, 6 ] => swapped [6, 9]
Items compared: [ 9, 5 ] => swapped [5, 9]
Items compared: [ 9, 4 ] => swapped [4, 9]
Items compared: [ 9, 3 ] => swapped [3, 9]
Items compared: [ 9, 2 ] => swapped [2, 9]
Items compared: [ 9, 1 ] => swapped [1, 9]
Items compared: [ 9, 0 ] => swapped [0, 9]
Iteration 1#: [8 7 6 5 4 3 2 1 0 9 ]
Items compared: [ 8, 7 ] => swapped [7, 8]
Items compared: [ 8, 6 ] => swapped [6, 8]
Items compared: [ 8, 5 ] => swapped [5, 8]
Items compared: [ 8, 4 ] => swapped [4, 8]
Items compared: [ 8, 3 ] => swapped [3, 8]
Items compared: [ 8, 2 ] => swapped [2, 8]
Items compared: [ 8, 1 ] => swapped [1, 8]
Items compared: [ 8, 0 ] => swapped [0, 8]
Iteration 2#: [7 6 5 4 3 2 1 0 8 9 ]
Items compared: [ 7, 6 ] => swapped [6, 7]
Items compared: [ 7, 5 ] => swapped [5, 7]
Items compared: [ 7, 4 ] => swapped [4, 7]
Items compared: [ 7, 3 ] => swapped [3, 7]
Items compared: [ 7, 2 ] => swapped [2, 7]
Items compared: [ 7, 1 ] => swapped [1, 7]
Items compared: [ 7, 0 ] => swapped [0, 7]
Iteration 3#: [6 5 4 3 2 1 0 7 8 9 ]
Items compared: [ 6, 5 ] => swapped [5, 6]
Items compared: [ 6, 4 ] => swapped [4, 6]
Items compared: [ 6, 3 ] => swapped [3, 6]
Items compared: [ 6, 2 ] => swapped [2, 6]
Items compared: [ 6, 1 ] => swapped [1, 6]
Items compared: [ 6, 0 ] => swapped [0, 6]
Iteration 4#: [5 4 3 2 1 0 6 7 8 9 ]
Items compared: [ 5, 4 ] => swapped [4, 5]
Items compared: [ 5, 3 ] => swapped [3, 5]
Items compared: [ 5, 2 ] => swapped [2, 5]
Items compared: [ 5, 1 ] => swapped [1, 5]
Items compared: [ 5, 0 ] => swapped [0, 5]
Iteration 5#: [4 3 2 1 0 5 6 7 8 9 ]
Items compared: [ 4, 3 ] => swapped [3, 4]
Items compared: [ 4, 2 ] => swapped [2, 4]
Items compared: [ 4, 1 ] => swapped [1, 4]
Items compared: [ 4, 0 ] => swapped [0, 4]
Iteration 6#: [3 2 1 0 4 5 6 7 8 9 ]
Items compared: [ 3, 2 ] => swapped [2, 3]
Items compared: [ 3, 1 ] => swapped [1, 3]
Items compared: [ 3, 0 ] => swapped [0, 3]
Iteration 7#: [2 1 0 3 4 5 6 7 8 9 ]
Items compared: [ 2, 1 ] => swapped [1, 2]
Items compared: [ 2, 0 ] => swapped [0, 2]
Iteration 8#: [1 0 2 3 4 5 6 7 8 9 ]
Items compared: [ 1, 0 ] => swapped [0, 1]
Iteration 9#: [0 1 2 3 4 5 6 7 8 9 ]
Output Array: [0 1 2 3 4 5 6 7 8 9 ]
Press any key to continue . . .
This Particular section is dedicated to Programs only. If you want learn more about Data Structure. Then you can visit below links to get more depth on this subject.
Program:
#include #include #define MAX 10 int list[MAX] = {9,8,7,6,5,4,3,2,1,0}; void display() { int i; printf("["); // navigate through all items for(i = 0; i < MAX; i++) { printf("%d ",list[i]); } printf("]\n"); } void bubbleSort() { int temp; int i,j; bool swapped = false; // loop through all numbers for(i = 0; i < MAX-1; i++) { swapped = false; // loop through numbers falling ahead for(j = 0; j < MAX-1-i; j++) { printf(" Items compared: [ %d, %d ] ", list[j],list[j+1]); // check if next number is lesser than current no // swap the numbers. // (Bubble up the highest number) if(list[j] > list[j+1]) { temp = list[j]; list[j] = list[j+1]; list[j+1] = temp; swapped = true; printf(" => swapped [%d, %d]\n",list[j],list[j+1]); }else { printf(" => not swapped\n"); } } // if no number was swapped that means // array is sorted now, break the loop. if(!swapped) { break; } printf("Iteration %d#: ",(i+1)); display(); } } main() { printf("Input Array: "); display(); printf("\n"); bubbleSort(); printf("\nOutput Array: "); display(); }
Output:
Output below
Explanation:
Input Array: [9 8 7 6 5 4 3 2 1 0 ] Items compared: [ 9, 8 ] => swapped [8, 9] Items compared: [ 9, 7 ] => swapped [7, 9] Items compared: [ 9, 6 ] => swapped [6, 9] Items compared: [ 9, 5 ] => swapped [5, 9] Items compared: [ 9, 4 ] => swapped [4, 9] Items compared: [ 9, 3 ] => swapped [3, 9] Items compared: [ 9, 2 ] => swapped [2, 9] Items compared: [ 9, 1 ] => swapped [1, 9] Items compared: [ 9, 0 ] => swapped [0, 9] Iteration 1#: [8 7 6 5 4 3 2 1 0 9 ] Items compared: [ 8, 7 ] => swapped [7, 8] Items compared: [ 8, 6 ] => swapped [6, 8] Items compared: [ 8, 5 ] => swapped [5, 8] Items compared: [ 8, 4 ] => swapped [4, 8] Items compared: [ 8, 3 ] => swapped [3, 8] Items compared: [ 8, 2 ] => swapped [2, 8] Items compared: [ 8, 1 ] => swapped [1, 8] Items compared: [ 8, 0 ] => swapped [0, 8] Iteration 2#: [7 6 5 4 3 2 1 0 8 9 ] Items compared: [ 7, 6 ] => swapped [6, 7] Items compared: [ 7, 5 ] => swapped [5, 7] Items compared: [ 7, 4 ] => swapped [4, 7] Items compared: [ 7, 3 ] => swapped [3, 7] Items compared: [ 7, 2 ] => swapped [2, 7] Items compared: [ 7, 1 ] => swapped [1, 7] Items compared: [ 7, 0 ] => swapped [0, 7] Iteration 3#: [6 5 4 3 2 1 0 7 8 9 ] Items compared: [ 6, 5 ] => swapped [5, 6] Items compared: [ 6, 4 ] => swapped [4, 6] Items compared: [ 6, 3 ] => swapped [3, 6] Items compared: [ 6, 2 ] => swapped [2, 6] Items compared: [ 6, 1 ] => swapped [1, 6] Items compared: [ 6, 0 ] => swapped [0, 6] Iteration 4#: [5 4 3 2 1 0 6 7 8 9 ] Items compared: [ 5, 4 ] => swapped [4, 5] Items compared: [ 5, 3 ] => swapped [3, 5] Items compared: [ 5, 2 ] => swapped [2, 5] Items compared: [ 5, 1 ] => swapped [1, 5] Items compared: [ 5, 0 ] => swapped [0, 5] Iteration 5#: [4 3 2 1 0 5 6 7 8 9 ] Items compared: [ 4, 3 ] => swapped [3, 4] Items compared: [ 4, 2 ] => swapped [2, 4] Items compared: [ 4, 1 ] => swapped [1, 4] Items compared: [ 4, 0 ] => swapped [0, 4] Iteration 6#: [3 2 1 0 4 5 6 7 8 9 ] Items compared: [ 3, 2 ] => swapped [2, 3] Items compared: [ 3, 1 ] => swapped [1, 3] Items compared: [ 3, 0 ] => swapped [0, 3] Iteration 7#: [2 1 0 3 4 5 6 7 8 9 ] Items compared: [ 2, 1 ] => swapped [1, 2] Items compared: [ 2, 0 ] => swapped [0, 2] Iteration 8#: [1 0 2 3 4 5 6 7 8 9 ] Items compared: [ 1, 0 ] => swapped [0, 1] Iteration 9#: [0 1 2 3 4 5 6 7 8 9 ] Output Array: [0 1 2 3 4 5 6 7 8 9 ] Press any key to continue . . .
This Particular section is dedicated to Programs only. If you want learn more about Data Structure. Then you can visit below links to get more depth on this subject.