Skip to content

Instantly share code, notes, and snippets.

@pedrofurtado
Created January 3, 2016 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedrofurtado/3e5cc1cef723cbef1eb6 to your computer and use it in GitHub Desktop.
Save pedrofurtado/3e5cc1cef723cbef1eb6 to your computer and use it in GitHub Desktop.
Implementation of Quick Sort in Java.
/**
* @file
* Quick sort class.
*/
public class QuickSort {
/**
* Printing vector method.
*
* Prints on screen the elements of the vector, from first to last element.
*
* @param int[] a
* Vector of integers.
* @return void
*/
public static void print_vector(int[] a) {
if (a == null) {
System.out.println("Null vector.");
return;
}
System.out.println();
System.out.print("< ");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + ", ");
}
System.out.print(" >");
System.out.println();
}
/**
* Partition method.
*
* Makes a logical division in vector, separating (from a defined pivot),
* the elements lesses or equals than pivot on it left side
* and the biggers or equals elements on it right side of vector.
*
* @param int[] a
* Vector of integers.
* @param int i
* Initial vector index to start the logical partition as from of pivot.
* @param int j
* Last vector index to start the logical partition as from of pivot.
* @return
* The vector index when the partition has been made.
*/
public static int partition(int[] a, int i, int j) {
int pivot = a[i];
int begin = i + 1;
int end = j;
while (begin <= end) {
if (a[begin] <= pivot) {
begin++;
}
else if (pivot < a[end]) {
end--;
}
else {
int aux = a[begin];
a[begin] = a[end];
a[end] = aux;
begin++;
end--;
}
}
a[i] = a[end];
a[end] = pivot;
return end;
}
/**
* Partition method.
*
* Makes a logical division in vector, separating (from a defined pivot),
* the elements biggers or equals than pivot on it left side
* and the lesses or equals elements on it right side of vector.
*
* @param int[] a
* Vector of integers.
* @param int i
* Initial vector index to start the logical partition as from of pivot.
* @param int j
* Last vector index to start the logical partition as from of pivot.
* @return
* The vector index when the partition has been made.
*/
public static int partition2(int[] a, int i, int j) {
int pivot = a[i];
int begin = i + 1;
int end = j;
while (begin <= end) {
if (a[begin] >= pivot) {
begin++;
}
else if (pivot > a[end]) {
end--;
}
else {
int aux = a[begin];
a[begin] = a[end];
a[end] = aux;
begin++;
end--;
}
}
a[i] = a[end];
a[end] = pivot;
return end;
}
/**
* Quick sort.
*
* Recursive version.
*
* Sort order: ascending.
*
* @param int[] a
* Vector of integers.
* @param int i
* Initial index of vector.
* @param int j
* Last index of vector.
* @return void
*/
public static void quick_sort_ascending(int[] a, int i, int j) {
if (a == null) return;
if (i < j) {
int pivot = partition(a, i, j);
quick_sort_ascending(a, i, pivot - 1);
quick_sort_ascending(a, pivot + 1, j);
}
}
/**
* Overloading of quick_sort_ascending() method.
*
* Simplifies the call of method.
*/
public static void quick_sort_ascending(int[] a) {
quick_sort_ascending(a, 0, a.length - 1);
}
/**
* Quick sort.
*
* Recursive version.
*
* Sort order: descending.
*
* @param int[] a
* Vector of integers.
* @param int i
* Initial index of vector.
* @param int j
* Last index of vector.
* @return void
*/
public static void quick_sort_descending(int[] a, int i, int j) {
if (a == null) return;
if (i < j) {
int pivot = partition2(a, i, j);
quick_sort_descending(a, i, pivot - 1);
quick_sort_descending(a, pivot + 1, j);
}
}
/**
* Overloading of quick_sort_descending() method.
*
* Simplifies the call of method.
*/
public static void quick_sort_descending(int[] a) {
quick_sort_descending(a, 0, a.length - 1);
}
/**
* Examples of use.
*/
public static void main(String args[]) {
int[] a = { 24, 4, 67, 4, 3, 5, 6, 6, 7, 34, 12, 12, 24, 35, 2, 24, 45, 56, 78 };
quick_sort_descending(a);
print_vector(a);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment