Understanding of quick Sort in Java and examples

Source: Internet
Author: User

The idea of the so-called quick sort is to first take out the first number of the array as a key, set a i,j as the identity before and after each, and then take the key to the array from behind, and j--, until we find the first number less than this key, and then exchange the two values, after the exchange is complete , we take this key to go backwards from I, and i++, loop until the end of the i=j, when this is over, we will find that the value greater than this key will go to the back of the key, not if you write the wrong, less than this key will run to the front of this value Then we can complete the ordering of the entire array by invoking the array recursively.

Represented by a graphical representation of the following:

This will be divided into two segments of the key, we will take these two paragraphs further to solve the problem.

The code is as follows:

Package test;

public class Testquicksort {

public static void Main (string[] args)
{
TODO automatically generate method stubs
Quicksort qs = new Quicksort ();
int data[] = {44,22,2,32,54,22,88,77,99,11};
Qs.data = data;
Qs.sort (0, qs.data.length-1);
Qs.display ();
}

}


Class Quicksort
{
public int data[];

private int partition (int sortarray[],int low,int hight)
{
int key = Sortarray[low];

while (Low{
while (Lowhight--;
Sortarray[low] = Sortarray[hight];

while (Lowlow++;
Sortarray[hight] = Sortarray[low];
}
Sortarray[low] = key;
return low;
}

public void sort (int low,int hight)
{
if (low{
int result = partition (Data,low,hight);
Sort (low,result-1);
Sort (result+1,hight);
}

}

public void display ()
{
for (int i=0;i<data.length;i++)
{
System.out.print (Data[i]);
System.out.print ("");
}
}

}

Understanding of quick Sort in Java and examples

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.