java面试之快速排序

问题:java面试之快速排序

解决方案:

public void qsort(int[] arr, int low, int high) {
		if (low < high) {
			int temp, pPos, i;
			pPos = low;
			temp = arr[pPos];
			for (i = low + 1; i <= high; i++) {
				if (arr[i] > temp) {
					pPos++;
					swap(arr, pPos, i);
				}
			}
			swap(arr, low, pPos);
			qsort(arr, low, pPos - 1);
			qsort(arr, pPos + 1, high);
		}
	}

	public void swap(int[] arr, int i, int j) {
		int temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}

你可能感兴趣的:(Java面试)