#include <stdio.h> #define SIZE 8 void bubble_sort( int a[], int n); void bubble_sort( int a[], int n) { int i, j, temp; for (j = 0; j < n - 1; j++) for (i = 0; i < n - 1 - j; i++) { if (a[i] > a[i + 1]) { temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } } } int main() { int number[SIZE] = {95, 45, 15, 78, 84, 51, 24, 12}; int i; bubble_sort(number, SIZE); for (i = 0; i < SIZE; i++) { printf ( "%d" , number[i]); } printf ( "
" ); } |
很奇怪,为什么网上都这样写,虽然这样没问题,我还是觉得冒泡应该是起泡从下向上冒,哈哈,那么想一下,从下网上冒,应该怎么写呢?
Have a nice day!