快速排序

java实现:
public class QuickSort {
	public static int getPos(int[] arr, int l, int h) {
		int pivot = arr[l];
		while(l<h) {
			while(l<h && arr[h]>=pivot)
				h--;
			if(l < h)	arr[l++] = arr[h];
			while(l<h && arr[l]<=pivot)
				l++;
			if(l < h)	arr[h--] = arr[l];
		}
		arr[l] = pivot;
		return l;
	}
	public static void sort(int arr[], int l, int h) {
		if(l < h) {
			int pos = getPos(arr, l, h);
			sort(arr, l, pos-1);
			sort(arr, pos+1, h);
		}
	}
	
	public static void main(String[] args) {
		int[] arr = { 2, 1, 7, 8, 3, 6, 9, 0, 5, 4 };  
		sort(arr, 0, arr.length-1);
		System.out.println(Arrays.toString(arr));
	}
}

你可能感兴趣的:(排序)