1 /******************************************************************* 2 * > File Name: 01-bubbleSort.c 3 * > Author: fly 4 * > Mail: XXXXXXXX@icode.com 5 * > Create Time: Sun Nov 5 22:34:30 2017 6 ******************************************************************/ 7 8 #include <stdio.h> 9 #include <stdbool.h> 10 11 void show(int a[], int n){ 12 int i; 13 14 for(i = 0; i < n; i++){ 15 printf("%d ", a[i]); 16 } 17 puts(""); 18 } 19 20 void bubble_sort(int a[], int n){ 21 int i, j; 22 int tmp; 23 bool isSorted; 24 25 for(i = 0; i < n - 1; i++){ 26 isSorted = true; 27 for(j = 0; j < n - 1 - i; j++){ 28 if(a[j] > a[j + 1]){ 29 tmp = a[j]; 30 a[j] = a[j+1]; 31 a[j+1] = tmp; 32 isSorted = false; 33 } 34 } 35 if(isSorted) 36 break; 37 show(a, n); 38 } 39 } 40 41 int main(int argc, char* argv[]) 42 { 43 int a[8] = {89,38,11,78,96,44,19,25}; 44 45 show(a, 8); 46 47 bubble_sort(a, 8); 48 49 show(a, 8); 50 51 return 0; 52 }