采用希尔排序
1 #include <stdio.h> 2 3 #define MAXN 10 4 typedef float ElementType; 5 6 ElementType Median( ElementType A[], int N ); 7 8 int main () 9 { 10 ElementType A[MAXN]; 11 int N, i; 12 13 scanf("%d", &N); 14 for ( i=0; i<N; i++ ) 15 scanf("%f", &A[i]); 16 printf("%.2f\n", Median(A, N)); 17 18 return 0; 19 } 20 21 ElementType Median( ElementType A[], int N ){ 22 int h = 1; 23 while( h < N/2 ) 24 h = 2 * h + 1; 25 while( h >= 1 ){ 26 for( int i = h ; i < N ; i ++ ){ 27 ElementType e = A[i]; 28 int j; 29 for( j = i ; j >= h && e < A[j-h] ; j -= h ) 30 A[j] = A[j-h]; 31 A[j] = e; 32 } 33 h /= 2; 34 } 35 return A[N/2]; 36 }
- 希尔排序比常用的O(n^2)排序要快,采用其他排序算法会超时
- 间隔可任意设置
- 返回A[N/2]