数据结构- 快速排序

快速排序

这是一个非常流行并且高校的排序算法:QUICKSORT。

首先要先介绍一下划分算法:

设A[ low...high ]是一个包含n个数的数组,并且x=A[low]。

重排数组A,使得小于等于x的元素排在x前面,大于x的元素排在x后面。

得到新数组:对于w ,low ≤ w ≤ high,x在A[w]中。

如:数组A=5 3 9 2 7 1 8 ,low=1(最低),high=7(最高),x=5(主元),

重排后:A=1 3 2 5 7 9 8,w=4(新位置为4)。

这种重排列行为也称为围绕x的拆分或划分,x成为主元或拆分元素。

定义1:如果元素A[ j ]既不小于A[low...j-1]中的元素,也不大于A[j+1,,,high]中的元素,则称其处在一个适当位置或正确位置。

★观察结论:用x(x∈A)作为主元划分数组A后,x将处与一个正确位置。

SPLIT

输入:数组A[ low...high]

输出:(1)如有必要,输出没有通过辅助数组B,原空间重新排列的数组A

           (2)划分元素A[low]的新位置w。

i ← low

x ←A[low]

for j← low +1 to high

    if A[j] ≤ x then

        i ← i+1

        if i ≠ j then 互换A[i] 和A[j]

    end if

end if

互换A[low]和A[i]

w ←i

return A和w

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N=10;
void split(int *a,int &w){
	int i=0,x=a[0];
	for(int j=1;j<=N-1;++j){
		if(a[j] <=x ){
			i=i+1;
			if(i!=j)
				swap(a[i],a[j]);
			/*for(int tm=st;tm<=ed;tm++){
				printf("%d ",a[tm]);
			} 
			printf("\ni=%d,j=%d\n",i,j);*/
		}
	}
	swap(a[0],a[i]);
	w=i;
} 
int main()
{
	int a[N]={5,6,3,2,1,8,9,7,4,10}; 
	int w=0;
	split(a,w);
	printf("w:%d\n",w);//w:4 因为数组0-9 
	for(int i=0;i<N;i++){
		printf("%d ",a[i]);//4 3 2 1 5 8 9 7 6 10
	}
	return 0;
}

☆ 验证准则:每次交换的双方,都是 小的左边,大的右边。最终准基左边比准基小,右边比准基大。

快排一次的过程(以第一个数 5 为准基)


56321897410
                                                                                            ←从右到左看,第一个比  5(准基)小的数   4   。交换

46321897510

→从左到右看,第一个比  5(准基)大的数 6 ,交换

45321897610

                                                                                            ←从右到左看,第一个比  5(准基)小的数  1  。交换

41325897610

→从左到右看,5前面都比 5 小,第一趟结束。
做题的时候:

5       6       3        2       1        8       9        7        4        10    (原)
4                                                                          5

         5                                                                 6

         1                          5

把每一层的最下面 拿下来。就是第一趟结束。

4       1        3        2       5       8       9         7       6         10   

完毕


时间复杂性为:Θ(n)

空间复杂性为:Θ(1)


-----------------------------------------分隔线-----------------------------------------

快速排序算法:二分配合划分算法

输入:n个元素的数组A[1..n]。

输出:按非降序排序的数组A中的元素。

quickSort(A,low,high)

if low < high then

    SPLIT(A[low...high[ , w )//w为A[low]的新位置

    quickSort(A,low,w-1)

    quickSort(A,w+1,high)

end if

最坏情况:输入数组已经非降序排列,最小元素为主元,quickSort调用n次,每次split执行j-1次,n(n-1)/2 ≈ Θ(n平方)

★如果总是选择中项作为主元,最坏情况可以优化到Θ(nlogn)

平均情况:Θ(nlogn)

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N=10;
void split(int *a,int &w,int low,int high){
	int i=low,x=a[low];
	for(int j=low+1;j<=high;++j){
		if(a[j] <=x ){
			i=i+1;
			if(i!=j)
				swap(a[i],a[j]);
		}
	}
	swap(a[low],a[i]);
	w=i;
} 
void quickSort(int *a,int low,int high){
	if(low < high){
		int w;
		split(a,w,low,high);
		quickSort(a,low,w-1);
		quickSort(a,w+1,high);
	}
}
int main()
{
	int a[N]={5,6,3,2,1,8,9,7,4,10}; 
	quickSort(a,0,N-1);
	for(int i=0;i<N;i++){
		printf("%d ",a[i]);
	}
	return 0;
}



快速排序到此结束

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值