#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int S[9];
void Partition(int low,int high,int &pivotpoint)
{
int pivotitem=S[low];
int j=low;
for(int i=low+1;i<=high;i++)//自己差点忘掉 = high
{
if(S[i]<pivotitem)
{
j++;
swap(S[i],S[j]);
}
}
pivotpoint=j;
swap(S[j],S[low]);
}
void quicksort(int low,int high)
{
int pivotpoint;
if(low<high)
{
Partition(low,high,pivotpoint);
quicksort(low,pivotpoint-1);
quicksort(pivotpoint+1,high);
}
}
int main()
{
S[0]=0;
for(int i=1;i<=8;i++)
{
cin>>S[i];
}
quicksort(1,8);
for(int i=1;i<=8;i++)
{
cout<<S[i]<<endl;
}
return 0;
}
算法2.6&2.7快速排序
最新推荐文章于 2025-07-23 15:31:19 发布
本文介绍了一个使用C++实现的快速排序算法。通过递归方式,选取基准元素并对数组进行分区,实现了对数组的有效排序。代码中详细展示了分区过程及排序流程。
1349

被折叠的 条评论
为什么被折叠?



