Implementing Quick Sort in place allows us to decrease the space-complexity of the algorithm and make it slightly more efficient. Here is an in-place implementation of quick sort in java:
publicstatic<K>voidquickSortInPlace(K[]S,Comparator<K>comp,inta,intb){if(a>=b){return;}intleft=a;intright=b-1;Kpivot=S[b];Ktemp;// temp object used for swapping
while(left<=right){// scan until reaching value equal or larger than pivot (or right marker)
while(left<=right&&comp.compare(S[left],pivot)<0){left++;}// scan until reaching value equal or smaller than pivot (or left marker)
while(left<=right&&comp.compare(S[right],pivot)>0){right--;}if(left<=right){// indices did not strictly cross
// so swap values and shrink range
temp=S[left];S[left]=S[right];S[right]=temp;left++;right--;}}// put pivot into its final place (currently marked by left index)
temp=S[left];S[left]=S[b];S[b]=temp;quickSortInPlace(S,comp,a,left-1);quickSortInPlace(S,comp,left+1,b);}