Created
June 30, 2012 20:44
-
-
Save ffbit/3025436 to your computer and use it in GitHub Desktop.
The first naive implementation of the QuickSort algorithm
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 ffbit.sorting; | |
| import java.util.Arrays; | |
| public class QuickSorter implements Sorter { | |
| @Override | |
| public void sort(int[] array) { | |
| System.out.println(Arrays.toString(array)); | |
| sort(array, 0, array.length); | |
| } | |
| private void sort(int[] array, int from, int to) { | |
| if (from >= to) { | |
| return; | |
| } | |
| int p = array[from]; | |
| int i = from + 1; | |
| for (int j = from + 1; j < to; j++) { | |
| if (p >= array[j]) { | |
| swap(array, j, i); | |
| i++; | |
| } | |
| } | |
| if (from < i - 1) { | |
| swap(array, from, i - 1); | |
| } | |
| sort(array, i, to); | |
| sort(array, from, i - 1); | |
| } | |
| private void swap(int[] array, int from, int to) { | |
| int buffer = array[from]; | |
| array[from] = array[to]; | |
| array[to] = buffer; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment