简体   繁体   English

排序数组(升序或降序) - true 或 false

[英]Sorted array (ascending or descending) - true or false

This post details how to determine if the given integers of an array (ascending or descending) are sorted, returning an integer representing the array.这篇文章详细介绍了如何确定数组的给定整数(升序或降序)是否已排序,返回代表该数组的 integer。 It also accounts for when given integers are equal (1 1 2 3 3 4 5, for example).它还考虑了给定整数何时相等(例如,1 1 2 3 3 4 5)。

I've been trying to figure out how to change the return values from an integer to a boolean value in the Main with no avail.我一直在试图弄清楚如何在 Main 中将返回值从 integer 更改为 boolean 值,但无济于事。 Meaning, if the array is sorted in any direction, return 'true', if not, 'false'.意思是,如果数组按任意方向排序,则返回“true”,否则返回“false”。 Been working on this problem for the better part of two days now and have learned a lot, but I'm stuck.现在已经在这两天的大部分时间里一直在研究这个问题并且学到了很多东西,但我被困住了。 Please help.请帮忙。

I have attempted System.out.println(isSorted(array.toString("true")));我尝试过 System.out.println(isSorted(array.toString("true"))); to see if that even works, which it does not.看看这是否有效,但它没有。

I have tried switching the method return value from int to boolean and then changing all the integers that would be affected to 'true' or 'false'.我尝试将方法返回值从 int 切换为 boolean,然后将所有会受到影响的整数更改为“true”或“false”。

I have attempted reassigning the array reference in the Main method to a String 'true' or 'false'.我尝试将 Main 方法中的数组引用重新分配给字符串“true”或“false”。

import java.util.Scanner;
import java.util.Arrays;

class Sorted {

public static int isSorted(int[] intArray) {

    boolean sortedAsc = true;
    boolean sortedDesc = true;
    boolean equalValues=true;
    int result = 0;

    for (int i = 0; i < intArray.length-1; i++)
   {
       equalValues = equalValues && (intArray[i] == intArray[i+1]);
       sortedAscending = sortedAscending && ((intArray[i] <= intArray[i+1]));
       sortedDescending = sortedDescending && ((intArray[i] >= intArray[i+1]));


    if(sortedAsc) result= 1;
    if(sortedDesc) result =  -1;
    if(equalValues) result = 2;

    return result;
}


    public static void main(String[] args) {
        // TODO code application logic here
        int array[] = new int[4];
        array[0]=1;
        array[1]=2;
        array[2]=3;
        array[3]=4;


        int sortedResult = isSorted(array);
        System.out.println( sortedResult == 1 || sortedResult == -1 || sortedResult == 2 );

    } 
}

If I understand correctly, what you are trying to do is to see if an array is sorted in ascending or descending way or if all the elements are equal in the array ( which is also sorted by the way ).如果我理解正确,您要做的是查看数组是否以升序或降序方式排序,或者数组中的所有元素是否相等(也按方式排序)。 If that is the case I would modify your for loop as:如果是这种情况,我会将您的for循环修改为:

   for (int i = 0; i < intArray.length-1; i++)
   {
       equalValues = equalValues && (intArray[i] == intArray[i+1]);
       sortedAscending = sortedAscending && ((intArray[i] <= intArray[i+1]));
       sortedDescending = sortedDescending && ((intArray[i] >= intArray[i+1]));
   }

Basically you need to keep track of your previous values of the booleans to check the invariant of the sorted-ness.基本上,您需要跟踪布尔值的先前值以检查排序的不变量。

If you need strictly ascending or strictly descending, you just need to change <= to < and >= to >如果您需要严格升序或严格降序,您只需将<=更改为<>=更改为>

Now in order to print your result as true based on the result现在为了根据result打印你的结果为true

You just need to do:你只需要这样做:

int sortedResult = isSorted(array)
System.out.println( sortedResult == 1 || sortedResult == -1 || sortedResult == 2 )

Based on your update to the code, you have a few errors, just replace the method isSorted method with the below and try:根据您对代码的更新,您有一些错误,只需将方法isSorted方法替换为以下方法并尝试:

public static int isSorted(int[] intArray) {

    boolean sortedAsc = true;
    boolean sortedDesc = true;
    boolean equalValues=true;
    int result = 0;

    for (int i = 0; i < intArray.length-1; i++)
    {
       equalValues = equalValues && (intArray[i] == intArray[i+1]);
       sortedAsc = sortedAsc && ((intArray[i] <= intArray[i+1]));
       sortedDesc = sortedDesc && ((intArray[i] >= intArray[i+1]));
    }

    if(sortedAsc) result= 1;
    if(sortedDesc) result =  -1;
    if(equalValues) result = 2;

    return result;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定一个整数数组作为输入,如果该数组已排序,则返回true。 请注意,数组可以按升序或降序排序 - Given an array of integers as input, return true if the array is sorted. Note that the array can be sorted in either ascending or descending order 升序排序数组是按降序打印的吗? - Ascending order Sorted array is getting printed in descending order? 已排序? 检查数组是否在 Java 中按升序或降序排序 - isSorted? Check array if is sorted in ascending or descending order in Java 检查一个数组,它是否按升/降排序,并且以固定值递增/递减 - check an array whether this is sorted ascending/descending and they are increasing/decreasing with a fixed value 如果数组不是降序还是升序,为什么我的代码不会返回false? - why will my code not return false if the array is not descending or ascending order? 检查数组是否已排序,返回 true 或 false - Check if an array is sorted, return true or false 运行代码时,当第二个数组应按升序或降序排序时,排序方法会打印相同的确切数组编号 - When running code, sorting method prints same exact array numbers when the second array should be sorted in ascending or descending order 按升序和降序对数组的 LinkedHashmap 进行排序 - Java - Sort LinkedHashmap of Array in ascending and descending order - Java 在循环中检查数组是升序还是降序 - Checking if an Array is in Ascending or Descending order in a loop Java:未排序的数组到降序的数组 - Java: Unsorted Array to Descending Sorted Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM