1. 程式人生 > >快速排序法-Java實現

快速排序法-Java實現

public static void quickSort(int[] a, int left, int right) {
		int f, t;
		int rtemp, ltemp;
		ltemp = left;
		rtemp = right;
		f = a[(left + right) / 2];
		while (ltemp < rtemp) {
			while (a[ltemp] < f)
				ltemp++;
			while (a[rtemp] > f)
				rtemp--;
			if (ltemp <= rtemp) {
				t = a[ltemp];
				a[ltemp] = a[rtemp];
				a[rtemp] = t;
				rtemp--;
				ltemp++;
			}
		}
		if (ltemp == rtemp)
			ltemp++;
		if (left < rtemp)
			quickSort(a, left, ltemp - 1);
		if (ltemp < right)
			quickSort(a, rtemp + 1, right);
	}