算法复习--快速排序

快速排序的基本思想:http://www.cnblogs.com/foreverking/articles/2234225.html

有图解。




	static void QuickSortAlg(int R[],int low,int heigh){
		if(low >= heigh || low <0 || heigh<0){
			return;
		}
		int i=0,j=0,t=0;
		i=low;
		j=heigh;
		t=R[i];
		while(i != j){
			while(i<j && t<R[j]){
				j--;
				}
			if(i<j){ 
				R[i++]=R[j];
				}
			
			while(i<j && t>R[i]){
				i++;
				}
			if(i<j){ 
				R[j--]=R[i];
				}
		}
		R[i]=t;
		for(int d = 0;d<8;d++){
			System.out.print(R[d]+" ");
		}
		System.out.println();
		QuickSortAlg(R,low,i-1);
		QuickSortAlg(R,i+1,heigh);
	}

时间复杂度  
最好的情况下:因为每次都将序列分为两个部分(一般二分都复杂度都和logN相关),故为 O(N*logN)  
最坏的情况下:基本有序时,退化为冒泡排序,几乎要比较N*N次,故为O(N*N)
稳定性  不稳定
由于每次都需要和目标元素交换,因此原来的顺序就可能被打乱。如序列为 5 3 3 4 3 8 9 10 11会将3的顺序打乱。所以说,快速排序是不稳定的

猜你喜欢

转载自blog.csdn.net/Diaoliangwang/article/details/38473917