什麼是遞歸選擇排序法?數組
就是先從1位開始將數組中的最大/最小的數找到放在首位。 而後在從第2位開始找把最大/最小的數找到放在第2位,以此類推,直到最後一位,排序完成。ide
- #include <stdio.h>
- #include <math.h>
- int main() {
- void sort(int is[5], int count, int start);
- int i;
- int is[] = { 1, 3, 4, 2, 5 ,8,6,3,2};
- sort(is, 9, 0);
- for (i = 0; i < 9; i++) {
- printf("%d\n", is[i]);
- }
- return 0;
- }
- void sort(int is[], int count, int start) {
- int max, maxIndex = -1, temp, i;
- max = is[start];
- for (i = start; i < count; i++) {
- if (is[i] > max) {
- max = is[i];
- maxIndex = i;
- }
- }
- if (maxIndex > -1) {
- temp = is[maxIndex];
- is[maxIndex] = is[start];
- is[start] = temp;
- }
- if (start < count - 1) {
- sort(is, count, start + 1);
- }
- }