Induction or tail-recursion

选择排序

  1. 遍历整个待排序的数组,从第一个元素开始。
  2. 在未排序的部分中,找到最小(或最大)的元素,并将其与第一个元素交换位置。
  3. 接着从第二个元素开始,重复步骤2,直到所有元素都被排序

迭代版

47228a3a9a734526bf097b91a6174d29.png

递归版

7711da53a5e84b5f8025ace1d72ffcaf.png

Time Complexity

      C(n):  the number of element comparisons 元素比较次数

      Clearly, as n=1, C(1)=0;

      Procedure sort is invoked i=1, and there are n elements in the array. Therefore ,in this call, there are n-1 element comparisons Therefore ,in this call, there are n-1 element comparisons plus the number of element comparisons that result from the recursive call sort(i+1) to sort the array A[2..n].

       We have:

33b876d23b034ece9804c01e8b314f2d.png插入排序

插入排序

1.从第一个元素开始,该元素可以认为已经被排序
2.取下一个元素tem,从已排序的元素序列从后往前扫描
3.如果该元素大于tem,则将该元素移到下一位
4.重复步骤3,直到找到已排序元素中小于等于tem的元素
5.tem插入到该元素的后面,如果已排序所有元素都大于tem,则将tem插入到下标为0的位置
6.重复步骤2~502021cf4b6184c83a163d2bdc82cb228.png

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值