算法之排序:快排

//快排
	public static void quickSort(int[] arr, int l, int r) {
		if (l < r) {
			swap(arr, l + (int) (Math.random() * (r - l + 1)), r);
			int[] p = partition(arr, l, r);
			quickSort(arr, l, p[0]);
			quickSort(arr, p[1], r);
		}
	}

	public static int[] partition(int[] arr, int l, int r) {
		int less = l - 1;
		int more = r + 1;
		int current = less + 1;
		int num = arr[r];

		while (current < more) {
			if (arr[current] < num) {
				swap(arr, ++less, current++);
			} else if (arr[current] > num) {
				swap(arr, --more, current);
			} else {
				current++;
			}
		}
		return new int[]{less, more};
	}

  

posted @ 2018-12-09 19:48  君奉天  阅读(173)  评论(0编辑  收藏  举报