sort (draft)
来源:互联网 发布:青岛知行国际官网 编辑:程序博客网 时间:2024/04/30 07:14
//All Rights Reserved#include <time.h>#include <stdio.h>#include <stdlib.h>#define swap(a,b) { \ (a) ^= (b); \ (b) ^= (a); \ (a) ^= (b); \}#define SIZE 2000int array[SIZE];int swapcnt = 0, worstcnt = 0;void bubbleSort(int a[]);void printArray(int a[]);int main(){int i;for (i=0; i<SIZE; i++)array[i] = i;bubbleSort(array);//printArray(array);return 0;}void bubbleSort(int a[]){int i, j;int begin;begin = clock();for (i=0; i<SIZE; i++)for (j=i+1; j<SIZE; j++){worstcnt++;if (a[i] < a[j]){swap(a[i], a[j]);swapcnt++;}}printf("time elapsed: %d ms\n", clock() - begin);printf("swapcnt: %d, worstcnt: %d\n", swapcnt, worstcnt);}void printArray(int a[]){for (int i=0; i<SIZE; i++){if (i%10 == 0)printf("\n");printf("%2d ", a[i]);}printf("\n");}