From today's review!
Last night thought of a question: bubble sort is not possible with a return value type it?
When the exam is so written, but not write, turned back the notes, I found to be possible.
Knock code to deepen under the impression:
/ ** * @date: 2019-11-24 08:40 * @King:!! No Blood No No Bone Ash !!! * / public class TestBubbleSortIII { public static void main (String [] args) { / ** * define an array * / int [] array 12,21,16,5,36,22,2,9,66 = { }; / ** * sorting the way before the first output, for practicing the loop through the array * / System.out.println ( "front sort:" ); for ( int I = 0; I <be array.length; I ++ ) { of System.out.print (Array [I] + "" ); } System.out.println (); / ** * Call sorted * / ; bubbleSort (Array) / ** * practice the for-each loop through the array * / (System.out.println "sorted:" ); for ( int I: Array) { the System.out. Print (I + "" ); } } / ** * return value type bubble sort * / public static int [] bubbleSort ( int [] Array) { / ** * for the outer loop - the number of sorting times * / for ( int I = 0; I <be array.length; I ++ ) { / ** * define a logo, the sorting process is determined whether the position of the exchange element occurs, the default false means no exchange * (the beginning of the array represents false feel default disorder, thought to be behind the confusion determination) * / Boolean In Flag = to false ; / ** * compare the size of the inner loop of adjacent elements, then the switch position of the former than the latter * Comparative each trip, i + 1, j - 1 , j and i can relationship with j <array .length - 1 - i represents * / for ( int J = 0; J <be array.length -. 1 - I; J ++ ) { IF (Array [J]> Array [J +. 1 ]) { int TEMP = Array [J . 1 + ]; array [J +. 1] = array [J]; array [J] = TEMP; in Flag = to true ; } } / ** IF * Cycling trip down the position switching element does not occur in the array - the array has been ordered, the end of the loop * / (! In Flag) { BREAK ; } } return Array; } }
Output:
Before sorting:
122,116,536,222,966
sorted:
259,121,621,223,666
Summary: thought to live, but also to withstand verification.