快速排序

#include<iostream>
using namespace std;
class List
{
public:
    List(int size);//创建
    ~List();//销毁
    void ClearList();//清空线性表
    bool ListEmpty();//判断是否为空表
    int ListLenght();//求线性表的长度
    int LocateElem(int *e);//
    bool ListInsert(int i,int *e);//在第i个位置插入元素
    bool ListDelete(int i,int *e);//删除第i个位置的元素
    void ListTraverse();//遍历线性表
    int Partition(int low,int high);
    void QSort(int low,int high);
    void QuickSort();
private:
    int *m_pList;
    int m_iSize;//线性表大小
    int m_iLength;//线性表长度
};
List::List(int size)
{
    m_iSize=size;
    m_pList=new int[m_iSize];
    m_iLength=0;
}
List::~List()
{
    delete []m_pList;
    m_pList=NULL;
    cout<<"销毁完成"<<endl;
}
void List::ClearList()
{
    m_iLength=0;
}
bool List::ListEmpty()
{
    if(m_iLength==0)
        return true;
    else
        return false;
}
int List::ListLenght()
{
    return m_iLength;
}
int List::LocateElem(int *e)
{
    for(int i=0;i<m_iLength;i++)
    {
        if(m_pList[i]==*e)
            return i;
    }
    return -1;
}
void List::ListTraverse()
{
    for(int i=1;i<m_iLength;i++)
        cout<<m_pList[i]<<" ";
    cout<<endl;
}
bool List::ListInsert(int i,int *e)
{
    if(i<0||i>m_iLength)
    {
        return false;
    }
    for(int k=m_iLength-1;k>=i;k--)
    {
        m_pList[k+1]=m_pList[k];
    }
    m_pList[i]=*e;
    m_iLength++;
    return true;
}
bool List::ListDelete(int i,int *e)
{
    if(i<0||i>=m_iLength)
    {
        return false;
    }
    *e=m_pList[i];
    for(int k=i+1;k<m_iLength;k++)
    {
        m_pList[k-1]=m_pList[k];
    }
    m_iLength--;
    return true;
}
int List::Partition(int low,int high)
{
    m_pList[0]=m_pList[low];//用子表的第一个数据做基准数
    int pivotkey = m_pList[low];//将基准数保存在pivotkey中
    while(low<high)//长度大于1
    {
        while(low<high&&m_pList[high]>=pivotkey)--high;//将比基准数小的数移到低位
        m_pList[low]=m_pList[high];//交换两个数的位置
        while(low<high&&m_pList[low]<=pivotkey) ++low;//将比基准数大的数移到高位
        m_pList[high]=m_pList[low];//交换两个数的位置
    }
    m_pList[low]=m_pList[0];//基准数归位
    return low;//返回基准数位置
}
void List::QSort(int low,int high)
{
    if(low<high)//长度大于1
    {
        int pivotloc = Partition(low,high);//将数据一分为二,pivotloc是基准数位置
        QSort(low,pivotloc-1);//对左子表递归排序
        QSort(pivotloc+1,high);//对右子表递归排序
    }
}
void List::QuickSort()
{
    QSort(1,ListLenght());
}
int main()
{
    int e1=6,e2=1,e3=2,e4=7,e5=9,e6=3,e7=4,e8=5,e9=10,e10=8,no_use=99;
    List *list1=new List(10);
    list1->ListInsert(0,&no_use);//填坑
    list1->ListInsert(1,&e1);
    list1->ListInsert(2,&e2);
    list1->ListInsert(3,&e3);
    list1->ListInsert(4,&e4);
    list1->ListInsert(5,&e5);
    list1->ListInsert(6,&e6);
    list1->ListInsert(7,&e7);
    list1->ListInsert(8,&e8);
    list1->ListInsert(9,&e9);
    list1->ListInsert(10,&e10);
    list1->ListTraverse();

    list1->QuickSort();//调用快速排序函数
    list1->ListTraverse();

    list1->~List();
    delete list1;
    return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值