我想编写一个名为 itisSorted
的方法(返回一个布尔值),它接受两个参数;
-
data
:整数数组 -
n
:数组中的元素数
并递归检查数组是否已排序 . 也就是说,如果(且仅当) data
数组被排序,则返回 true
.
public boolean itisSorted(int [] data, int n)
{
if(data.length ==0 || data.length==1)
return true;
else if (data[n] > data[n-1]) //here i compare the first two elements
return false;
else //here is where i put the recursive call to check if
// the rest of the array is sorted, but I am having difficulties with the
// part of the code
}
4 回答
我想你想要这样的东西
使用递归的解决方案
相应地改变下降的条件 .
//使用此方法无需传递数组的长度
这是你想要的答案吗?