快速排序
快速排序
时限:1000ms 内存限制:10000K 总时限:3000ms
3 2 1 4 5
2
3
4
5
#include <iostream>
using namespace std;
void quicksort(int *datapointer,int low,int high)
{
if(low>=high)
return;
int i = low;
int j = high;
int key = datapointer[low];
while(i<j)
{
while(datapointer[j]>=key&&i<j)
j--;
datapointer[i] = datapointer[j];
//cout<<"hello"<<endl;
while(datapointer[i]<=key&&i<j)
i++;
datapointer[j] = datapointer[i];
}
datapointer[i] = key;
quicksort(datapointer,low,i-1);
quicksort(datapointer,i+1,high);
}
int main()
{
int *datapointer,num;
cin>>num;
datapointer = new int[num+1];
for(int i = 1;i <= num; i++)
cin>>datapointer[i];
quicksort(datapointer,1,num);
for(i = 1;i <= num; i++)
cout<<datapointer[i]<<endl;
//cout<<endl;
return 0;
}