Example of bubble sort algorithm implemented by java

  • 2020-05-27 05:38:28
  • OfStack

The example of this paper describes the bubble sort algorithm implemented by java. I will share it with you for your reference 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 has been helpful to you in java programming.


Related articles: