Java enables quick sorting

Source: Internet
Author: User

<pre name= "code" class= "Java" >package Com.test.linked;public class QuickSort {public class array{private int[] myarray;private int size;public Array (int max) {myarray=new int[max];size=0;} /** * Insert Data * @param value */public void insert (int value) {myarray[size]=value;size++;} /** * Sort */public void sort () {requicksort (0,size-1);} /** * Sorting, using recursion to divide the array into smaller two arrays * @param left * @param right */public void requicksort (int left,int right) {if (right-left<=0) { return;} Else{int center=myarray[right];int Partition=partitionarray (left,right,center); Requicksort (left,partition-1); Requicksort (Partition+1,right);}} /** * Define HUB position * @param left * @param right * @param center * @return */public int partitionarray (int left,int Right , int center) {int Leftpart=left-1;int rightpart=right;while (leftpart<rightpart) {while (myarray[++leftpart]< Center&&leftpart<right);//Find a larger number while (rightpart>=left&&myarray[--rightpart]>) than the center Center)//Find a smaller number if (Leftpart>=rightpart) {break;} Else{swap (Leftpart,rightpart)///Two number Exchange}}swap (leftpart,right);//re-record the location of the hub return leftpart;// Because left and right exchange so return leftpart}/** * Exchange data * @param leftpart * @param rightpart */public void swap (int leftpart,int rightpart) { int Temp=myarray[leftpart]; Myarray[leftpart]=myarray[rightpart]; Myarray[rightpart]=temp;} /** * Display array */public void display () {System.out.println ("array Begin"), for (int i = 0; i < size; i++) {System.out.println (Myarray[i]);}} public static void Main (string[] args) {int max=10; QuickSort sort=new QuickSort (); Array test=sort.new Array (int i = 0; I <max; i++) {Test.insert ((int) (Math.random () *87));} Test.display (); Test.sort (); Test.display ();}}

Data:
<pre name= "code" class= "java" >array Begin218104846867630172array begin021721304648768186


Java enables quick sorting

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.