Created
January 22, 2014 07:56
-
-
Save louisbros/8555014 to your computer and use it in GitHub Desktop.
Java Quicksort
This file contains 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 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