Home  >  Article  >  Java  >  Example of bubble sort algorithm implemented in java

Example of bubble sort algorithm implemented in java

高洛峰
高洛峰Original
2017-01-17 13:26:431347browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn