Bubble sorting is to compare two adjacent elements in a group of arrays, which can be arranged in order from large to small or from small to large,
Bubble sorting diagram:
The size of two adjacent numbers is compared once in each cycle. If the sequence is from small to large, the large data is on the right side of the small data
Bubble sorting flow chart
Create an array and assign different values
Bubble sort first cycle
According to the data in the original array, if 100 is compared with 343, then 343 large non exchange positions are not exchanged. If 343 is compared with 66, then 343 large exchange positions are exchanged. At this time, the data of Array[2] is 343 If 343 is compared with 12, the position will be changed. 343 is now in the position of Array[3]. If 343 is compared with 78, the position will be changed. If 343 is in the position of Array[4], it will not be changed compared with 9999
Bubble sort second cycle
The data 100 of Array[0] is compared with 66 to exchange the position, and 100 is compared with 12 to exchange the position. At this time, the data of Array[2] is 100, and then compared with 17 to exchange the position. If the comparison with 343 is less than 343, it will not be exchanged, and 343 and 9999 will not be exchanged
Bubble sort third cycle
The data 66 and 12 of Array[0] are compared to exchange positions, and 66 and 78 are not compared to exchange positions. The final sorting is completed
Code implementation:
Create an array and pass in the initial value:
int[]Array={9999,434,5,321,777,8752};
Create a Sort method, define a formal parameter array, and the return value type is array
public int[] Sort(int[] arr){ return arr; }
Define judgment in Sort method
for (int i=0;i<arr.length;i++){ if (arr[i]>arr[i+1]){ //If the value of the first number is greater than the second number int sum=0; sum=arr[i]; arr[i]=arr[i+1]; //Exchange arr[i+1]=sum; } }
Make test call
Import Arrays package
import java.util.Arrays;
Calling the Sort method in the main method
public static void main(String[] args) { int[]Array={9999,434,5,321,777,8752}; Demo15 D=new Demo15(); System.out.println(Arrays.toString(D.Sort(Array))); }
Execution results:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at Test_Demo1.Demo15.Sort(Demo15.java:11) at Test_Demo1.Demo15.main(Demo15.java:7)
Execution error report result display [ subscript out of bounds ]
The value of i of the for loop statement in the Sort method is less than arr.length-1
for (int i=0;i<arr.length-1;i++){
Execute the program again
[434, 5, 321, 777, 8752, 9999]
Obviously, 5 is smaller than 434, but there is no exchange position, because it only makes a cycle. As mentioned earlier, only one cycle only compares the size of two adjacent numbers and does not compare the whole data. Therefore, we add a layer of cycle outside the for cycle to enable it to compare the data between the whole data
The Sort method code is as follows
public int[] Sort(int[] arr){ for (int j=0;j<arr.length;j++){ for (int i=0;i<arr.length-1;i++){ if (arr[i]>arr[i+1]){ int sum=0; sum=arr[i]; arr[i]=arr[i+1]; arr[i+1]=sum; } } } return arr; }
Execute the program again
[5, 321, 434, 777, 8752, 9999]
This output call uses the toString() method under the Arrays class. We can write a method ourselves for array output
Create Print method
public static int[]Print(int[] Arr){ for(int i =0 ;i<Arr.length;i++){ if(i==0){ System.out.print("["); } if (i==Arr.length-1){ System.out.println(Arr[i]+"]"); }else{ System.out.print(Arr[i]+","); } } return Arr; }
Call and pass parameters in the main method
System.out.println(Print(Sort(Array)));
Execution results:
[5, 321, 434, 777, 8752, 9999] [I@1b6d3586 //hashcode value
A hash code value will be returned during execution because an array returned in the Sort method return is hashcode
Attachment – reference articles:
Detailed explanation of JavaSE - Arrays class in java full stack series 027
java full stack series: JavaSE - array usage 025