检查数组是否已排序,返回 true 或者 false

我写了一个简单的程序,刚刚返回 true, 如果阵列否则排序 false, 我继续出现一个例外 eclipse, 我只是无法理解为什么。 我想知道有人可以看待我的代码,似乎解释为什么我得到一个例外 array out of bounds.


public static boolean isSorted/int[] a/ 
{
int i;
for/i = 0; i < a.length; i ++/;{
if /a[i] < a[i+1]/ {
return true;
} else {
return false;
}
}
}
public static void main/String[] args/
{
int ar[] = {3,5,6,7};
System.out.println/isSorted/ar//;
}
已邀请:

裸奔

赞同来自:

让我们来看看你构建的循环的清洁版本:


for /i = 0; i < a.length; i++/; { 
if /a[i] < a[i + 1]/ {
return true;
}
else {
return false;
}
}


首先,我必须在源周期中指定一个语法错误。 即,逗号有一个点 /
;

/ 在图括号之前 /
{

/, 它推出循环体。 必须删除此分号。
另请注意,我重新格式化代码的代码以使其更可读。

现在让我们讨论在您的周期内发生的事情。 周期迭代器
i

首先
0

并结束
a.length - 1

. 只要
i

函数作为您的阵列索引,是有意义的,表明这一点
a[0]

是第一个元素,和
a[a.length - 1]

- 您的阵列的下一个元素。 但是,在您的循环的身体中,您还写了一个索引
i + 1

. 它的意思是 , 如果什么
i

一样
a.length - 1

, 然后你的索引是平等的
a.length

, 哪个超出了阵列的边界。

功能
isSorted

因为它回归也存在重大问题 true 首次
a[i] < a[i+1]

和 false 这是第一次,当它不是那么; 因此,它实际上没有检查数组是否完全分类! 最有可能,他只检查前两个记录的排序。

具有相似逻辑的函数,但检查数组是否真实排序,是


public static boolean isSorted/int[] a/ {
// Our strategy will be to compare every element to its successor.
// The array is considered unsorted
// if a successor has a greater value than its predecessor.
// If we reach the end of the loop without finding that the array is unsorted,
// then it must be sorted instead.

// Note that we are always comparing an element to its successor.
// Because of this, we can end the loop after comparing
// the second-last element to the last one.
// This means the loop iterator will end as an index of the second-last
// element of the array instead of the last one.
for /int i = 0; i < a.length - 1; i++/ {
if /a[i] > a[i + 1]/ {
return false; // It is proven that the array is not sorted.
}
}

return true; // If this part has been reached, the array must be sorted.
}

窦买办

赞同来自:

对于那些使用的人 Java 8 以上,这是一个简单的单行选项:


public static boolean isSorted/int[] array/ {
return IntStream.range/0, array.length - 1/.noneMatch/i -> array[i] > array[i + 1]/;
}


或逻辑上等同的替代:


public static boolean isSorted/int[] array/ {
return IntStream.range/0, array.length - 1/.allMatch/i -> array[i] <= array[i + 1]/;
}

郭文康

赞同来自:

用这个表达式
a[i+1]

, 您从阵列的末尾运行。

如果必须与以下项目进行比较,请停止迭代 1 元素早些时候 /并删除逗号点 Java 将被解释为循环体
for

/:


// stop one loop early ---v v--- Remove semicolon here
for/i = 0; i < a.length - 1; i ++/{

莫问

赞同来自:

int i;
for/i = 0; i < a.length - 1 && a[i] < a[i+1]; i++/{}
return /i == a.length - 1/;


只指的是阵列的元素,最终条件的最后一部分不是
如果第一部分是假的,则处理

停止第一个未排序的元素

董宝中

赞同来自:

a[i+1]

什么时候
i == a.length

给你这个错误。

例如,在数组长度中 10 你有元素 0 到 9.


a[i+1]

, 什么时候
i

一样 9, 展示
a[10]

, 哪个在边界之外。

使固定:


for/i=0; i < a.length-1;i++/


此外,您的代码一旦调用,您的代码就不会检查整个数组。 return, 检查周期已完成。
您只需检查第一个值,只需第一个含义即可。

AND, 在循环公布后,您有一个逗号 for, 还有什么造成问题

董宝中

赞同来自:

要检查数组是否被排序,我们可以比较数组中的相邻元素。

检查边界条件
null

&
a.length == 0



public static boolean isSorted/int[] a/{ 

if/a == null/ {
//Depends on what you have to return for null condition
return false;
}
else if/a.length == 0/ {
return true;
}
//If we find any element which is greater then its next element we return false.
for /int i = 0; i < a.length-1; i++/ {
if/a[i] > a[i+1]/ {
return false;
}
}
//If array is finished processing then return true as all elements passed the test.
return true;
}

二哥

赞同来自:

你不应该使用
a[i+1]

, 因为此值可能会或不能留下阵列。

例如:


A = {1, 2, 3}
// A.length is 3.
for/i = 0; i < a.length; i ++/ // A goes up to 3, so A[i+1] = A[4]


要修复它,请提前停止循环。


int i;
for/i = 0; i < a.length - 1; i ++/;{

if /a[i] < a[i+1]/ {

return true;
}else{
return false;

}

}

快网

赞同来自:

还对下游阵列进行了分类。 有关升序和下游阵列进行计入,我使用以下内容:


public static boolean isSorted/int[] a/{
boolean isSorted = true;
boolean isAscending = a[1] > a[0];
if/isAscending/ {
for /int i = 0; i < a.length-1; i++/ {
if/a[i] > a[i+1]/ {
isSorted = false;
break;
}
}
} else {//descending
for /int i = 0; i < a.length-1; i++/ {
if/a[i] < a[i+1]/ {
isSorted = false;
break;
}
}
}
return isSorted;
}

董宝中

赞同来自:

public static boolean isSorted/int[] a/
{
for / int i = 0; i < a.length - 1 ; i++ / {
if / a[i] > a[i+1] /
return false;
}
return true;
}


此功能检查数组是否按升序顺序。

冰洋

赞同来自:

boolean checkElements/int arr[], int first, int last/ {
while/arr.length > first/ {
if/arr[i] > arr[last-1]/ {
if/arr[i] > arr[i+1]/
return checkElements/arr, first+1, first+2/;;
return false;
}else {
if/arr[i] < arr[i+1]/
return checkElements/arr, first+1, first+2/;
return false;
}
}
return true;
}

窦买办

赞同来自:

如果要检查数组是否排序 DESC 或者 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;
}

三叔

赞同来自:

bool checkSorted/int a[], int n/ {
for /int i = 1; i < n-1; i++/ {
if /a[i] > a[i-1]/ {
return false;
}
}
return true;
}

小姐请别说爱

赞同来自:

https://developer.mozilla.org/ ... every
方法

every//

检查所提供的函数是否实现的测试数组的所有元素。


arr.every/function /a, b/ {
return a > b;
}/;

var arr = [1,2,3] // true

var arr = [3,2,1] // false

要回复问题请先登录注册