ITeye博客

快速排序

fishisnow · 2015-07-14 16:32 · 常用算法 · 编程语言 · Markdown

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));
	}
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。