快速排序算法

<img src="https://img-blog.csdn.net/20151224122026960?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
#include "stdafx.h"
#include <iostream>

using namespace std;

void QuictSort(int a[], int left, int right)
{
	if (left > right)
	{
		return ;
	}
	int first = left;
	int last = right;
	int  key = a[first];  //设定基准
	/*下面的循环是为了得到大于和小于基准值的数据分别位于左右两侧*/  
	while (first < last)
	{
		while (first < last&&a[last] >= key)
		{
			last--;
		}
		a[first] = a[last];
		while (first<last&&a[first]<key )
		{
			first++;
		}
		a[last] = a[first];
	}

	a[first] = key;

	 QuictSort( a, left, first-1);
	 QuictSort(a, first + 1, right);

}

int main()
{
	int a[] = { 9, 18, 6, 3, 8, 4, 77 };

	QuictSort(a, 0,sizeof(a)/sizeof(a[0])-1);
	for (int i = 0; i < sizeof(a) / sizeof(a[0]) ; i++)
	{
		cout << " " << a[i];
	}
	//waitKey(0);
	return 0;
}

下面这个是引用别人写的快速排序http://blog.csdn.net/liuchen1206/article/details/6954074

#include<iostream>  
using namespace std;  
void quickSort(int a[],int,int);  
int main()  
{  
    int array[]={34,65,12,43,67,5,78,10,3,70},k;  
    int len=sizeof(array)/sizeof(int);  
    cout<<"The orginal arrayare:"<<endl;  
    for(k=0;k<len;k++)  
        cout<<array[k]<<",";  
    cout<<endl;  
    quickSort(array,0,len-1);  
    cout<<"The sorted arrayare:"<<endl;  
    for(k=0;k<len;k++)  
        cout<<array[k]<<",";  
    cout<<endl;  
    system("pause");  
    return 0;  
}  
  
void quickSort(int s[], int l, int r)  
{  
    if (l< r)  
    {        
        int i = l, j = r, x = s[l];  
        while (i < j)  
        {  
            while(i < j && s[j]>= x) // 从右向左找第一个小于x的数  
                j--;   
            if(i < j)  
                s[i++] = s[j];  
            while(i < j && s[i]< x) // 从左向右找第一个大于等于x的数  
                i++;   
            if(i < j)  
                s[j--] = s[i];  
        }  
        s[i] = x;  
        quickSort(s, l, i - 1); // 递归调用  
        quickSort(s, i + 1, r);  
    }  
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值