您的位置:首页 > 编程语言 > C语言/C++

快速排序【c++】

2010-05-19 21:53 218 查看
代码

//============================================================================
// Name : Sort.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;
template<class T>
int partition(T data[],int low,int high)
{
int t=data[low];
while(low<high)
{
while(low<high&&t<=data[high])
high--;
if(low<high)
data[low++]=data[high];
while(low<high&&t>=data[low])
low++;
if(low<high)
data[high--]=data[low];

}
data[low]=t;
return low;

}
template<class T>
void quickSort(T data[],int i,int j)
{
int d;
if(i<j)
{
d=partition(data,i,j);
quickSort(data,i,d-1);
quickSort(data,d+1,j);
}
}
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
int t[]={12,34,6,-4,6,46,78,42,65,8,43};

quickSort(t,0,11);
for(int i=0;i<10;i++)
printf("%d ",t[i]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: