Skip to content

Instantly share code, notes, and snippets.

@louisbros
Created January 22, 2014 07:56
Show Gist options
  • Save louisbros/8555014 to your computer and use it in GitHub Desktop.
Save louisbros/8555014 to your computer and use it in GitHub Desktop.
Java Quicksort
package com.test;
import java.util.Collections;
import java.util.List;
public class QuickSort<T extends Comparable<T>> {
public void sort(List<T> values) {
quickSort(0, values.size() - 1, values);
}
private void quickSort(int low, int high, List<T> values) {
int left = low;
int right = high;
T pivot = values.get(low + (high - low) / 2);
while(left <= right){
while(values.get(left).compareTo(pivot) < 0){
left++;
}
while(values.get(right).compareTo(pivot) > 0){
right--;
}
if(left <= right){
Collections.swap(values, left++, right--);
}
}
if(low < right){
quickSort(low, right, values);
}
if(left < high){
quickSort(left, high, values);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment