Last active
December 18, 2015 02:49
-
-
Save arunma/5714009 to your computer and use it in GitHub Desktop.
QuickSort3Way
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package basics.sorting.quick; | |
| import static basics.shuffle.KnuthShuffle.shuffle; | |
| import static basics.sorting.utils.SortUtils.exchange; | |
| import static basics.sorting.utils.SortUtils.less; | |
| public class QuickSort3Way { | |
| public void sort (int[] input){ | |
| //input=shuffle(input); | |
| sort (input, 0, input.length-1); | |
| } | |
| public void sort(int[] input, int lowIndex, int highIndex) { | |
| if (highIndex<=lowIndex) return; | |
| int lt=lowIndex; | |
| int gt=highIndex; | |
| int i=lowIndex+1; | |
| int pivotIndex=lowIndex; | |
| int pivotValue=input[pivotIndex]; | |
| while (i<=gt){ | |
| if (less(input[i],pivotValue)){ | |
| exchange(input, i++, lt++); | |
| } | |
| else if (less (pivotValue, input[i])){ | |
| exchange(input, i, gt--); | |
| } | |
| else{ | |
| i++; | |
| } | |
| } | |
| sort (input, lowIndex, lt-1); | |
| sort (input, gt+1, highIndex); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment