JAVA Implementation of the Quick Sort Algorithm

Source: Internet
Author: User

My quick sortingAlgorithmJava implementation.

View code

 1   /*  2   * Purpose: to implement a quick Sorting Algorithm  3   * Function: sorts a group of data.  4   * Author: Chen peirui  5   * Time: 2013.04.04  6   *  7   * Experience accumulation: 8   * 1. Write the IF () Statement in advance in recursive functions to prevent missing write;  9    */  10   Package  Part02.chapter07;  11   12   Import  Java. Text. simpledateformat;  13   Import  Java. util. date;  14   Import Java. util. Random;  15   Import  Java. util. vendor;  16   17   Public   Class  _ 1 exercise {  18   19       Public   Static   Void  Main (string [] ARGs ){  20 System. Out. println ("Enter the data volume you want to generate (an integer ):");  21 Export myworkshop = New  Using (system. In );  22           Int Array_length = Myrule. nextint ();  23           Int [] A = New   Int  [Array_length];  24 System. Out. println ("Enter the maximum value in the data (expressed as an integer ):" ); 25           Int Array_max = Myrule. nextint ();  26 System. Out. println ("Enter the minimum value (integer) in the data ):" );  27           Int Array_min = Myrule. nextint ();  28 Random ran = New  Random ();  29 System. Out. println ("the randomly generated data is as follows :" );  30          For ( Int I = 0; I <array_length; I ++ ){  31 A [I] = ran. nextint (array_max-array_min + 1) + Array_min;  32 System. Out. Print (A [I] + "" );  33   }  34   System. Out. println ();  35 Simpledateformat SDF = New Simpledateformat ("HH: mm: SS" );  36 System. Out. println (SDF. Format ( New  Date ()));  37 Quicksort = New  Quicksort ();  38 Quicksort. Sort (A, 0, array_length-1 );  39 System. Out. println (SDF. Format ( New  Date ()));  40  System. Out. println ();  41 System. Out. println ("the data after fast sorting is as follows :" );  42           For ( Int I = 0; I <array_length; I ++ ){  43 System. Out. Print (A [I] + "" );  44   }  45   }  46  47   }  48   49   //  Quick sorting  50   Class  Quicksort {  51       Public   Void Sort ( Int [], Int P, Int  R ){  52          If (P <r ){ //  Pay attention to the determination of recursive Conditions  53               Int J = Partition (A, P, R );  54 Sort (A, P, J-1 );  55 Sort (A, J + 1 , R );  56   }  57   }  58  59       Public   Int Partition ( Int [], Int P, Int  R ){  60           Int I = p-1 ;  61           //  To prevent the for statement from getting the value of a [R] Every time  62           Int X = A [R]; 63           Int Temp = 0 ;  64           For ( Int J = P; j <r; j ++ ){  65               If (A [J] < X ){  66 I ++ ;  67 Temp = A [I];  68 A [I] = A [J];  69 A [J] = Temp;  70   }  71   }  72           //  Exchange the value of the lower mark of I + 1 with a [J]  73 A [R] = A [I + 1 ];  74 A [I + 1] = X;  75          Return I + 1 ;  76   }  77   }  78   /**  79   * A sample output:  80   * Enter the data volume you want to generate (in integer format ):  81   10  82   Enter the maximum value in the data (expressed in integer ):  83  20  84   Enter the minimum value (an integer) in the data ):  85   1  86   The randomly generated data is as follows:  87   4 11 3 13 15 7 3 13 3 16  88   The data after quick sorting is as follows:  89   3 3 3 4 7 11 13 13 15 16  90   */ 

 

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.