日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

快速排序非递归算法c语言实现,数据结构与算法----3.5 非递归的快速排序方法

發(fā)布時(shí)間:2023/12/20 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 快速排序非递归算法c语言实现,数据结构与算法----3.5 非递归的快速排序方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

[c++]代碼庫#include

using namespace std;

#include

using namespace std;

typedef int KeyType;

struct LElemType

{

KeyType key;

};

struct SElemType

{

int a;

int b;

};

struct SList

{

LElemType *r;

int length;

};

const int StackInitSize=10;

const int StackInc=15;

struct SStack

{

SElemType *base,*top;

int stacksize;

};

bool StackInit (SStack &S);//構(gòu)造棧

bool Push(SStack &S,SElemType &e);//入棧

bool Pop(SStack &S,SElemType &e);//出棧

bool StackEmpty(SStack &S);//空棧

bool ListCreate(SList &L,int n,LElemType a[]);//創(chuàng)建順序表

void Listshow(SList &L);//顯示表

void quicksort(SList &L);//非遞歸的快速排序

void InsertSort(SList &L) ;

int Partition(SList &L, int a, int b);

int main()

{

const int i=13;

LElemType a[i]={3,7,1,5,9,6,4,10,15,12,14,19,16};

SList L;

ListCreate(L,i,a);

cout<

Listshow(L);

quicksort(L);

cout<

Listshow(L);

cout<

return 0;

}

bool ListCreate(SList &L,int n,LElemType a[])

{

int i;

L.r=new LElemType[n];

if(!L.r)return false;

L.length=n;

for(i=0;i

{

L.r[i]=a[i];

}

return true;

}

void Listshow(SList &L)

{

int i=L.length;

for(int j=0;j

{

cout<

}

}

bool StackInit (SStack &S)

{

S.base=new SElemType[StackInitSize];

if(!S.base) return false;

S.top=S.base;

S.stacksize=StackInc;

return true;

}

bool StackEmpty(SStack &S)

{

return S.top==S.base;

}

bool Push(SStack &S,SElemType &e)

{

SElemType *base;

if(S.top-S.base==S.stacksize)

{

base=(SElemType*)realloc(S.base,(S.stacksize+StackInc)*sizeof(SElemType));

if(!base) return false;

S.base=base;

S.top=S.base+S.stacksize;

S.stacksize+=StackInc;

}

*S.top=e;

S.top++;

return true;

}

bool Pop(SStack &S,SElemType &e)

{

if(S.top==S.base) return false;

S.top--;

e=*S.top;

return true;

}

int Partition(SList &L, int a, int b)

{

LElemType x = L.r[a];

while (a < b) {

while (a < b && L.r[b].key >= x.key) {

b--;

}

L.r[a] = L.r[b];

while (a < b && L.r[a].key <= x.key) {

a++;

}

L.r[b] = L.r[a];

}

L.r[a] = x; return a;

}

void quicksort(SList &L)

{

SStack S; StackInit(S);

SElemType e, p;

e.a = 0; e.b = L.length-1;

Push(S, e); int t;

while (Pop(S, e)) {

if (e.b-e.a<7) {

continue;

}

t = Partition(L, e.a, e.b);

p.a = e.a; p.b = t - 1;

Push(S, p);

p.a = t + 1; p.b = e.b;

Push(S, p);

}

InsertSort(L);

}

void InsertSort(SList &L)

{

int i, j; LElemType x;

for (i = 1; i < L.length; i++) {

for (x = L.r[i], j = i - 1; j >= 0 && x.key < L.r[j].key; j--) {

L.r[j + 1] = L.r[j];

}

L.r[j + 1] = x;

}

}

總結(jié)

以上是生活随笔為你收集整理的快速排序非递归算法c语言实现,数据结构与算法----3.5 非递归的快速排序方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。