快速排序算法C++实现

来源:互联网 发布:波士顿矩阵图怎么画 编辑:程序博客网 时间:2024/06/15 17:52
#include <iostream>
using namespace std;
void display( int arr[],int num)
{
int i = 0;
for( i = 0;i<num;i++)
{
cout << arr[i] << "#";
}
cout << endl;
}
int Partition( int array[],int low,int high )
{
int i,j = 0;
int key = array[low];
while(low< high)
{
while( ( low < high ) && ( array[high] > key ) )
{
high--;
}
array[low] = array[high];
while(( low < high ) && ( array[low] <= key ) )
{
low ++;
}
array[high] = array[low];
}
array[low] = key;
return low;

}
static void QuickSort( int array[],int low,int high )
{
int location = 0;
if( low < high )
{
location = Partition( array,low,high);
QuickSort( array,low,location - 1 );
QuickSort( array,location + 1,high );
}
}
int main()
{
int arr[100] = {0};
int count = 0;
cout <<  "please input the number of int :" << endl;
cin >> count;
cout << "your input is:" << endl;
for(int i = 0;i < count;i++)
{
cin >> arr[i];
}
cout << "after quicksort is:" << endl;
QuickSort(arr,0,count-1);
display(arr,count);
return 0;
}
原创粉丝点击