#include <stdio.h>
#include <time.h>
#include <stdlib.h>
typedef int TYPE;
#define N 100
void exchange(TYPE A[], int i, int j){TYPE tmp = A[i]; A[i]=A[j];A[j]=tmp;}
void bubbleSort(TYPE A[], int n)
{
int i, j;
for(i = 0; i < n-1; ++i)
for(j = n-1; j > i; --j)
if(A[j]<A[j-1])
exchange(A, j, j-1);
}
void bubbleSort2(TYPE A[], int n)
{
int i, j;
for(i = n-1; i > 0; --i)
for(j = 0; j < i; ++j)
if(A[j]>A[j+1])
exchange(A, j, j+1);
}
void print(TYPE A[], int n)
{
int i;
for(i = 0; i < n; ++i)
{
printf("%3d", A[i]);
if((i+1)%10==0)
printf("\n");
}
}
bool test(TYPE A[], int n)
{
int i;
bool flag =true;
for(i = 1; i < n; ++i)
if(A[i-1]>A[i])
{
flag = false;
break;
}
return flag;
}
int main(int argc, const char * argv[])
{
srand(time(NULL));
int i;
TYPE A[N];
for(i = 0; i < N; ++i)A[i] = rand()%100;
//bubbleSort(A, N);
bubbleSort2(A, N);
printf("%d\n", test(A, N));
print(A, N);
return 0;
}
冒泡排序bubbleSort
最新推荐文章于 2024-05-19 17:15:17 发布