16. Quick Sort

Source: Internet
Author: User

public class QuickSort {


Dividing an array
public static int partition (int[] array, int. left,int Right,int Point)
{
int leftptr = left-1;
int rightptr = right;

while (true)
{
Right Shift Left
while (Leftptr<rightptr && array[++leftptr] < point);
Left shift Right
while (Leftptr < rightptr && array[--rightptr]>point);
if (leftptr >= rightptr)
Break
Else
{
int tmp = ARRAY[LEFTPTR];
ARRAY[LEFTPTR] = array[rightptr];
ARRAY[RIGHTPTR] = tmp;
}

}
Switches the keyword to the current point
int tmp = ARRAY[LEFTPTR];
ARRAY[LEFTPTR] = Array[right];
Array[right] = tmp;
return leftptr;
}


Quick Sort
public static void sort (int[] array, int. Left,int right)
{
if (left >= right)
Return
Set keywords
int point = Array[right];
Get pointcuts, divide arrays
int partion = partition (array, left, right, point);
To make a quick row of the left sub-array
Sort (array, left,partion-1);
Right array quick row
Sort (array, partion+1, right);

}

public static void display (int[] array)
{
System.out.print ("[");
Advanced usage of the for (int num:array)//for
{
System.out.print (num+ "");
}
System.out.print ("]");
System.out.println ();
}

}

16. 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.