Class QuickSort
- java.lang.Object
-
- smile.sort.QuickSort
-
public class QuickSort extends java.lang.Object
Quicksort is a well-known sorting algorithm that, on average, makes O(n log n) comparisons to sort n items. For large n (say > 1000), Quicksort is faster, on most machines, by a factor of 1.5 or 2 than other O(n log n) algorithms. However, in the worst case, it makes O(n2) comparisons. Quicksort requires a bit of extra memory.Quicksort is a comparison sort. A comparison sort is a type of sorting algorithm that only reads the list elements through a single abstract comparison operation (often a "less than or equal to" operator) that determines which of two elements should occur first in the final sorted list. The only requirement is that the operator obey the three defining properties of a total order:
- if a ≤ b and b ≤ a then a = b (antisymmetry)
- if a ≤ b and b ≤ c then a ≤ c (transitivity)
- a ≤ b or b ≤ a (totalness or trichotomy)
Quicksort, however, is not a stable sort in efficient implementations. Stable sorting algorithms maintain the relative order of records with equal keys. If all keys are different then this distinction is not necessary. But if there are equal keys, then a sorting algorithm is stable if whenever there are two records(let's say R and S) with the same key, and R appears before S in the original list, then R will always appear before S in the sorted list.
For speed of execution, we implement it without recursion. Instead, we requires an auxiliary array (stack) of storage, of length 2 log2 n. When a subarray has gotten down to size 7, we sort it by straight insertion.
- Author:
- Haifeng Li
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int[]
sort(double[] arr)
Sorts the specified array into ascending numerical order.static void
sort(double[] arr, double[] brr)
This is an effecient implementation Quick Sort algorithm without recursive.static void
sort(double[] arr, double[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive.static void
sort(double[] arr, int[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.static void
sort(double[] arr, int[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive.static void
sort(double[] arr, java.lang.Object[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.static void
sort(double[] arr, java.lang.Object[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive.static int[]
sort(float[] arr)
Sorts the specified array into ascending numerical order.static void
sort(float[] arr, float[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.static void
sort(float[] arr, float[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive.static void
sort(float[] arr, int[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.static void
sort(float[] arr, int[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive.static void
sort(float[] arr, java.lang.Object[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.static void
sort(float[] arr, java.lang.Object[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive.static int[]
sort(int[] arr)
Sorts the specified array into ascending numerical order.static void
sort(int[] arr, int[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.static void
sort(int[] arr, int[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive.static void
sort(int[] arr, java.lang.Object[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.static void
sort(int[] arr, java.lang.Object[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive.static <T extends java.lang.Comparable<? super T>>
int[]sort(T[] arr)
Sorts the specified array into ascending order.static <T extends java.lang.Comparable<? super T>>
voidsort(T[] arr, int[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.static <T extends java.lang.Comparable<? super T>>
voidsort(T[] arr, int[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive.static <T> void
sort(T[] arr, int[] brr, int n, java.util.Comparator<T> comparator)
This is an effecient implementation Quick Sort algorithm without recursive.static <T extends java.lang.Comparable<? super T>>
voidsort(T[] arr, java.lang.Object[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.static <T extends java.lang.Comparable<? super T>>
voidsort(T[] arr, java.lang.Object[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive.
-
-
-
Method Detail
-
sort
public static int[] sort(int[] arr)
Sorts the specified array into ascending numerical order.- Returns:
- the original index of elements after sorting in range [0, n).
-
sort
public static void sort(int[] arr, int[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.
-
sort
public static void sort(int[] arr, int[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive. Besides sorting the first n elements of array arr, the first n elements of array brr will be also rearranged as the same order of arr.
-
sort
public static void sort(int[] arr, java.lang.Object[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.
-
sort
public static void sort(int[] arr, java.lang.Object[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive. Besides sorting the first n elements of array arr, the first n elements of array brr will be also rearranged as the same order of arr.
-
sort
public static int[] sort(float[] arr)
Sorts the specified array into ascending numerical order.- Returns:
- the original index of elements after sorting in range [0, n).
-
sort
public static void sort(float[] arr, int[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.
-
sort
public static void sort(float[] arr, int[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive. Besides sorting the first n elements of array arr, the first n elements of array brr will be also rearranged as the same order of arr.
-
sort
public static void sort(float[] arr, float[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.
-
sort
public static void sort(float[] arr, float[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive. Besides sorting the first n elements of array arr, the first n elements of array brr will be also rearranged as the same order of arr.
-
sort
public static void sort(float[] arr, java.lang.Object[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.
-
sort
public static void sort(float[] arr, java.lang.Object[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive. Besides sorting the first n elements of array arr, the first n elements of array brr will be also rearranged as the same order of arr.
-
sort
public static int[] sort(double[] arr)
Sorts the specified array into ascending numerical order.- Returns:
- the original index of elements after sorting in range [0, n).
-
sort
public static void sort(double[] arr, int[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.
-
sort
public static void sort(double[] arr, int[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive. Besides sorting the first n elements of array arr, the first n elements of array brr will be also rearranged as the same order of arr.
-
sort
public static void sort(double[] arr, double[] brr)
This is an effecient implementation Quick Sort algorithm without recursive. Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.
-
sort
public static void sort(double[] arr, double[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive. Besides sorting the first n elements of array arr, the first n elements of array brr will be also rearranged as the same order of arr.
-
sort
public static void sort(double[] arr, java.lang.Object[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.
-
sort
public static void sort(double[] arr, java.lang.Object[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive. Besides sorting the first n elements of array arr, the first n elements of array brr will be also rearranged as the same order of arr.
-
sort
public static <T extends java.lang.Comparable<? super T>> int[] sort(T[] arr)
Sorts the specified array into ascending order.- Returns:
- the original index of elements after sorting in range [0, n).
-
sort
public static <T extends java.lang.Comparable<? super T>> void sort(T[] arr, int[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.
-
sort
public static <T extends java.lang.Comparable<? super T>> void sort(T[] arr, int[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive. Besides sorting the first n elements of array arr, the first n elements of array brr will be also rearranged as the same order of arr.
-
sort
public static <T> void sort(T[] arr, int[] brr, int n, java.util.Comparator<T> comparator)
This is an effecient implementation Quick Sort algorithm without recursive. Besides sorting the first n elements of array arr, the first n elements of array brr will be also rearranged as the same order of arr.
-
sort
public static <T extends java.lang.Comparable<? super T>> void sort(T[] arr, java.lang.Object[] brr)
Besides sorting the array arr, the array brr will be also rearranged as the same order of arr.
-
sort
public static <T extends java.lang.Comparable<? super T>> void sort(T[] arr, java.lang.Object[] brr, int n)
This is an effecient implementation Quick Sort algorithm without recursive. Besides sorting the first n elements of array arr, the first n elements of array brr will be also rearranged as the same order of arr.
-
-