(1) 偶數在奇數前面
(2) 偶數遞減排序、奇數遞增排序
然後輸出這個數列
// Comment #include <stdio.h> #include <stdlib.h> int lessthan(const void * p, const void * q){ return *(int*)p - *(int*)q; } int greaterthan(const void * p, const void * q){ return *(int*)q - *(int*)p; } void eosort(int * arr, int arrSize){ int *a1 = (int*)malloc(sizeof(int)*arrSize); int *a2 = (int*)malloc(sizeof(int)*arrSize); int odd_index = 0; int even_index = 0; int i; for(i = 0; i < arrSize; ++i){ if(arr[i] % 2){ a1[odd_index++] = arr[i]; } else a2[even_index++] = arr[i]; } qsort(a1, odd_index, sizeof(int), lessthan); qsort(a2, even_index, sizeof(int), greaterthan); for(i = 0; i < even_index; ++i){ arr[i] = a2[i]; } for(i = 0; i < odd_index; ++i){ arr[i + even_index] = a1[i]; } } int main(void){ int arr[] = {0, 9, 2, 7, 4, 5, 6, 3, 8, 1, 10}; int arrSize = 11; eosort(arr, arrSize); int i; for(i = 0; i < arrSize; ++i){ printf("%d ", arr[i]); } putchar('\n'); return 0; }
沒有留言:
張貼留言