Java Quick Sort Implementation

Source: Internet
Author: User

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 for the data prototype, and 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 pivot, compared to small elements on the left, compared to large elements in the right
Pivot = partition (n, left, right);
Recursive calls to left and right arrays are quickly sorted 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];
Pivot is selected and will never change, eventually in the middle, before the small rear big
while (left < right) {
while (left < right && N[right] >= pivotkey)--right;
Move the element smaller than the pivot to the lower end, at which point the right bit is equal to NULL, waiting for the lower-than-pivotkey 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 empty, waiting for a higher number to be smaller than the pivotkey.
N[right] = N[left];
}
When left = = right, complete a quick sequence, the left bit is equivalent to empty, waiting for PivotKey to fill up
N[left] = PivotKey;
return left;
}
}

Java Quick Sort Implementation

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.