//c++
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void sort(vector<int>::iterator beg,vector<int>::iterator end)
{
if(beg>=end) return;
vector<int>::iterator b=beg,e=end;
int s=*beg;
while(b<e)
{
while((b<e)&&(*e>s))
--e;
if(b<e)
*b=*e;
while((b<e)&&(*b<=s))
++b;
if(b<e)
*e=*b;
}
*b=s;
sort(beg,b-1);
sort(b+1,end);
}
template<typename T> struct print
{
void operator()(const T &out)
{
cout<<out<<endl;
}
};
int main(int argc,char *argv[])
{
int in;
vector<int> arr;
while(cin>>in)
arr.push_back(in);
for_each(arr.begin(),arr.end(),print<int>());
sort(arr.begin(),arr.end()-1);
for_each(arr.begin(),arr.end(),print<int>());
return(0);
}
//c
#include
int main(int argc,char *argv[])
{
void sortarray(int a[],int s,int e);
int a[] = {5,4,6,8,7,0,1,3,2,9};
int s,e;
s = 0;
e = sizeof(a)/sizeof(a[0])-1;
for(;s <= e;++s)
printf("%d ",a[s]);
printf("\r\n");
sortarray(a,0,e);
for(s = 0;s <= e;++s)
printf("%d ",a[s]);
return(0);
}
void sortarray(int a[],int s,int e)
{
if(s == e)return;//出口
int i = s,j = e,key = a[s];//边界和基数
while(i < j){
while(i < j && key < a[j])//后往前
--j;
if(i < j) a[i] = a[j];
while( i< j && key >= a[i])//前往后
++i;
if(i < j)
a[j] = a[i];
}
a[i] = key;//i == j
sortarray(a,s,i);//
sortarray(a,i+1,e);
}
快速排序算法
最新推荐文章于 2023-03-20 21:08:57 发布
本文详细介绍了使用C++和C语言实现快速排序算法的过程,包括模板类、迭代器的应用,以及两种不同语言环境下算法的具体实现细节。
648

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



