Last active
December 17, 2015 22:49
-
-
Save arunma/5684627 to your computer and use it in GitHub Desktop.
Quick Sort Basic
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.sorting.utils.SortUtils.exchange; | |
| import static basics.sorting.utils.SortUtils.less; | |
| import basics.shuffle.KnuthShuffle; | |
| public class QuickSortBasic { | |
| public void sort (int[] input){ | |
| //KnuthShuffle.shuffle(input); | |
| sort (input, 0, input.length-1); | |
| } | |
| private void sort(int[] input, int lowIndex, int highIndex) { | |
| if (highIndex<=lowIndex){ | |
| return; | |
| } | |
| int partIndex=partition (input, lowIndex, highIndex); | |
| sort (input, lowIndex, partIndex-1); | |
| sort (input, partIndex+1, highIndex); | |
| } | |
| private int partition(int[] input, int lowIndex, int highIndex) { | |
| int i=lowIndex; | |
| int pivotIndex=lowIndex; | |
| int j=highIndex+1; | |
| while (true){ | |
| while (less(input[++i], input[pivotIndex])){ | |
| if (i==highIndex) break; | |
| } | |
| while (less (input[pivotIndex], input[--j])){ | |
| if (j==lowIndex) break; | |
| } | |
| if (i>=j) break; | |
| exchange(input, i, j); | |
| } | |
| exchange(input, pivotIndex, j); | |
| return j; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment