1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include<stdlib.h> #include<stdio.h>
void Quick_sort(int *Data,int N); void swap(int *A,int *B); void Qsort(int *Data, int Left, int Right); void Insertion_sort(int *Data,int N); int Median3(int *Data, int Left, int Right );
const int Cutoff = 100;
int main() { int i,N,*Data; scanf("%d\n",&N); Data = (int*)malloc(N*sizeof(int)); for(i = 0;i < N;i++) scanf("%d ",&Data[i]); Quick_sort(Data,N); for(i = 0;i < N-1; i++) printf("%d ",Data[i]); printf("%d\n",Data[N-1]); }
void Quick_sort(int *Data,int N) { Qsort( Data, 0, N-1 ); } void swap(int *A,int *B) { int tmp; tmp = *A; *A = *B; *B = tmp; } void Qsort(int *Data, int Left, int Right) { int Pivot, i, j; if( Cutoff <= Right-Left ){ Pivot = Median3( Data, Left, Right ); i = Left; j = Right - 1; while(1) { while( Data[++i] < Pivot ); while( Data[--j] > Pivot ); if( i < j ) swap( &Data[i], &Data[j] ); else break; } swap( &Data[i], &Data[Right-1]); Qsort( Data, Left, i-1 ); Qsort( Data, i+1, Right); } else Insertion_sort(Data+Left,Right-Left+1); } void Insertion_sort(int *Data,int N) { int i,j,tmp; for(i = 1; i < N;i++) { tmp = Data[i]; for(j = i;j > 0 && tmp < Data[j-1];j--) Data[j] = Data[j-1]; Data[j] = tmp; } } int Median3( int *Data, int Left, int Right ) { int center = ( Left + Right ) / 2; if( Data[Left] > Data[center] ) swap( &Data[Left], &Data[center] ); if( Data[Left] > Data[Right] ) swap( &Data[Left], &Data[Right] ); if( Data[center] > Data[Right] ) swap( &Data[center], &Data[Right] ); swap( &Data[center], &Data[Right-1]); return Data[Right-1]; }
|