diff --git a/最基本的冒泡排序2 b/最基本的冒泡排序2 new file mode 100644 index 0000000..ddbb5a0 --- /dev/null +++ b/最基本的冒泡排序2 @@ -0,0 +1,42 @@ +#include +void BubbleSort(int n, int a[]) +{ + int low = 0; + int high= n -1; /*设置边界low和high*/ + int temp, j, count = 0; + while (low < high) + { + for(j = low; j < high; j ++, count ++) + /*正向冒泡,找最大值*/ + if (a[j] > a[j + 1]) + { + temp = a[j]; + a[j] = a[j+1]; + a[j+1] = temp; + } + high --; /*更新冒泡区间*/ + for(j = high; j > low; j --, count ++) + /*反向冒泡,找最小值*/ + if (a[j] < a[j - 1]) + { + temp = a[j]; + a[j] = a[j - 1]; + a[j - 1] = temp; + } + low ++; /*更新冒泡区间*/ + } +int main() +{ + int n, a[1000]; + int i, j, temp; + printf("请输入待排序个数:\n"); + scanf("%d", &n); + for(i = 0; i < n; i ++) + scanf("%d", &a[i]); + BubbleSort(n, a); + printf("排序过后的数顺序:\n"); + for(i = 0; i < n - 1; i++) + printf("%d ", a[i]); + printf("%d", a[n - 1]); + return 0; +} \ No newline at end of file