Home > Java > javaTutorial > How to implement array sorting in java

How to implement array sorting in java

王林
Release: 2023-04-19 16:25:20
forward
1384 people have browsed it

Array sorting (bubble sort)

public class TestDemo {
    public static void bubbleSort(int[] array){
        for (int i = 0; i <array.length-1 ; i++) {
            boolean flg = false;
            for (int j = 0; j <array.length-1-i ; j++) {
                if(array[j]>array[j+1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1]= tmp;
                    flg = true;
                }
            }
            if(flg = false){
                return;
            }
 
        }
    }
    public static void main(String[] args) {
        int[] array = {12,1,23,15,16,13,17};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
 
 
    }
}
Copy after login

Print results:

How to implement array sorting in java

##Java sorting array function:

How to implement array sorting in java

Filling function:

How to implement array sorting in java

This function can be followed by three parameters:

How to implement array sorting in java

General The situation is all closed on the left and open on the right [2,6) form

The above is the detailed content of How to implement array sorting in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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