Home > Java > javaTutorial > body text

How to implement bubble sort algorithm in java?

coldplay.xixi
Release: 2020-06-15 17:17:03
Original
2836 people have browsed it

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.

How to implement bubble sort algorithm in java?

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

The complete code is as shown below:

How to implement bubble sort algorithm in java?

The running results are as follows:

How to implement bubble sort algorithm in java?

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!

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!