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)