Java Quick Sort

Source: Internet
Author: User

First, quick sort

1. Upgraded bubble sort + recursion

2. Compare when selecting a comparison pivot object so that its left is less than it, and the right side is greater than it.

3. Re-order data recursively on both sides of the pivot object

Second, the code example

public class QuickSort

{


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,

Wait 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 NULL,

Waiting for a higher number to be smaller than the pivotkey.

N[right] = N[left];

}

When left = = right, complete a quick sort of a trip,

At this point the left bit is equivalent to empty, waiting for PivotKey to fill

N[left] = PivotKey;

SYSTEM.OUT.PRINTLN ("Pivot key:" + left);

System.out.println ("Current value:" + arrays.tostring (n));

return left;

}


/*

* 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 void Main (string[] args)

{

int[] Array =

{3,4,2,6,7,1,4,1,2,4};

QuickSort (array, 0, array.length-1);

for (int i = 0; i < array.length; i++)

{

System.out.println (Array[i]);

}

}

}



Java Quick Sort

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.