快速排序、冒泡排序、堆排序、shell排序的递归和非递归实现

本文是我粗略写出来的一些测试程序仅在提供简单思路 ,可能一些递归条件还不充分,尽请谅解!

先来介绍最简单的冒泡排序:

一般我们的写法如下(稍微优化后):

void bubble_sort(int a[],int lenth)
{
        if(a==NULL || lenth<1) return ;
        int change=0;
        for(int i=0;i<lenth-1;i++)
        {
                for(int j=lenth-2;j>=i;j--)
                {
                        if(a[j]>a[j+1])
                        {
                                a[j]=a[j]^a[j+1];
                                a[j+1]=a[j]^a[j+1];
                                a[j]=a[j]^a[j+1];
                                change=1;
                        }
                }
                if(change==0)return ;

        }
}
可以看出其中用了两个循环。

下面用一个递归代替一个循环:

//**************************************************
a - 要排序数组头指针
lenth -  数组长度
index -  可以看成非递归方法中的 i 变量
**************************************************/
void bubble_sort_cr(int a[],int& lenth,int index)
{
        if(index<lenth)
        {
              for(int i=lenth-2;i>=index;i--)
              {
                     if(a[i]>a[i+1])
                      {
                              a[i]^=a[i+1];
                              a[i+1]^=a[i];
                              a[i]^=a[i+1];
                      }
             }
                bubble_sort_cr(a,lenth,index+1);
        }
}

可以看出其中只有一个循环。

下面再用一个递归函数代替循环变变量:

/**********************recursion method for bubble sort*******************************
a - 要排序数组头指针
lenth -  数组长度
index -  可以看成非递归方法中的 i 变量
cur - 可以看作一个循环方法中的 i 变量
***************************************************************************************/
void rc_swap(int a[],int& lenth,int& index,int cur)
{       
        if(index<=cur)
        {       
                if(a[cur]>a[cur+1])
                {       
                        a[cur]^=a[cur+1];
                        a[cur+1]^=a[cur];
                        a[cur]^=a[cur+1];
                }
                rc_swap(int a[],lenth,index,cur-1);
        }
}
void bubble_sort_cr(int a[],int& lenth,int index)
{
        if(index<lenth)
        {
                rc_swap(a,lenth,index,lenth-2);
                bubble_sort_cr(a,lenth,index+1);
        }
}

到此可以看出代码中已经没有循环。


下面是快速排序的递归实现:

//这个函数用来分划分,是公共部分
int partition(int a[],int low,int high)
{
        int key=a[low];
        while(low<high)
        {
                while(low<high && key<=a[high])high--;
                a[low]=a[high];
                while(low<high && key>=a[low])low++;
                a[high]=a[low];
        }
        a[low]=key;
        return low;
}
/*****************递归实现*******************************/
/**********recursion method ************/
void quick_sort(int a[],int low,int high)
{
        if(low<high)
        {
                int index=partition(a,low,high);
                quick_sort(a,low,index-1);
                quick_sort(a,index+1,high);
        }
}
/******************非递归实现**************************/
void quick_sort_noCR(int a[],int low,int high)
{
        stack<int> st;


        if(low<high)
        {
                int index=partition(a,low,high);
                if(low<index-1)
                {
                        st.push(low);
                        st.push(index-1);
                }
                if(index+1<high)
                {
                        st.push(index+1);
                        st.push(high);
                }
                while(!st.empty())
                {
                        int t=st.top();
                        st.pop();
                        int s=st.top();
                        st.pop();
                        index=partition(a,s,t);
                        if(s<index-1)
                        {
                                st.push(s);
                                st.push(index-1);
                        }
                        if(index+1<t)
                        {
                                st.push(index+1);
                                st.push(t);
                        }
                }


        }
}

堆排序的递归和非递归体现在对堆的调整过程

//堆排序的递归和非递归一并列出:
#include <iostream>
using namespace std;
typedef void (*HAfun)(int a[],int s,int lenth);
/***************heap sort non recursion*************************
arry - the arrary which we want to sort
s- start index
m- end index
the arry meet with big top heap except arry[s]
so we should adjust the heap to a real big top heap
***************************************************************/
void heapAdjust(int a[],int s,int m)
{
        int rc=a[s];
        for(int j=2*s+1;j<=m;j=2*j+1)
        {
                if(j<m && a[j]<a[j+1]) j++;
                if(rc>a[j])break;
                a[s]=a[j];
                s=j;
        }
        a[s]=rc;
}

/**********************heap srot recursion*****************************
arry - the arrary which we want to sort
s- start index
m- end index
the arry meet with big top heap except arry[s]
so we should adjust the heap to a real big top heap
***************************************************************/
void heapAdjust_rcs(int a[],int s,int& lenth)
{
        int largest=s;
        int left=(s<<1)+1;
        int right=(s<<1)+2;
        if(left<lenth)
        {
                largest = a[left]>a[right]?left:right;
                largest = a[largest]>a[s]?largest:s;
        }
        if(left==lenth)
        {
                largest= a[left]>a[largest]?left:largest;
        }
        if(largest!=s)
        {
                a[s] ^= a[largest];
                a[largest] ^= a[s];
                a[s] ^= a[largest];
                heapAdjust_rcs(a,largest,lenth);
        }

}

/********************************************************************
heapAdjustfun - the function point which can reference to #define 
a - arrary to sort
lenth - the length of arrary
*******************************************************************/
void heapSort(HAfun heapAdjust ,int a[],int& lenth)
{
        for(int i=(lenth-1)/2;i>=0;--i)
        {
                heapAdjust(a,i,lenth-1);
        }
        for(int i=lenth-1;i>=1;--i)
        {
                a[0] ^= a[i];
                a[i] ^= a[0];
                a[0] ^= a[i];
                heapAdjust(a,0,i-1);
        }
}
/************************************************************************/
void print(int a[],int lenth)
{
        for(int i=0;i<lenth;i++)
        {
                cout<<a[i]<<" ";
        }
        cout<<endl;
}

int main()
{
        int a[]={45,54,21,76,8,43,9,0,65,43,23,73,51};
        int lenth=sizeof(a)/sizeof(a[0]);
        print(a,lenth);
        HAfun p=heapAdjust;
        //HAfun p=heapAdjust_rcs;
        heapSort(p,a,lenth);
        print(a,lenth);
        return 0;
}

下面是shell排序:

void shell_sort(int a[],int lenth)
{
        if(a==NULL||lenth<=1)return ;
        for(int step=lenth/2;step>0;step/=2)
        {
                for(int i=step;i<lenth;i++)
                {
                        int tmp=a[i];
                        int j=i-step;
                        for(;j>=0 && tmp<a[j];j-=step)
                        {
                                a[j+step]=a[j];
                        }
                        a[j+step]=tmp;
                }
        }
}
杨辉三角的打印:

//杨辉三角的每一行都符合从n个元素取出 0 - n/2 个元素的组合数的规律
#include <iostream>
using namespace std;
#define LINE 10
//此函数用于计从 n个元素中取出r个元素的组合数
long combi(int n,int r)
{
        if(n<r)return 0;
        long p=1;
        for(int i=1;i<=r;i++)
        {
                p=p*(n-i+1)/i;
        }
        return p;
}
int main(int argv,char* argc[])
{
        for(int n=0;n<LINE;n++)
        {
                for(int r=0;r<=n;r++)
                {
//此判断是为了对其
                        if(r==0)
                        {
                                for(int i=0;i<LINE-n;i++)
                                cout<<" ";
                        }
                        else
                        {
                                cout<<" ";
                        }
                        cout<<combi(n,r);
                }
                cout<<endl;
        }
}


the end !

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值