-2

I want to create a method that returns 0 if array not sorted returns 1 if it is sorted in ascending order and returns -1 if its sorted in descending order

This is what I have done so far:

public static int isSorted(int[] intArray) {

    int end = intArray.length - 1;
    int val = 0;

    for (int i = 1; i < end; i++) {

        if (intArray[0] < intArray[i]) {

            val = 1;
        }

        else if (intArray[0] > intArray[i]) {

            val = -1;
        }
    }

    return v;
}

}

This returns 1 if its ascending and -1 if its descending. But if I create a random array it does not return 0. The question is how to check if both conditions fail, i.e., if its not sorted at all.

5
  • 1
    go step by step into your code for array 1 2 1 and write down value of val variable at every loop iteration Commented Apr 17, 2017 at 19:59
  • 1
    Just a note: In your for-loop replace i < end with i <= end since end is the length but already subtracted one Commented Apr 17, 2017 at 20:01
  • If the first two elements in an array are increasing, check that all subsequent elements are increasing too; if the first two elements are decreasing, check that all subsequent elements are decreasing too. Commented Apr 17, 2017 at 20:01
  • 1
    When you say "ascending" and "descending" order, do you actually mean that, or do you mean "non-descending" and "non-ascending"? (e.g. 1,2,3 is ascending, 1,1,2 is non-descending). Commented Apr 17, 2017 at 20:37
  • it takes just as much code to sort an array as it does to check if its sorted....so why not just sort it by default...am i wrong? Commented Jul 12, 2017 at 1:23

3 Answers 3

2

You can use two additional variables to count:

public static int isSorted(int[] intArray) {

    int end = intArray.length-1;
    int counterAsc = 0;
    int counterDesc = 0;

    for (int i = 0; i < end; i++) {
        if(intArray[i] < intArray[i+1]){
            counterAsc++;
        }
        else if(intArray[i] > intArray[i+1]){
            counterDesc++;
        }
    }
    if(counterDesc == 0){
        return 1;
    }
    else if(counterAsc == 0){
        return -1;
    }
    else return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your comparison is just between first element and other elements in the array.

val = 0;
for(int i=0;i<=end;i++){
     for(int j=0;j<end;j++){
          if(intArray[j]>intArray[j+1])
               val = 1; 

     }
   }
 return val;

Comments

0

Here you go

public class stackoverflow {


    public static int isSorted(int[] intArray) {


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

    for (int i = 0; i < intArray.length-1; i++)
    {

        if (intArray[i] > intArray[i+1]) {
            sortedAsc=false;
            sameValues=false;
        }
        if (intArray[i] < intArray[i+1]) {
            sortedDesc=false;
            sameValues=false;
        }
        }

    if(sortedAsc) result= 1;
    if(sortedDesc) result =  -1;
    if(sameValues) 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;

        System.out.println(isSorted(array));

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.