The bubble sort is to take the maximum number out in turn, and then swap to the last edge of the array.
Intuitive notation:
Public Long[] Sort (Long[] a) { intn = a.length-1; //Step:1 Select the maximum number for(inti = 0; I < n; i++){ //swap If the previous number is greater than the last number if(A[i] > a[i+1]) {A[i]= A[i] ^ a[i+1]; A[i+1] = a[i+1] ^A[i]; A[i]= A[i] ^ a[i+1]; }} N--; //Step:2 to elect the second largest number for(inti = 0; I < n; i++){ //swap If the previous number is greater than the last number if(A[i] > a[i+1]) {A[i]= A[i] ^ a[i+1]; A[i+1] = a[i+1] ^A[i]; A[i]= A[i] ^ a[i+1]; }} N--; //Step:3 for(inti = 0; I < n; i++){ //swap If the previous number is greater than the last number if(A[i] > a[i+1]) {A[i]= A[i] ^ a[i+1]; A[i+1] = a[i+1] ^A[i]; A[i]= A[i] ^ a[i+1]; }} N--; //Step:4 for(inti = 0; I < n; i++){ //swap If the previous number is greater than the last number if(A[i] > a[i+1]) {A[i]= A[i] ^ a[i+1]; A[i+1] = a[i+1] ^A[i]; A[i]= A[i] ^ a[i+1]; }} N--; System.out.println ("N=" +N); //Step:5 for(inti = 0; I < n; i++){ //swap If the previous number is greater than the last number if(A[i] > a[i+1]) {A[i]= A[i] ^ a[i+1]; A[i+1] = a[i+1] ^A[i]; A[i]= A[i] ^ a[i+1]; }} N--; returnA; }
Add a For loop:
Public Long[] Sort (Long[] a) { intn = a.length-1; for(intj = 0; J < A.length; J + +){ for(inti = 0; I < n; i++){ //swap If the previous number is greater than the last number if(A[i] > a[i+1]) {A[i]= A[i] ^ a[i+1]; A[i+1] = a[i+1] ^A[i]; A[i]= A[i] ^ a[i+1]; }} N--; } returnA; }
Bubble sort Java Code