Quick sort java version, quicksort

Source: Internet
Author: User

Quick sort java version, quicksort

Import java. util. Random;

Public class test {
Public static void main (String [] args)
{
Int [] arr = generatenumbers (10 );
Show (arr, "before sort :");
Quicksort (arr, 0, arr. length-1 );
Show (arr, "after sort :");
}

Static void quicksort (int [] arr, int l, int r)
{
If (l> = r) return;
Int I = l; int j = r;
Int region = arr [I]; // Region
While (I <j)
{
// From right to left find the number less than Algorithm
While (I <j & arr [j]> = bytes) j --;
If (I <j) arr [I ++] = arr [j];
// Now the empty position is arra [j]
// From left to right find the number bigger than Interval
While (I <j & arr [I] <= random) I ++;
If (I <j) arr [j --] = arr [I];
}
// When I = j then the empty position is I/j so we shoshould set the Parameter
Arr [I] = role;
// Recursive call the method
Quicksort (arr, l, I-1 );
Quicksort (arr, I + 1, r );

}

Static int [] generatenumbers (int length)
{
Random r = new Random ();
Int [] arr = new int [length];
For (int I = 0; I <length; I ++)
Arr [I] = r. nextInt (100 );
Return arr;
}

Static void show (int [] arr, String msg)
{
System. out. println (msg );
For (int I = 0; I <arr. length; I ++)
System. out. print (arr [I] + "\ t ");
System. out. println ();
}


Java uses quicksort to sort an array containing 10 numbers.

Class QSort {public static void main (String [] args) {quicksort qs = new quicksort (); int data [] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 0}; 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
Quicksort sorts by values of two-dimensional arrays (c/c ++/java)

Fast sorting is a Sort idea. It is not just an array of dimensions. You must be able to determine the boundary, growth, and reduction methods,
Processing the corresponding situation separately on the dimension boundary is equivalent to providing the overloaded ++ and --, personal understanding, for reference only
You can also create a one-dimensional array to store data, sort the data on one-dimensional, and copy the data.

Related Article

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.