Given an ordered array of arr, adjust arr so that the left half of the array does not have repeating elements and ascending, and does not guarantee that the right is ordered
The partition is OK. The U-Zone is non-repeating and the ascending U is the last position of this region, the initial u=0
I do a left-to-right traversal, and on arr[u+1....i] there is no guarantee that there are no duplicate elements and that the ascending area I is the last position of this area
I move to the right because the array is ordered as a whole, if arr[i]!=arr[u] indicates that the current arr[i] should be added to the U area then exchange it! If Arr[i]==arr[u] indicates the value of the current number Arr[i] has been added to the U area before
On the code:
PackageTT; Public classTest81 { Public voidLeftunique (int[] arr) { if(arr==NULL|| Arr.length<2){ return; } intU = 0; intI =1; while(I! =arr.length) { if(arr[i++]!=Arr[u]) {Swap (arr,++u, I-1); } } } }
Partition adjustment of the array of algorithm summary