java实现的快速排序

/**
 *
 * @author zero

 * 1. we set a mid number to divide the array into two part, in the left part,all numbers are less than the mid number, in the right part, it's in contrast

我们首先设置一个中间数字作为划分数组的中间量, 一般设数组的第一个值为中间量,经过排序使得中间量左边的数字都小于它,右边的数字都大于它

注意这里左右两边的数字不一定排序好了,只是都大于或者小于中间量

 * 2. if the numbers in the left part more than one, sort the left part and the sort way is as same as above. Do the right part in the same way.

按照步骤1的做法,分别再对左右两边的数据继续排序,简单的来说就是将左右两边的数据又看成两个新的数组

 * 3. long time passed(in fact it is not really very long)  we got the final result

层层递进,显然是一个递归的过程,我们得到了最终的结果

 */


这里只给出了实现的策略步骤一的实现方式请参考百度百科

点击打开链接

下面给出具体代码实现

/**
 * 
 * @author zero
 * 1. we set a mid number to divide the array into two part, in the left part,all numbers are less than the mid number, in the right part, it's in contrast
 * 2. if the numbers in the left part more than one, sort the left part and the sort way is as same as above. Do the right part in the same way. 
 * 3. long time passed(in fact it is not really very long)  we got the final result
 */
public class SwiftSort {
	static int[] array = new int[]{10, 3, 2 ,1, 7, 10, 13, 4, 18};
	
	public static void quickSort(int start, int end) {
		if(start == end) {
			return;
		}
		
System.out.println("this time we sort the array from " + start + " to " + end);
System.out.println("this time the midnum'value is" + array[start]);
	
		int initStart = start;
		int initEnd = end;
		int midIndex = start;
		
		int midnum = array[start];
		int direction = 1; //1---from right to left  2---from left to right
		
		while(start != end) {
			if(direction == 1) {
				if(array[end] < midnum) {
					array[start] = array[end];
					direction = 2;
				}else {
					end--;
				}
			}else if(direction == 2) {
				if(array[start] > midnum) {
					array[end] = array[start];
					direction = 1;
				}else {
					start++;
				}
			}
		}
		
		array[start] = midnum;
		midIndex = start;
		
		System.out.println("this turn of sort process end.....");
		for(int i : array) {
			System.out.print(i + " ");
		}
		System.out.println("");
		
		if(initStart < midIndex) {
			quickSort(initStart, midIndex-1);
		}
		
		if(initEnd > midIndex) {
			quickSort(midIndex+1, initEnd);
		}
		
	}
	
	public static void main(String[] args) {
		quickSort(0, array.length-1);
		System.out.println("the final result is。。。。。。");
		
		for(int i : array) {
			System.out.print(i + " ");
		}
		
	}
}

运行结果为:

this time we sort the array from 0 to 8
this time the midnum'value is10
this turn of sort process end.....
4 3 2 1 7 10 10 13 18
this time we sort the array from 0 to 5
this time the midnum'value is4
this turn of sort process end.....
1 3 2 4 7 10 10 13 18
this time we sort the array from 0 to 2
this time the midnum'value is1
this turn of sort process end.....
1 3 2 4 7 10 10 13 18
this time we sort the array from 1 to 2
this time the midnum'value is3
this turn of sort process end.....
1 2 3 4 7 10 10 13 18
this time we sort the array from 4 to 5
this time the midnum'value is7
this turn of sort process end.....
1 2 3 4 7 10 10 13 18
this time we sort the array from 7 to 8
this time the midnum'value is13
this turn of sort process end.....
1 2 3 4 7 10 10 13 18
the final result is。。。。。。
1 2 3 4 7 10 10 13 18 



只要弄清楚第一步的策略  这个排序的程序就好写了  大家加油


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值