#include<stdio.h>
#include<stdlib.h>
#define Maxsize 50
void swap(int *a , int *b ){
int temp ;
temp = *a;
*a = *b ;
*b = temp ;
}
void BubbleSort( int A[] , int n ){
int low = 0 , high = n - 1 ;
while( low < high){
for(int i = low ; i < high ; i++){ //从前向后起泡
if(A[i]>A[i+1])
swap( &A[i],&A[i+1]);
}
high--; //更新上界
for(int j = high ; j > low ; j--){ //从后向前起泡
if(A[j]<A[j-1])
swap(&A[j],&A[j-1]);
}
low++; //更新下界
}
}
int main(){
int n ;
int A[Maxsize];
printf("输入顺序表长度:");
scanf("%d",&n);
for(int i = 0 ; i < n ; i++){
scanf("%d",&A[i]);
}
BubbleSort(A,n);
printf("双向起泡排序结果:");
for(int i = 0 ; i < n ; i++){
printf("%d ",A[i]);
}
}
双向起泡排序
猜你喜欢
转载自blog.csdn.net/RRWJ__/article/details/82990857
今日推荐
周排行