Home >Java >javaTutorial >Example of bubble sort algorithm implemented in java
The example in this article describes the bubble sorting algorithm implemented in java. Share it with everyone for your reference, the details are as follows:
public class PaoPaixu { public static void sort(int[] data){ int tmp; for (int i = 0; i < data.length; i++) { for (int j = i+1; j < data.length; j++) { if(data[i]>data[j]){ /*tmp=data[i]; data[i]=data[j]; data[j]=tmp;*/ data[i]=data[i]+data[j]; data[j]=data[i]-data[j]; data[i]=data[i]-data[j]; } } } } public static void main(String[] args) { int[] data={4,2,1,8,9,4,2}; sort(data); for (int i = 0; i < data.length; i++) { System.out.println(data[i]); } } }
I hope this article will be helpful to everyone in java programming.
For more examples of bubble sort algorithm implemented in java, please pay attention to the PHP Chinese website!