Bubble sort and quick sort of algorithm

Source: Internet
Author: User
Tags rand

 Packagecom.sort.test;Importjava.util.Arrays;ImportJava.util.Random; Public classVeriablesort {Private Static intmax_number=10;  Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        int[] Array =New int[Max_number]; Random Rand=NewRandom ();  for(inti = 0; i < Max_number; i++) {Array[i]= Rand.nextint (20); }        //the original array sequenceSystem.out.println ("The original array is sorted as follows:"); System.out.println (arrays.tostring (array));//Bubblesort (array);System.out.println ("bubble sorted array is sorted as follows:");        System.out.println (arrays.tostring (array)); QuickSort (Array,0,array.length-1); System.out.println (The quick sorted array is sorted as follows:);    System.out.println (arrays.tostring (array)); }    /**Bubble Sort * Core idea of bubble sort * 1. Sort n-1 times, each sorted by two compare and exchange to get the minimum value of this time in the corresponding position * 2. The second layer of the loop is J>i, because the first I already ordered, there is no need to compare and exchange * 3. The bubbling sort can be optimized when there is a single traversal of the sort that has never been exchanged, * stating that the order of the sequence has been ordered, the first loop can be exited, and the sort ends *@paramArray*/    Private Static voidBubblesort (int[] Array) {        intTmp=0; BooleanIsswapflag =false;  for(inti=0; i<array.length-1;i++){             for(intj = array.length-1;j>i;j--){                if(array[j-1]>Array[j]) {                    //swap adjacent two numbersTMP = Array[j-1]; Array[j-1] =Array[j]; ARRAY[J]=tmp; Isswapflag=true; }            }            //If there is no exchange, the sequence is sorted by order.            if(!Isswapflag) {                 Break; }        }    }    /*** The first element of the array is the keyword, which is bigger than it, and it's smaller than it is in front of it *@paramArray *@paramLeft *@paramRight *@return     */    Private Static intQuickdivide (int[] Array,intLeft,intRight ) {        intBase =Array[left];  while(Left <Right ) {             while(Left < right && Array[right] >=base) { Right--; }            //find smaller than the keyword on the left side of the keywordArray[left] =Array[right];  while(Left < right && Array[left] <=base) { Left++; }            //find bigger than the keyword on the right side of the keywordArray[right] =Array[left]; }        //Left=right when the loop exits, left refers to the position of the keyword is locatedArray[left] =Base; //returns the subscript where the keyword is located        returnLeft ; }    /**Quick Sort * Use recursive method to sort, in fact, the way of thinking of the sorting is divided into the first element of the array is the keyword, bigger than it in the row behind it, than it is small in front of it * and then recursive to the keyword before and after the two sets of sequences sorted *@paramArray *@paramLeft *@param Right*/    Private Static voidQuickSort (int[] Array,intLeft,intRight ) {        if(Left <Right ) {            intBase =quickdivide (array,left,right); QuickSort (Array,left,base-1); QuickSort (Array,base+1, right); }    }}

Bubble sort and quick sort of algorithm

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.