Add 最基本的冒泡排序2

master
pql5ifepn 4 years ago
parent d8e946845e
commit 3a4c320016

@ -0,0 +1,42 @@
#include<stdio.h>
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;
}
Loading…
Cancel
Save