现在的位置: 首页 > 综合 > 正文

【基础排序】快速排序

2013年08月22日 ⁄ 综合 ⁄ 共 566字 ⁄ 字号 评论关闭
#include <iostream>
using namespace std;

void QuickSort(int a[],int low,int high)
{
	int i=low;
	int j=high;
	int temp=a[low];
	while(low < high)
	{
		while(low<high && a[high]>=temp)
		{
			high--;
		}
		swap(a[high],a[low]);
		while(low<high && a[low]<=temp)
		{
			low++;
		}
		swap(a[high],a[low]);
	}
	a[low]=temp;
	if(i != low)
	{
		QuickSort(a,i,low-1);
	}
	if(j != high)
	{
		QuickSort(a,high+1,j);
	}
}

void print(int a[],int n)    
{    
    for(int i=0; i<n; i++)    
    {    
        cout<<a[i]<<" ";    
    }    
    cout << endl;    
}    
  
  
void main()    
{    
    int a[] = {1,3,6,8,0,5,7};    
    cout << "排序前:";    
    print(a,sizeof(a)/sizeof(a[0]));  
	int n=sizeof(a)/sizeof(a[0]);  
    QuickSort(a,0,n-1);    
    cout << "排序后:";    
    print(a,sizeof(a)/sizeof(a[0]));    
}    

抱歉!评论已关闭.