dsg523
dsg523

Reputation: 21

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. It also accounts for when given integers are equal (1 1 2 3 3 4 5, for example).

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. Meaning, if the array is sorted in any direction, return 'true', if not, '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"))); 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'.

I have attempted reassigning the array reference in the Main method to a String 'true' or '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 );

    } 
}

Upvotes: 1

Views: 1571

Answers (1)

SomeDude
SomeDude

Reputation: 14228

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 (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

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:

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;
}

Upvotes: 1

Related Questions