在不适用循环的条件下,写出冒泡排序。
循环与递归之间可以相互转换,不让使用循环那只能通过递归来代替循环过程了。
搞清楚递归过程中需要改变的变量和递归的结束条件就ok了。
代码比较简单,如下所示:
#include<iostream>
#include<algorithm>
using namespace std;
void BubbleSortNoLoop(int a[],int start,int n)
{
//start:每次比较的第一个数字
//n: 待排序的元素个数
if(start==n-1){
--n;
start=0;
}
if(n==1)
retur