Check if an array is sorted, return true or false

前端 未结 13 1985
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 06:03

I am writing an easy program the just returns true if an array is sorted else false and I keep getting an exception in eclipse and I just can\'t figure out why. I was wonder

13条回答
  •  悲&欢浪女
    2020-11-30 06:55

    If you want to check if the array is sorted DESC or ASC:

    boolean IsSorted(float [] temp)
    {
        boolean result=true,result2=true;
        for (int i = 0; i < temp.length-1; i++)  
            if (temp[i]< temp[i + 1]) 
                    result= false;
    
        for (int i = 0; i < temp.length-1; i++)  
            if (temp[i] > temp[i + 1])   
                result2= false;
    
        return result||result2;
    }
    

提交回复
热议问题