快速排序(解析及代码实现)

//快速排序的时间复杂度为n*logn,常用来处理无序的大量数据(越无序效率越高)
//第一种
//取每一段数据的第一个数作为枢轴,保存,并空出位置,纪为a[empty],empty=0;
//从右开始找j=a.length-1 --找到一个数a[j]小于枢轴,将其移动到a[empty]处,empty=j;
//开始从左开始找 i=1 ++ 找到一个数a[i]大于枢轴,将其移动到a[empty]处,empty=i;
//开始右左重复进行,直到使得j<i,将a[j]移动到a[empty]处,a[j]=枢轴;
//使得枢轴左边的数皆小于枢轴,枢轴右边的数皆大于枢轴

//开始递归,重复以上动作,直到排序完成




public static boolean quickSort(double[] a, int low, int high,int type) {
		//递归结束
		if(low>=high)
			return false;
		//确定枢轴位置
		//方法一
		int p=partition1(a,low,high);
		//方法二
		//int p=partition(a,low,high,type);
		//左右分离,递归快排
		quickSort(a, low, p-1,type);
		quickSort(a, p+1, high,type);
		return true;
	}
	private static int partition1(double[] a, int low, int high) {

		//取第一个数为枢轴,对于有序序列效率太低,采取随机数选取,与第一个交换
		int rand = (int)(Math.random()*(high-low));
		swap(a,low,low+rand);
		//定位枢轴的位置
		double temp=a[low];
		//分区
		while(low!=high){
			//high--找到小于枢轴的数
			while(a[high]>temp&&low<high){
				high--;
			}
			if(low<high){
				a[low++]=a[high];
			}
			//low++找到大于枢轴的数
			while(a[low]<temp&&low<high){
				low++;
			}
			if(low<high){
				a[high--]=a[low];
			}
		}
		a[low]=temp;
		return low;
	}

public static boolean quickSort(double[] a, int low, int high,int type) {
		//递归结束
		if(low>=high)
			return false;
		//确定枢轴位置
		//方法一
		//int p=partition1(a,low,high);
		//方法二
		int p=partition(a,low,high,type);
		//左右分离,递归快排
		quickSort(a, low, p-1,type);
		quickSort(a, p+1, high,type);
		return true;
	}
	private static int partition(double[] a, int low, int high,int type) {
		//取第一个数为枢轴,对于有序序列效率太低,采取随机数选取,与第一个交换
		int rand = (int)(Math.random()*(high-low));
		swap(a,low,low+rand);
		//定位枢轴的位置
		double temp=a[low];
		int i=low;
		int j=high+1;
		while(true){
			//找到左边大于/小于枢轴的数
			if(type==0)//从小到大排
				while(a[++i]<temp&&i<high);
			else
				while(a[++i]>temp&&i<high);
				
			//找到右边小于/大于枢轴的数
			if(type==0)
				while(a[--j]>temp);
			else
				while(a[--j]<temp);
			
			if(j <= i){
				break;
			}
			//交换
			swap(a, j, i);
		}
		swap(a, j, low);
		return j;
	}



以下是函数重载:(完整代码)

package cn.hncu.sort;

import java.util.Random;

public class QuickSort {

	//快速排序的时间复杂度为n*logn,常用来处理无序的大量数据(越无序效率越高)
	//第一种
	//取每一段数据的第一个数作为枢轴,保存,并空出位置,纪为a[empty],empty=0;
	//从右开始找j=a.length-1 --找到一个数a[j]小于枢轴,将其移动到a[empty]处,empty=j;
	//开始从左开始找 i=1 ++ 找到一个数a[i]大于枢轴,将其移动到a[empty]处,empty=i;
	//开始右左重复进行,直到使得j<i,将a[j]移动到a[empty]处,a[j]=枢轴;
	//使得枢轴左边的数皆小于枢轴,枢轴右边的数皆大于枢轴
	//开始递归,重复以上动作,直到排序完成
	public static void quickSort(double[] a){
		quickSort(a,0);
	}
	public static void quickSort(double[] a,int type){
		quickSort(a,0,a.length-1,type);
	}
	public static boolean quickSort(double[] a, int low, int high,int type) {
		//递归结束
		if(low>=high)
			return false;
		//确定枢轴位置
		//方法一
		int p=partition1(a,low,high);
		//方法二
		//int p=partition(a,low,high,type);
		//左右分离,递归快排
		quickSort(a, low, p-1,type);
		quickSort(a, p+1, high,type);
		return true;
	}
	private static int partition(double[] a, int low, int high,int type) {
		//取第一个数为枢轴,对于有序序列效率太低,采取随机数选取,与第一个交换
		int rand = (int)(Math.random()*(high-low));
		swap(a,low,low+rand);
		//定位枢轴的位置
		double temp=a[low];
		int i=low;
		int j=high+1;
		while(true){
			//找到左边大于/小于枢轴的数
			if(type==0)//从小到大排
				while(a[++i]<temp&&i<high);
			else
				while(a[++i]>temp&&i<high);
				
			//找到右边小于/大于枢轴的数
			if(type==0)
				while(a[--j]>temp);
			else
				while(a[--j]<temp);
			
			if(j <= i){
				break;
			}
			//交换
			swap(a, j, i);
		}
		swap(a, j, low);
		return j;
	}
	private static int partition1(double[] a, int low, int high) {

		//取第一个数为枢轴,对于有序序列效率太低,采取随机数选取,与第一个交换
		int rand = (int)(Math.random()*(high-low));
		swap(a,low,low+rand);
		//定位枢轴的位置
		double temp=a[low];
		//分区
		while(low!=high){
			//high--找到小于枢轴的数
			while(a[high]>temp&&low<high){
				high--;
			}
			if(low<high){
				a[low++]=a[high];
			}
			//low++找到大于枢轴的数
			while(a[low]<temp&&low<high){
				low++;
			}
			if(low<high){
				a[high--]=a[low];
			}
		}
		a[low]=temp;
		return low;
	}
	public static boolean quickSort(double[] a, int low, int high) {
		return quickSort(a, low, high, 0);
	}
	//float[]数组
	public static void quickSort(float[] a,int type){
		quickSort(a,0,a.length-1,type);
	}
	public static void quickSort(float[] a){
		quickSort(a, 0);
	}
	public static boolean quickSort(float[] a,int first,int end,int type){
		if(first<0)
			return false;
		if(end>=a.length)
			return false;
		if(first>end)
			return false;
		double[] b=new double[end-first+1];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i+first];
		}
		quickSort(b, 0, b.length-1, type);
		for (int i = 0; i < b.length; i++) {
			a[i+first]=(float)b[i];
		}
		return true;
	}
	public static boolean quickSort(float[] a,int first,int end) {
		return quickSort(a, first, end, 0);
	}
	//long[]数组
	public static void quickSort(long[] a,int type){
		quickSort(a,0,a.length-1,type);
	}
	public static void quickSort(long[] a){
		quickSort(a, 0);
	}
	public static boolean quickSort(long[] a,int first,int end,int type){
		if(first<0)
			return false;
		if(end>=a.length)
			return false;
		if(first>end)
			return false;
		double[] b=new double[end-first+1];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i+first];
		}
		quickSort(b, 0, b.length-1, type);
		for (int i = 0; i < b.length; i++) {
			a[i+first]=(long)b[i];
		}
		return true;
	}
	public static boolean quickSort(long[] a,int first,int end) {
		return quickSort(a, first, end, 0);
	}
	//int[]数组
	public static void quickSort(int[] a,int type){
		quickSort(a, 0, a.length-1, type);
	}
	public static void quickSort(int[] a){
		quickSort(a, 0);
	}
	public static boolean quickSort(int[] a,int first,int end,int type){
		if(first<0)
			return false;
		if(end>=a.length)
			return false;
		if(first>end)
			return false;
		double[] b=new double[end-first+1];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i+first];
		}
		quickSort(b, 0, b.length-1, type);
		for (int i = 0; i < b.length; i++) {
			a[i+first]=(int)b[i];
		}
		return true;
	}
	public static boolean quickSort(int[] a,int first,int end) {
		return quickSort(a, first, end, 0);
	}
	//short[]数组
	public static void quickSort(short[] a,int type){
		quickSort(a, 0, a.length-1, type);
	}
	public static void quickSort(short[] a){
		quickSort(a, 0);
	}
	public static boolean quickSort(short[] a,int first,int end,int type){
		if(first<0)
			return false;
		if(end>=a.length)
			return false;
		if(first>end)
			return false;
		double[] b=new double[end-first+1];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i+first];
		}
		quickSort(b, 0, b.length-1, type);
		for (int i = 0; i < b.length; i++) {
			a[i+first]=(short)b[i];
		}
		return true;
	}
	public static boolean quickSort(short[] a,int first,int end) {
		return quickSort(a, first, end, 0);
	}
	//byte[]数组
	public static void quickSort(byte[] a,int type){
		quickSort(a, 0, a.length-1, type);
	}
	public static void quickSort(byte[] a){
		quickSort(a, 0);
	}
	public static boolean quickSort(byte[] a,int first,int end,int type){
		if(first<0)
			return false;
		if(end>=a.length)
			return false;
		if(first>end)
			return false;
		double[] b=new double[end-first+1];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i+first];
		}
		quickSort(b, 0, b.length-1, type);
		for (int i = 0; i < b.length; i++) {
			a[i+first]=(byte)b[i];
		}
		return true;
	}
	public static boolean quickSort(byte[] a,int first,int end) {
		return quickSort(a, first, end, 0);
	}
	//char[]数组
	public static void quickSort(char[] a,int type){
		quickSort(a, 0, a.length-1, type);
	}
	public static void quickSort(char[] a){
		quickSort(a, 0);
	}
	public static boolean quickSort(char[] a,int first,int end,int type){
		if(first<0)
			return false;
		if(end>=a.length)
			return false;
		if(first>end)
			return false;
		double[] b=new double[end-first+1];
		for (int i = 0; i < b.length; i++) {
			b[i]=a[i+first];
		}
		quickSort(b, 0, b.length-1, type);
		for (int i = 0; i < b.length; i++) {
			a[i+first]=(char)b[i];
		}
		return true;
	}
	public static boolean quickSort(char[] a,int first,int end) {
		return quickSort(a, first, end, 0);
	}
	//String[]数组
	public static void quickSort(String[] a){
		quickSort(a,0);
	}
	public static void quickSort(String[] a,int type){
		quickSort(a,0,a.length-1,type);
	}
	public static boolean quickSort(String[] a, int low, int high,int type) {
		//递归结束
		if(low>=high)
			return false;
		//确定枢轴位置
		//方法一
		//int p=partition1(a,low,high);
		//方法二
		int p=partition(a,low,high,type);
		//左右分离,递归快排
		quickSort(a, low, p-1,type);
		quickSort(a, p+1, high,type);
		return true;
	}
	private static int partition(String[] a, int low, int high,int type) {
		//取第一个数为枢轴,对于有序序列效率太低,采取随机数选取,与第一个交换
		int rand = (int)(Math.random()*(high-low));
		swap(a,low,low+rand);
		//定位枢轴的位置
		String temp=a[low];
		int i=low;
		int j=high+1;
		while(true){
			//找到左边大于/小于枢轴的数
			if(type==0)//从小到大排
				while(a[++i].compareTo(temp)<0&&i<high);
			else
				while(a[++i].compareTo(temp)>0&&i<high);
				
			//找到右边小于/大于枢轴的数
			if(type==0)
				while(a[--j].compareTo(temp)>0);
			else
				while(a[--j].compareTo(temp)<0);
			
			if(j <= i){
				break;
			}
			//交换
			swap(a, j, i);
		}
		swap(a, j, low);
		return j;
	}
	public static boolean quickSort(String[] a, int low, int high) {
		return quickSort(a, low, high, 0);
	}
	
	//交换
	private static void swap(double[] a, int j, int i) {
		double temp=a[i];
		a[i]=a[j];
		a[j]=temp;
		
	}
	private static void swap(String[] a, int j, int i) {
		String temp=a[i];
		a[i]=a[j];
		a[j]=temp;
	}
	//输出
	private static void print(String[] a) {
		for (String x : a) {
			System.out.print(x+" ");
		}
		System.out.println();
	}
	private static void print(double[] a) {
		for (double x : a) {
			System.out.print(x+" ");
		}
		System.out.println();
	}
	private static void print(float[] a) {
		for (float x : a) {
			System.out.print(x+" ");
		}
		System.out.println();
	}
	private static void print(long[] a) {
		for (long x : a) {
			System.out.print(x+" ");
		}
		System.out.println();
	}
	private static void print(int[] a) {
		for (int x : a) {
			System.out.print(x+" ");
		}
		System.out.println();
	}
	private static void print(short[] a) {
		for (short x : a) {
			System.out.print(x+" ");
		}
		System.out.println();
	}
	private static void print(byte[] a) {
		for (byte x : a) {
			System.out.print(x+" ");
		}
		System.out.println();
	}
	private static void print(char[] a) {
		for (char x : a) {
			System.out.print(x+" ");
		}
		System.out.println();
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值