Home >Java >javaTutorial >How to implement bubble sort algorithm in java?

How to implement bubble sort algorithm in java?

coldplay.xixi
coldplay.xixiOriginal
2020-06-15 17:17:032888browse

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;               
               }        
                   }     
                      }    }

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!

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