检查数组是否在java中以递归方式排序
我想写一个方法,返回一个名为Itissured的布尔值,该值包含两个参数;数据:整数arrayn:数组中的元素数,递归检查数组是否已排序。也就是说,当且仅当数据数组已排序时返回true检查数组是否在java中以递归方式排序,java,recursion,Java,Recursion,我想写一个方法,返回一个名为Itissured的布尔值,该值包含两个参数;数据:整数arrayn:数组中的元素数,递归检查数组是否已排序。也就是说,当且仅当数据数组已排序时返回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 t
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
}
这就是你想要的答案吗
这就是你想要的答案吗?我想你想要这样的答案
public static boolean itisSorted(int[] data, int n) {
// Null or less then 2 elements is sorted.
if (data == null || n < 2) {
return true;
} else if (data[n - 2] > data[n - 1]) {
// If the element before (n-2) this one (n-1) is greater,
return false;
}
// recurse.
return itisSorted(data, n - 1);
}
public static void main(String[] args) {
int [] data = {1,2,3};
System.out.println(Arrays.toString(data) //
+ (itisSorted(data, data.length) ? " Sorted" : " Unsorted"));
data = new int[] {3,2,1};
System.out.println(Arrays.toString(data) //
+ (itisSorted(data, data.length) ? " Sorted" : " Unsorted"));
}
我想你想要这样的东西
public static boolean itisSorted(int[] data, int n) {
// Null or less then 2 elements is sorted.
if (data == null || n < 2) {
return true;
} else if (data[n - 2] > data[n - 1]) {
// If the element before (n-2) this one (n-1) is greater,
return false;
}
// recurse.
return itisSorted(data, n - 1);
}
public static void main(String[] args) {
int [] data = {1,2,3};
System.out.println(Arrays.toString(data) //
+ (itisSorted(data, data.length) ? " Sorted" : " Unsorted"));
data = new int[] {3,2,1};
System.out.println(Arrays.toString(data) //
+ (itisSorted(data, data.length) ? " Sorted" : " Unsorted"));
}
//使用此方法,不需要传递数组的长度
public static boolean isOrdered(int nums[])
{
// Base Cases
if (nums.length == 0)
return true;
if (nums.length == 1)
return true;
int[] temp = new int[nums.length - 1];
if (nums[0] <= nums[1]) {
for (int i = 1; i < nums.length; i++) {
temp[i-1] = nums[i];
}
return isSorted(temp);
} else
return false;
}
//使用此方法,不需要传递数组的长度
public static boolean isOrdered(int nums[])
{
// Base Cases
if (nums.length == 0)
return true;
if (nums.length == 1)
return true;
int[] temp = new int[nums.length - 1];
if (nums[0] <= nums[1]) {
for (int i = 1; i < nums.length; i++) {
temp[i-1] = nums[i];
}
return isSorted(temp);
} else
return false;
}
递归求解
int isArraySorted(int []a, int index)
{
if(index == 1 || a.length == 1)
return 1;
return (a[index -1] <= a[index-2]) ? 0 : isArraySorted(a, index-1) ;
}
相应地更改降序的条件。使用递归解决方案
int isArraySorted(int []a, int index)
{
if(index == 1 || a.length == 1)
return 1;
return (a[index -1] <= a[index-2]) ? 0 : isArraySorted(a, index-1) ;
}
相应地更改递减的条件。如果n是元素数,我认为他是在向后比较它们。这很好,但是递归调用必须使用n-1。同样在这种情况下,在数据[n]处的第一次检查将超出范围。@弗兰克,你说得对。原始代码引用data[n]和data[n-1]作为前两个元素,因此我假设您将其称为ssorteddata,1作为递归的开始。我将很快相应地更新我的答案。@Henry此函数将检查数组是否按降序排序,对吗?如果n是元素数,我想他是在向后比较它们。这很好,但是递归调用必须使用n-1。同样在这种情况下,在数据[n]处的第一次检查将超出范围。@弗兰克,你说得对。原始代码引用data[n]和data[n-1]作为前两个元素,因此我假设您将其称为ssorteddata,1作为递归的开始。我将很快相应地更新我的答案。@Henry此函数将检查数组是否按降序排序,对吗?如果n是元素数,则数据[n]将超出范围,因为数组中的第一个元素将是数据[0],而不是数据[1]。如果n是元素数,则数据[n]将超出范围,因为数组中的第一个元素将是数据[0],而不是数据[1]。谢谢,我知道哪里出错了。我现在可以运行代码以完全理解问题。再次谢谢。谢谢,我知道我错在哪里了。我现在可以运行代码以完全理解问题。再次感谢你。