老师好,我按照双指针的方法自己实现了一下划分,但是有个问题,标记处这两个语句交换一下位置,交换前是有序正确的,交换后就是错的,我不知道为啥...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | int _partition( int A[], int left, int right){ int temp = rand () % (right - left + 1) + left; swap(A[left],A[temp]); temp = left; while (left < right){ //***这是交换前,是正确的 while (left < right && A[right] > A[temp]) right--; while (left < right && A[left] <= A[temp]) left++; //***这是交换后,是错误的 while (left < right && A[left] <= A[temp]) left++; while (left < right && A[right] > A[temp]) right--; swap(A[left],A[right]); } swap(A[temp],A[left]); return left; } |