Problem Solving steps:
- Use a For Loop (outer loop) to specify the number of laps to loop (by illustration, the number of laps in an array loop is 1)
- In each lap, a for loop (inner Loop) completes the adjacent element values in order to compare the large values to the subsequent elements
- The number of cycles per circle is determined by the cycle of the first circle. As shown
A) the number of inner loops is an array length-1 when the first loop element is compared
b) in the second loop element comparison, the inner loop number of the array length-2
c) and so on, it is concluded that the number of inner loops is an array length when the nth loop element is compared-n
//Bubble Sort Public Static voidBubblesort (int[] arr) { //function//the outer loop is used to control the number of loops in the array for(inti = 0; i < arr.length-1; i++) { //J < Arr.length-1 to avoid the crossing of the corner Mark//J < Arr.length-1-i to compare efficiency and avoid repeated comparisons//The inner loop is used to compare the values of the elements and to swap the values of the large elements back for(intj = 0; J < Arr.length-1-i; J + +) { if(Arr[j] > arr[j+1]) { inttemp =Arr[j]; ARR[J]= Arr[j+1]; Arr[j+1] =temp; } } }}
Sort-bubble sort