c语言递归算法--深入浅出(1)

RECURSION EXERCISES 1

(递归练习1)

前言:资质驽钝,刚刚学习递归的时候总是感觉有点空洞,悬乎。

可能没有足够例题练练手。最近找了几道有难度层次的递归例题,在此分享给大家

  1. Selection Sort(选择排序):A selection sort searches an array looking for the smallest element in the array. When the smallest element is found, it is swapped with the first element of the array. The process is then repeated for the subarray beginning with the second element of the array. Each pass of the array results in one element being placed in its proper location. This sort requires similar processing capabilities to the bubble sort—for an array of n elements, n– 1 passes must be made, and for each subarray, n– 1 comparisons must be made to find the smallest value. When the subarray being processed contains one element, the array is sorted. Write a recursive function selectionSortto perform this algorithm.
    (大意就是要求写一个递归函数  selectionSort 来实现选择排序)
    <span style="color:#333333;">#include <stdio.h>
    
    void selectionSort(int a[], int start, int last);
    
    int main() {
        int a[10];
        int i;
        printf("enter 20 numbers to compare: \n");
    
        for (i = 0; i < 10; i++) {
            scanf("%d", &a[i]);
        }
    
        selectionSort(a, 0, 9);
        for (i = 0; i < 10; i++) {
            printf("%d ", a[i]);
        }
        printf("\n");
        return 0;
    }
    
    void selectionSort(int a[], int start, int last) {
        int min;
        int j;
        int which = start;
        if (start == last) {
            return;
        }
        min = a[start];
        for (j = start; j <= last; j++) {
            if (a[j] < min) {
                min = a[j];
                which = j;
            }
        }
        a[which] = a[start];
        a[start] = min;
    
        selectionSort(a, start + 1, last);
    }</span><span style="color:#cc9933;">
    </span>
    (先码代码,具体讲解等有空再码^_^)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值