Home >Java >javaTutorial >How to implement bubble sort algorithm in java?
#How to implement the bubble sort algorithm in java?
How to implement the bubble sort algorithm in java:
Bubble Sort (BubbleSort) is the simplest sorting algorithm. Its basic idea is to iteratively compare the first element to the last element of the input sequence, and exchange the positions of the two elements when the conditions are met. This process continues until there is no need to perform the above process.
We customize a sorting function as sorter(int[]array)
;
private static void sorter(int[] array) for(int i=0;i<array.length-1;i++) { for(int j=0;j<array.length-i-1;j++) { if(array[j]>array[j+1]) { int temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } }
The complete code is as shown below:
The running results are as follows:
Recommended tutorial: "java video tutorial"
The above is the detailed content of How to implement bubble sort algorithm in java?. For more information, please follow other related articles on the PHP Chinese website!