Bubble sort (inside sort)

Source: Internet
Author: User

Is mainly the comparison exchange of 2 adjacent records
1  PackageCom.trfizeng.changesort;2 3 /**4  * @authorTrfizeng Internal Sort Exchange sort-bubble sort (Bubble sort)5  */6  Public classBubblesort {7      Public Static int[] Bubblesort (int[] Array) {8         if(Array! =NULL&& Array.Length! = 0) {9             //How many times does the outermost loop control take?Ten              for(inti = 0; i < Array.Length; i++) { One                 //The inner loop controls how many times each trip needs to be compared, when the inner loop is finished, the largest record has been moved to the end of the current cycle length, and the next one is split he no longer compares with him, the large number sink A                  for(intj = 0; J < array.length-i-1; J + +) { -                     //Swap If you find that the subsequent record is larger than the current record -                     if(Array[j] > array[j + 1]) { the                         inttemp =Array[j]; -ARRAY[J] = array[j + 1]; -Array[j + 1] =temp; -                     } +                 } -             } +         } A         returnArray; at     } -}
View Code
1  Packagecom.trfizeng.test;2 3 ImportCom.trfizeng.changesort.BubbleSort;4 ImportCom.trfizeng.insertionsort.StraightInsertionSort;5 ImportCom.trfizeng.selectionsort.SimpleSelectionSort;6 7 /**8 * Test Class9  * Ten  * @authorTrfizeng One  *  A  */ -  Public classSorttest { -     //Array to sort the     Static int[] Array =New int[] {6, 1, 4, 10, 11, 8, 7, 1, 0 }; -  -     /** - * Direct insertion sequencing test +      */ -      Public Static voidstraightinsertionsorttest () { +System.out.print ("Array to sort: ["); A          for(inti = 0; i < Array.Length; i++) { atSystem.out.print (Array[i] + ""); -         } -System.out.print ("]"); -  -Array =Straightinsertionsort.straightinsertionsort (array); -System.out.print ("Sorted array: ["); in          for(inti = 0; i < Array.Length; i++) { -System.out.print (Array[i] + ""); to         } +System.out.print ("]"); -     } the  *     /** $ * Select SortPanax Notoginseng      */ -      Public Static voidSimpleselectionsort () { theSystem.out.print ("Array to sort: ["); +          for(inti = 0; i < Array.Length; i++) { ASystem.out.print (Array[i] + ""); the         } +System.out.print ("]"); -  $Array =Simpleselectionsort.simpleselectionsort (array); $System.out.print ("Sorted array: ["); -          for(inti = 0; i < Array.Length; i++) { -System.out.print (Array[i] + ""); the         } -System.out.print ("]");Wuyi     } the  -     /** Wu * Bubble Sort -      */ About      Public Static voidBubblesort () { $System.out.print ("Array to sort: ["); -          for(inti = 0; i < Array.Length; i++) { -System.out.print (Array[i] + ""); -         } ASystem.out.print ("]"); +  theArray =Bubblesort.bubblesort (array); -System.out.print ("Sorted array: ["); $          for(inti = 0; i < Array.Length; i++) { theSystem.out.print (Array[i] + ""); the         } theSystem.out.print ("]"); the     } -  in      Public Static voidMain (string[] args) { the         //sorttest.straightinsertionsorttest (); the  About         //Sorttest.simpleselectionsort (); the  the Sorttest.bubblesort (); the     } +}
View Code

Bubble sort (inside sort)

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.