使用栈实现快排,斐波那契,汉诺塔

原文地址:http://blog.csdn.net/chinainvent/article/details/1317024(写的特别好)

原文地址:http://blog.csdn.net/gf426326/article/details/77983197

快排:

#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vld.h>
//快速排序

//一次划分过程
int Partation(int*arr,int low,int high)//返回值为low与high相等时的下标
{
    int tmp=arr[low];//将第一个数字作为基准
    while(low<high)
    {
        while((low<high) && (arr[high] >=tmp))//从后往前找比基准小的数字往前移
        {
            high--;//没有找到比基准小的数字
        }
        if(low == high)
        {
            break;//直到low与high相等跳出
        }
        else
        {
            arr[low] = arr[high];//将后面小的数字移到前面
        }

        while((low<high) && (arr[low]<=tmp))//从前往后找比基准大的数字
        {
            low++;//没有找到比基准大的数字
        }
        if(arr[low]>tmp)
        {
            arr[high] = arr[low];//将前面的数字往后移
        }
        else
        {
            break;
        }
    }
    arr[low]=tmp;//基准放入
    return low;//返回基准下标

}

//用栈和队列组数对实现快速排序

void QuickSort(int *arr, int len)//最坏情况下时间复杂度为O(n^2)
{
    int tmp=(int)ceil((log((double)len)/log((double)2)));//向上取整;log函数没有满足整型整型的,所以要强转为double类型;
    int *stack=(int *)malloc(sizeof(int)*2*tmp);//sizeof(int)是因为保存的是整型单元;2是因为保存的一个数对,两个数字;
    assert(stack != NULL);

    int top=0;//栈顶指针,下标可取

    int start=0;
    int end=len-1;
    int par=Partation(arr,start,end);

    if(par>start+1)//基准左边至少两个数据
    {
        stack[top++]=start;//入栈左边数据
        stack[top++]=par-1;
    }

    if(par<end-1)//基准右边至少两个数据
    {
        stack[top++]=par+1;//入栈右边数据
        stack[top++]=end;
    }

    while(top>0)//栈不空,处理栈中数据
    {
        //出栈数据
        end=stack[--top];//后进先出
        start=stack[--top];

        par=Partation(arr,start,end);//继续划分

        //如果两边还是大于两个数据,继续循环
        if(par>start+1)//基准左边至少两个数据
        {
            stack[top++]=start;//入栈左边数据
            stack[top++]=par-1;
        }

        if(par+1<end)//基准右边至少两个数据
        {
            stack[top++]=par+1;//入栈右边数据
            stack[top++]=end;
         }
     }

    free(stack);

}


