Skip to content

Instantly share code, notes, and snippets.

@ffbit
Created June 30, 2012 20:44
Show Gist options
  • Select an option

  • Save ffbit/3025436 to your computer and use it in GitHub Desktop.

Select an option

Save ffbit/3025436 to your computer and use it in GitHub Desktop.
The first naive implementation of the QuickSort algorithm
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