[C How To Program] 习题6.32 递归的选择排序

来源:互联网 发布:伊士顿高频交易算法 编辑:程序博客网 时间:2024/06/05 17:52
#include <stdio.h>void selectSortRecur( int *, int , int);void selectSort( int * , int);void printA(int *a, int n);void swap(int *a, int *b);int main(){int a[] = {2,3,1,9,8,4,6,10,5};selectSort(a, sizeof(a) / sizeof(int));printA(a, sizeof(a) / sizeof(int));}void selectSort( int * a, int n){selectSortRecur(a, 0, n - 1);}void selectSortRecur( int *a, int start, int end){if( start >= end){return;}int i, max = a[start], maxi = start;for(i = start + 1; i <= end; i++){if(a[i] > max){max  = a[i];maxi = i;}}swap(&a[start], &a[maxi]);selectSortRecur(a, start + 1, end);}void swap(int *a ,int *b){int temp = *a;*a = *b;*b = temp;}void printA( int *a, int n){int i;for(i = 0; i < n; i++){printf("%d ", a[i]);}printf("\n");}

0 0