//选择排序
//算法思想:每次从待排序序列中选出最小值,与待排序序列中的第一个值进行交换,已经确定的不参与再次比较,只将待排序数据进行排序
void SelectSort(int *arr,int len)//时复O(n^2),空复O(1),不稳定,   优化——锦标排序/树排序:两两比赛,谁小谁赢
{
    int tmp;
    int minindex;//最小值下标

    for(int i=0;i<len-1;i++)
    {
         int j;
         minindex=i;
         for(j=i+1;j<len;j++)
         {
             if(arr[minindex]>arr[j])//最小值大于待排序的值
             {
                 minindex=j;
             }
         }

         if(minindex!=i)
         {
             tmp=arr[i];
             arr[i]=arr[minindex];
             arr[minindex]=tmp;
         }
    }
}
void Show(int *arr,int len)
{
    for(int i=0;i<len;i++)
    {
        printf("%d ",arr[i]);
    }
    printf("\n");
}
int main()
{
    int arr[]={32,2,65,234,2,76,10,35,66,2,65,3,28,33,11};

    QuickSort(arr,sizeof(arr)/sizeof(arr[0]));//快速排序
    Show(arr,sizeof(arr)/sizeof(arr[0]));
    return 0;

汉诺塔:

#include <iostream>
#include <conio.h>
#include "StackInterface.h"//这个头文件,可以在栈那篇文章中找到,也可以自己用标准库中的stack改一下面的程序即可
using namespace std;
/*
现在我们来定义一个栈的元素类:TaskPaper任务纸
由下属B、C对子任务的分解情况,很容易看出,
可以把任务分成两类:
1、移动n-1个圆盘。这说明,这种任务可以继续分解。
2、移动1个圆盘。这说明,这种任务无法再分,可以执行移动操作。
那么,我们可以定义一个枚举类型,这个枚举类型作为栈元素
的一个数据成员,用来指示到底是继续分解,还是作出移动操作。
*/
enum Flag {toMove, toDecompose};//移动、继续分解
enum Pole {start, goal, temp};    //柱的三种状态,既然起始柱、目标柱、临时柱(也叫辅助柱)

class TaskPaper    //任务纸类,将作为栈的元素类
{
public:
    Flag flg;    //任务分类
    int num;        //任务规模
    Pole A, B, C;    //三根柱

    TaskPaper(int n, Pole a, Pole b, Pole c, Flag f)
    {
        flg = f;
        num = n;
        A = a;        
        B = b;
        C = c;
    }
};

void TOH(int n, Pole s, Pole t, Pole g)//用栈实现的汉诺塔函数
{
    LStack<TaskPaper *> stk;
    stk.push(new TaskPaper(n,s,t,g, toDecompose));    //上司A放第一张任务纸到办公桌上

    TaskPaper * paperPtr;
    long step=0;
    while (stk.pop(paperPtr))    //如果办公桌上有任务纸,就拿最上面的一张来看看
    {
        if (paperPtr->flg == toMove || paperPtr->num == 1)
        {
            ++step;
            if (paperPtr->A == start && paperPtr->B == goal)
            {
                cout<<"第"<<step<<"步:从A柱移动一个圆盘到B柱。"<<endl;
            }
            else if (paperPtr->A == start && paperPtr->C == goal)
            {
                cout<<"第"<<step<<"步:从A柱移动一个圆盘到C柱。"<<endl;
            }
            else if (paperPtr->B == start && paperPtr->A == goal)
            {
                cout<<"第"<<step<<"步:从B柱移动一个圆盘到A柱。"<<endl;
            }
            else if (paperPtr->B == start && paperPtr->C == goal)
            {
                cout<<"第"<<step<<"步:从B柱移动一个圆盘到C柱。"<<endl;
            }
            else if (paperPtr->C == start && paperPtr->A == goal)
            {
                cout<<"第"<<step<<"步:从C柱移动一个圆盘到A柱。"<<endl;
            }
            else if (paperPtr->C == start && paperPtr->B == goal)
            {
                cout<<"第"<<step<<"步:从C柱移动一个圆盘到B柱。"<<endl;
            }
        }
        else
        {
            int num = paperPtr->num;
            Pole a = paperPtr->A;
            Pole b = paperPtr->B;
            Pole c = paperPtr->C;
            
            if (a==start && c==goal) 
            {
                //书中仅写了这一种情况,而后面的五种的情况被作者大意地认为是相同的,
                //于是程序出错了。我估计没有几个人发现这个问题,因为只我这种疑心很重的人,
                //才会按照书中的思路写一遍这种程序^_^
                stk.push(new TaskPaper(num-1, b, a, c, toDecompose));//子任务执行顺序为3
                stk.push(new TaskPaper(1,a,b,c,::toMove));    //子任务中执行顺序为2
                stk.push(new TaskPaper(num-1, a, c, b, toDecompose));//子任务中执行顺序为1
            }
            else if (a==start && b==goal)
            {
                stk.push(new TaskPaper(num-1, c, b, a, toDecompose));//为goal的柱状态不变,其它两根柱的状态互换
                stk.push(new TaskPaper(1,a,b,c,::toMove));    //移动操作中,柱的状态不变
                stk.push(new TaskPaper(num-1, a, c, b, toDecompose));//为start的柱状态不变,其它两根柱的状态互换
            }
            else if (b==start && a==goal)
            {            
                stk.push(new TaskPaper(num-1, a, c, b, toDecompose));
                stk.push(new TaskPaper(1,a,b,c,::toMove));
                stk.push(new TaskPaper(num-1, c, b, a, toDecompose));
            }
            else if (b==start && c==goal)
            {
                
                stk.push(new TaskPaper(num-1, b, a, c, toDecompose));
                stk.push(new TaskPaper(1,a,b,c,::toMove));
                stk.push(new TaskPaper(num-1, c, b, a, toDecompose));
            }
            else if (c==start && a==goal)
            {
                
                stk.push(new TaskPaper(num-1, a, c, b, toDecompose));
                stk.push(new TaskPaper(1,a,b,c,::toMove));
                stk.push(new TaskPaper(num-1, b, a, c, toDecompose));
            }
            else if (c==start && b==goal)
            {
                
                stk.push(new TaskPaper(num-1, c, b, a, toDecompose));
                stk.push(new TaskPaper(1,a,b,c,::toMove));
                stk.push(new TaskPaper(num-1, b, a, c, toDecompose));
            }

        }

        delete paperPtr;
        
    }
}

void main()
{
    
    TOH(3,start,temp,goal);        
    getch();

}
斐波那契:

/*
这是我个人的头文件,用来定义各种函数
文件名:zyk.h
*/
#ifndef ZYK_H
#define ZYK_H
#include <iostream>
#include <stack>
using namespace std;

namespace zyk
{
    //Fibonacci数列的递归函数
    long fibr (int n);

    //用栈实现fibr递归
    long fibr_stack(int n);
}

long zyk::fibr(int n)
{
    if (n == 1 || n == 2)
    { return 1;}
    
    return fibr(n-1) + fibr(n-2);
}

long zyk::fibr_stack(int n)
{
    long val = 0;
    stack<int> stk;
    stk.push(n);      //初始化栈

    while (!stk.empty())    //如果办公桌上不是空的,即有任务纸
    {
        int nn = stk.top();    //查看这张纸
        stk.pop();            //拿开这张纸
        
        if (nn==1 || nn==2)
        {
            val += 1;        //累加。
        }
        else
        {
            stk.push(nn-1);    //分成两个子任务
            stk.push(nn-2);    //对于这两个子任务,其先后执行顺序是无关紧要的。
        }

    }

    return val;
}


#endif



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值