The principle of fast sorting algorithm and Java recursive implementation _java

Source: Internet
Author: User

A quick sort of an improvement to bubble sort, if the initial record sequence is ordered or ordered by the keyword, degenerate to bubble sort. Using the recursive principle, the average performance of all order O (n longn) is the best. In terms of average time, is currently considered the best kind of internal sorting method

The basic idea is to divide the data into two separate parts by a lie sort a part of all the data is smaller than the other part of all the data, and then the two parts of the data for the rapid sorting, the entire sequencing process can be recursive, so as to achieve the entire data into an ordered sequence.

Three pointers: The first pointer is called the PivotKey Pointer (pivot), and the second and third pointers are left and right pointers, pointing to the leftmost and rightmost values respectively. The left and right pointers approach the middle at the same time from both sides, and in the process of approximation, they move to the lower end of the element than the pivot, moving the elements larger than the pivot to the high-end, and the pivot is chosen forever, eventually in the middle, and before the small.

Requires two functions:

① recursive function public static void QuickSort (int[]n, int left,int right)
② Split function (a quick sort function) public static int partition (int[]n, int left,int right)

Java source code (successfully run):

Copy Code code as follows:

Package testsortalgorithm;

public class QuickSort {
public static void Main (string[] args) {
int [] array = {49,38,65,97,76,13,27};
QuickSort (array, 0, array.length-1);
for (int i = 0; i < Array.Length; i++) {
System.out.println (Array[i]);
}
}
/* First write the algorithm according to the array data prototype, then write the extensibility algorithm. Array {49,38,65,97,76,13,27}
* */
public static void QuickSort (int[]n, int left,int right) {
int pivot;
if (left < right) {
Pivot as a pivot, smaller than the elements in the left, compared to the larger elements in the right
Pivot = partition (n, left, right);
Recursive calls to the left and right arrays are sorted correctly until the order is completely correct
QuickSort (n, left, pivot-1);
QuickSort (n, pivot + 1, right);
}
}

public static int partition (int[]n, int left,int right) {
int pivotkey = N[left];
The pivot is chosen forever, and finally in the middle, the front is small after the big
while (left < right) {
while (left < right && N[right] >= pivotkey)--right;
will be smaller than the pivot of the elements moved to the lower end, right bit equivalent to empty, waiting for the lower than the PivotKey large number to fill up
N[left] = N[right];
while (left < right && N[left] <= pivotkey) ++left;
Move the element larger than the pivot to the high end, at which point the left bit is equal to the empty, waiting for the high position to be smaller than the pivotkey.
N[right] = N[left];
}
When left = right, complete a quick sort, at which point the left bit is equivalent to empty, waiting for PivotKey to fill up
N[left] = PivotKey;
return to left;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.