Bubble sort principle, below in ascending order for example
- Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
- For each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last pair, at this time, the final element is the largest.
- Repeat the above steps for all elements, except for the last one.
- Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.
1 Public classbubblesort{2 Public Static voidMain (string[] args) {3 intScore[] = {67, 69, 75, 87, 89, 90, 99, 100};4 for(inti = 0; i < score.length-1; i++) {//up to do n-1 sort of trip5 for(intj = 0; J < score.length-i-1; J + +) {//sort the current unordered interval score[0......length-i-1] (the range of J is critical, the range is gradually shrinking)6 if(Score[j] < Score[j + 1]) {//swap the small value to the back7 inttemp =Score[j];8SCORE[J] = score[j + 1];9Score[j + 1] =temp;Ten } One } ASystem.out.print ("First" + (i + 1) + "secondary" results: "); - for(intA = 0; A < Score.length; a++){ -System.out.print (Score[a] + "\ T"); the } -System.out.println (""); - } -System.out.print ("Final sort result:"); + for(intA = 0; A < Score.length; a++){ -System.out.print (Score[a] + "\ T"); + } A } at}
Java Bubble sort