快速排序

快速排序有很多注意事项...

 

public static void qsort(int[] a, int low, int high) {
	if (high < low + 2)
		return;
	int piv = low, end = high;
	while (low < high) {
		while (++low < end && a[low] <= a[piv]);
		while (--high > piv && a[high] >= a[piv]);
		if (low < high)
			swap(a, low, high);
	}
	swap(a, piv, high);
	qsort(a, piv, high);
	qsort(a, low, end);
}

public static void swap(int[] a, int i, int j) {
	int tmp = a[i];
	a[i] = a[j];
	a[j] = tmp;
}

 

交换函数如果用位运算方法, 需保证 i != j 才行...

 

 

你可能感兴趣的:(J#)