快速排序java实现

快速排序算法实现
package test;

import java.util.Arrays;
import java.util.Scanner;



public class Test{	
	static Scanner sc = new Scanner(System.in);
	static String line;		

	static int n = 8;
	static int[] arr = {2,1,4,7,8,6,5,3};
	
	static void QuickSort(int[] arr){
		Qsort(0, arr.length-1);
	}
	
	static void Qsort(int low, int high){
		if(low<high){
			int mid = partition(low,high);//找到分界点
			Qsort(low,mid-1);
			Qsort(mid+1,high);
		}
	}		
	
	private static int partition(int start, int end) {
		int temp = arr[start];
		while(start<end){
			while(end>start && arr[end]>=temp) --end;//找到比标志要小的数,换位
			arr[start] = arr[end];
			while(end>start && arr[start]<=temp) ++start;//找到比标志要大的数,换位
			arr[end] = arr[start];
		}
		arr[start] = temp;
		return start;
	}

	public static void main(String[] args) {
		QuickSort(arr);
		System.out.println(Arrays.toString(arr));
	}	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值