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

How to implement array sorting in java

王林
王林forward
2023-04-19 15:51:341314browse

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

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete