Home > Java > javaTutorial > body text

Example of bubble sort algorithm implemented in java

高洛峰
Release: 2017-01-17 13:26:43
Original
1399 people have browsed it

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]);
    }
  }
}
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!