算法导论的Java实现----7. 快速排序

1. 快速排序

/*
 *  快速排序
 */
import java.util.Comparator;
public class QuickSort {

	public static <T> int partition(T[] a, Comparator<? super T> c, int p,int r)
	{ 
		T t = a[r-1];
		int i = p-1;
		for(int j=p;j<r-1;j++)
		{
			if(c.compare(a[j],t)<=0)
			{
				i++;
				T temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
		}
		
		T temp = a[i+1];
		a[i+1] = a[r-1];
		a[r-1] = temp;
		return i+1;
	}
	//在java中,如果要对集合对象或数组对象进行排序,
	//需要实现Comparator接口以达到我们想要的目标
	public static <T> void quickSort(T[] a, Comparator<? super T> c)
	{
		quickSort(a,c,0,a.length);
	}
	public static <T> void quickSort(T[] a, Comparator<? super T> c,int p, int r)
	{
		int q = 0;
		if(p<r)
		{
			q = partition(a,c,p,r);
			quickSort(a,c,p,q);
			quickSort(a,c,q+1,r);
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Integer [] temp = new Integer[]{2,8,7,4,6,5,6,4};
		quickSort(temp,new Comparator<Integer>()
		{
			public int compare(Integer o1,Integer o2)
			{
				return o1-o2;
			}
		});
		
		for(int i:temp)
		{
			System.out.print(i + " ");
		}
	}

}

输出结果:2 4 4 5 6 6 7 8 


2. 快速排序的随机化版本

/*
 *  快速排序的随机化版本
 */
import java.util.Comparator;
import java.util.Random;
public class Randomized_quicksort {

	/**
	 * @param args
	 */
	public static <T> int partition(T[] a, Comparator<? super T> c, int p,int r)
	{ 
		T t = a[r-1];
		int i = p-1;
		for(int j=p;j<r-1;j++)
		{
			if(c.compare(a[j],t)<=0)
			{
				i++;
				T temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
		}
		
		T temp = a[i+1];
		a[i+1] = a[r-1];
		a[r-1] = temp;
		return i+1;
	}
	
	public static <T> int randomizedPartition(T[] a, Comparator<? super T> c, int p, int r)
	{
		int i = new Random().nextInt(r-p)+p;
		T temp = a[i];
		a[i] = a[r-1];
		a[r-1] = temp;
		
		return partition(a,c,p,r);
	}
	
	public static <T> void randomized_QuickSort(T[] a, Comparator<? super T> c)
	{
		randomized_QuickSort(a,c,0,a.length);
	}
	public static <T> void randomized_QuickSort(T[] a, Comparator<? super T> c, int p,int r)
	{
		if(p<r)
		{
			int q =randomizedPartition(a,c,p,r);
			randomized_QuickSort(a,c,p,q);
			randomized_QuickSort(a,c,q+1,r);
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Integer [] temp = new Integer[] {1,5,3,23,689,34,12};
		randomized_QuickSort(temp, new Comparator<Integer>()
		{
			public int compare(Integer o1, Integer o2)
			{
				return o1-o2;
			}
		});
		
		for(int i:temp)
		{
			System.out.print(i + " ");
		}
	}

}

输出结果:1 3 5 12 23 34 689 


3.  复杂度分析

(1)在最优情况下, partiation 每次都划分得很均匀

 

   此时:T(n) = 2 T(n / 2) + (n - 1) ; // 在笔记1中, 我们知道,这个解是 nlog(n)

  所以快速排序的最好情况是 nlog(n)

 

(2)在最坏情况下, partiation 每次都类似划分这样 [x][n-1个元素]

 

此时: T(n) = T(n - 1) + (n - 1) ;   //  这个是 n^2 

 

(3)在一般情况下, partiation 即使划分得很偏,比如划分在 1 / 10 位置

 

  此时 T(n) = T(1 / 10) + T(9 / n) + n - 1,  这个东西也是nlog(n)

 

(4)所以在平均情况下,快速排序的复杂度是 nlog(n)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值