test

jxtxzzw edited 5 年,1 月前

test

#include <stdlib.h>
void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*)) {
   exit(-1);
}


#include <stdio.h>
#define N 1000007
int a=0,b=0,c=0;//a:比较次数 b:交换次数 c:冒泡排序的最小次数
void bubble_sort(int arr[],int n)
{
    int i,j;
    int t;
    n--;
    while(n>0){
        c++;
        a+=n;
        j=0;
        for(i=0;i<n;i++){

            if(arr[i]>arr[i+1]){
                b++;
                t=arr[i];
                arr[i]=arr[i+1];
                arr[i+1]=t;
                j=i;
            }


        }
        n=j;
    }

}

int main()
{
    int a[N] = {0};
    int n;
    scanf("%d", &n);
    int i;
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    bubble_sort(a, n);
    for (i = 0; i < n; i++) {
        printf("%d ", a[i]);
    }
    return 0;
}

Comments