JAVA judges whether the array is in order

Determine whether the array is ordered

1. Problem

Given an array, judge whether it is in order, and output in order or disorder;

2. Analysis

Similar to array sorting, the arrays are compared from the front to the back. If the elements in the large subscript of the array are smaller than the elements in the small subscript, it is out of order.

3. Code implementation

public class ARR {
    
    
    public static void main(String[] args){
    
    
        int [] arrary = {
    
    12,19,1,25,99,52,3,67,23,15};
        ltest.Printarr(arrary);
        judgeARR(arrary);
        mybubblesort(arrary);
        ltest.Printarr(arrary);
        judgeARR(arrary);



    }
    public static void judgeARR(int [] arr){
    
    
        int len =arr.length;
        int flag = 1;
        for(int i=0;i<len-1;i++){
    
    
            if(arr[i]>arr[i+1]){
    
    

                flag =0;
            }

        }
        if (flag==1){
    
    
            System.out.println("此数组有序");
        }else{
    
    
            System.out.println("此数组无序");
        }
    }

Guess you like

Origin blog.csdn.net/weixin_44712669/article/details/111173284