Java and java
Zookeeper
Bubble Sorting is the traversal every time. Compare adjacent numbers. The former is greater than the latter, and the maximum value is constantly moved back until it is sunk to the last position. The key point of the algorithm is to determine the boundary of each loop;
The latter two algorithms improve Bubble sorting to a certain extent, but compared with other sorting algorithms, Bubble sorting performance is still poor.
// Bubble sort public class Bubble_Sort {// the most primitive solution public void bubble_sort1 (int [] data) {int n = data. length; for (int I = 0; I <n; I ++) {// pay attention to the index range of the loop to avoid overflow for (int j = 0; j <n-I-1; j ++) {if (data [j]> data [j + 1]) {swap (data, j, j + 1) ;}}}// improve the algorithm by introducing a flag to determine whether a cycle has been moved. If there is no movement, it indicates that // sorting has been completed, public void bubble_sort2 (int [] data) {int n = data. length; boolean flag = true; // indicates whether int index is moved = n-1 ;// Indicates the index of the last digit of the loop. // once moving, the loop continues while (flag) {flag = false; for (int j = 0; j <index-1; j ++) {if (data [j]> data [j + 1]) {swap (data, j, j + 1); flag = true ;}} index --;} // improved algorithm 2: When a traversal is in progress, the last m bit is not converted, it indicates that the next n bits are larger than the current maximum number. // sort by bubble. Each time the values are sunk to the maximum value, the following bits must have sorted public void bubble_sort3 (int [] data) {int n = data. length; int index = n-1; while (index! = 0) {int k = 0; for (int j = 0; j <index-1; j ++) {if (data [j]> data [j + 1]) {swap (data, j, j + 1); k = j ;}} index = k ;}} // The reference implementation cannot be used like C ++, so you have to use the data array to change the private void swap (int [] data, int a, int B) {int temp = data [a]; data [a] = data [B]; data [B] = temp;} public void print_array (int [] data) {for (int num: data) {System. out. print (num); System. out. print ("") ;}} public static void main (String [] args) {Bubble_Sort bubble_Sort = new Bubble_Sort (); int data [] =, 5, 24, 57}; bubble_Sort.bubble_sort3 (data); bubble_Sort.print_array (data );}}