Java (C#) code and formula for bubble sort

for(int i=0;i<arr.length-1;i++){//The outer loop controls the number of sorting passes
  for(int j=0;j<arr.length-1-i;j++){//The inner loop controls how many times to sort each time
    if(arr[j]>arr[j+1]){
      int temp=arr[j];
      arr[j]=arr[j+1];
      arr[j+1]=temp;
    }
  }
}

Bubble sort formula:

1. N numbers are queued up, and the two are smaller than the first. 

2.  Outer loop N-1 , inner loop N-1-i

3.  If you want to descend in descending order, just replace the greater than sign in the program with the less than sign.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325402054&siteId=291194637