|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sf.jsort.sort.AbstractSort
net.sf.jsort.sort.QuickSort
public class QuickSort
This is a implementation of QuickSort algorithm.
The algorithm follows the idea of divide and conquer, so it has a recursive approach. The recursion is over the quicksort method, which call the partition method. The method partition starts with a pivot and reorganize the original array such that 2 others arrays appers: the first one contains elements less that pivot, and the second one contains elements greater than pivot.
This reorganization is done recursively for the first and second piece. Check method partion about the influence of the input over the complexity.
Constructor Summary | |
---|---|
QuickSort()
|
Method Summary | |
---|---|
protected int |
partition(java.lang.Object[] list,
java.util.Comparator comparator,
int start,
int end)
It method is the main part of the algorithm. |
void |
sort(java.lang.Object[] list,
java.util.Comparator comparator,
int start,
int end)
Sort a specific range of the list, using the specified comparator. |
Methods inherited from class net.sf.jsort.sort.AbstractSort |
---|
sort, sort, sort, sort, sort, sort, sort |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public QuickSort()
Method Detail |
---|
public void sort(java.lang.Object[] list, java.util.Comparator comparator, int start, int end)
CompareSort
sort
in interface CompareSort
sort
in class AbstractSort
list
- to be sortprotected int partition(java.lang.Object[] list, java.util.Comparator comparator, int start, int end)
It method is the main part of the algorithm. It starts with the definition of the pivot (the last element of the list) and comparing all elements with the pivot, it creates 2 segments, those elements less than and greater than. The counter i keep track of the position of the last position of a less than pivot element.
The overal complexity of the algorithm depends on the split of the original array in 2. But this depend on the "selectivity" of the pivot been used, if the pivot splits the array in some proportion (in fact any proportion) the average complexity O(n * ln(n)) is garanteed.
For the sake of the stability, this implementation select the pivot at the end of the list. Using this approach the worst case is when the array is already selected, this will lead the pivot to split the original array in an array with n-1 elements and 1 element (itself), which will make n comparison with n elements, or O(n ^ 2).
list
- the elements to be orderedcomparator
- the element that define what is "greater than" semanticsstart
- first position of the list to be included in sortingend
- last position of the list to be included in sorting
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |