欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > c/c++ >内容正文

c/c++

java 快排非递归_C++ 中快排的递归和非递归实现

发布时间:2025/4/5 c/c++ 64 豆豆
生活随笔 收集整理的这篇文章主要介绍了 java 快排非递归_C++ 中快排的递归和非递归实现 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

快排的递归

void quickSort1(int* root,int low,int high)

{

int pat=root[low];

if(low

{

int i=low,j=high;

while(i

{

while(ipat)

j--;

root[i]=root[j];

while(i

i++;

root[j]=root[i];

}

root[i]=pat;

quickSort1(root,low,i-1);

quickSort1(root,i+1,high);

}

}

快排的非递归

int partion(int* root,int low,int high)

{

int part=root[low];

while(low

{

while(lowpart) high--;

root[low]=root[high];

while(low

root[high]=root[low];

}

root[low]=part;

return low;

}

void quickSort2(int* root,int low,int high)

{

stack st;

int k;

if(low

{

st.push(low);

st.push(high);

while(!st.empty())

{

int j=st.top();st.pop();

int i=st.top();st.pop();

k=partion(root,i,j);

if(i

{

st.push(i);

st.push(k-1);

}

if(k+1

{

st.push(k+1);

st.push(j);

}

}

}

}

int main()

{

int a[8]={4,2,6,7,9,5,1,3};

quickSort1(a,0,7);

//quickSort2(a,0,7);

int i;

for(i=0;i<8;i++)

cout<

cout<

getchar();

return 0;

}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

总结

以上是生活随笔为你收集整理的java 快排非递归_C++ 中快排的递归和非递归实现的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。