Fast sorting algorithm
definition
Fast sequencing is a sort of algorithm developed by Donny Holl. On average, sort n items to 0 (n log n) comparisons. In the worst case scenario, a 0 (N2) comparison is required, but this is not a common situation. In fact, fast sequencing is usually much faster than the other 0 (n log n) algorithms because its internal loop (inner Loop) can be implemented efficiently on most architectures.
Quick sort uses the divide-and-conquer (Divide and conquer) strategy to divide a serial (list) into two sub-serial (sub-lists).
algorithm Steps
1 Select an element from the series, called the "Datum" (pivot),
2 Reorder the columns, where all elements are placed in front of the datum in a smaller position than the base value, and all elements are larger than the base value behind the datum (the same number can be on either side). After the partition exits, the datum is in the middle of the sequence. This is called partition (partition) operation.
3 recursively (recursive) sorts sub-columns that are less than the base value element and that are larger than the base value element.
At the bottom of the recursive scenario, the size of the sequence is 0 or one, which is always sorted. Although it is always recursive, the algorithm always exits, because in each iteration (iteration), it will at least put an element to its last position.
Complexity of Time
Average O (nlogn), Worst O (n^2)
Code
Engaged in a morning to get a program, reluctantly passed, first save, and then optimize.
Package com.sprd.test.algorithm;
Import java.util.ArrayList;
Import java.util.List;
public class QuickSort {public int[] sort (int[] datas) {if (Datas.length = = 0 | | datas.length = = 1) return datas;
List<integer> left = new arraylist<integer> ();
list<integer> right = new arraylist<integer> ();
Random random = new random ();
int pivotindex = Random.nextint (datas.length);
int pivot = datas[0];
for (int i = 1; i < datas.length; i++) {if (Datas[i] <= pivot) {Left.add (datas[i]);
} else {Right.add (datas[i]);
}} int[] sorted = new Int[datas.length];
int[] Leftarray = new int[left.size ()];
for (int i = 0; i < left.size (); i++) {Leftarray[i] = Left.get (i);
} Leftarray = sort (Leftarray);
int[] Rightarray = new int[right.size ()];
for (int i = 0; i < right.size (); i++) {Rightarray[i] = Right.get (i);
} Rightarray = sort (Rightarray);
int index = 0; for (int i = 0; i < leftarray.length;
i++) {sorted[index++] = Leftarray[i];
} sorted[index++] = pivot;
for (int i = 0; i < rightarray.length; i++) {sorted[index++] = Rightarray[i];
} return sorted; } public void Recurrence () {} public static void Main (string[] args) {int[] datas = {4, 1, 2, 56, 24, 5, 6, 97,
8, 0, 3};
Long start = System.currenttimemillis ();
int[] sorted = new QuickSort (). sort (datas);
Long end = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("Time Consuming:" + (End-start));
System.out.println ("Before sorting:");
Print (datas);
System.out.println ("After sorting:");
print (sorted); public static void print (int[] datas) {for (int i = 0; i < datas.length; i++) {System.out.print (Datas[i] + "
");
} System.out.println ("");
}
}
Output
Time: 4946
before sorting:
4 1 2 5 6 $8 0 3
after sorting: