Home > Mobile >  Array Sorting, At Least One Element Is Great Than Zero, No Same Elements
Array Sorting, At Least One Element Is Great Than Zero, No Same Elements

Time:02-04

I am trying to check an array if it's sorted with specific introductions. There must be at least one element in array that is greater than 0. If the array is sorted as big numbers to small it must return true and all the elements must be different in that array. So here is my code;

public class BubbleSort{

public static boolean isSorted(int[] array) {

    for (int i = 0; i < array.length - 1; i  ){
        if(array[i] > 0 && array[i] > array[i   1]){
            return true;
        }
    }
    return false;
    }

    public static void main(String[] args) {
        int[] array = {1,-3,-3};
        System.out.println(isSorted(array));

    }

}

In my opinion this should return false and I don't know where it gets wrong. When it's the same element it should return false but this code returns true.

CodePudding user response:

So when using "Return" it, well, returns a value and stops the function.

In your case it will start at 1, looks if it is greater than 0 (Which it is) and if its greater then the next value in the array (Which is -3, so true). Both true so it will return true and stop the function.

If you want to check if it is greater then 0 and if the array goes from big to small try something like this:

public static boolean isSorted(int[] array) {
    boolean foundPositive = false;

    for (int i = 0; i < array.length - 1; i  ) {
        if (array[i] > 0) foundPositive = true;

        if (array[i] <= array[i   1]) {
            return false;
        }
    }
    return foundPositive;
}

It check if its greater then 0. Then if the number is smaller or equal to the next value, if so, return false. And when it passed that check, it will return if a number is greater then 0.

CodePudding user response:

Since the array must be big to small -> descending order and at least one element must be greater than 0, it can be done by simply checking if (array[0]>0) as array[0] should be the largest value.

if(array[0]>0){
   //your code
}

As for the order, check with `<=.

for (int i = 0; i < array.length - 1; i  ){
    if(array[i] <= array[i   1]){
        return false;
    }
}

Combining both

public static boolean isSorted(int[] array) {
    if(array[0]>0){ //largest value in array is greater than 0
        for (int i = 0; i < array.length - 1; i  ){
            if(array[i] <= array[i   1]){
                return false;
            }
        }
        return true;
    }
    return false;
}

CodePudding user response:

If you're just checking if the array is sorted descending:

public static boolean isSorted(int [] data) {
    for (int i = 1; i< data.length; i  ) {
        if (data[i-1] < data[i]) {
            return false;
        }
    }
    
    return true;
}

public static boolean hasPositive(int [] data) {
    return Arrays.stream(data).filter(a -> a > 0).findFirst().isPresent();
}

public static boolean hasDuplicates(int [] data) {
    long cnt = Arrays.stream(data).distinct().count();
    return cnt != data.length;
}

public static boolean meetsCriteria(int []data) {
    if (!hasPositive(data)) {
        return false;
    }
    
    if (hasDuplicates(data)) {
        return false;
    }
    
    return isSorted(data);
}

CodePudding user response:

Consider the following "sorted" array: {1,2,3,6,7,4,5}

Now consider your loop:

for (int i = 0; i < array.length - 1; i  ){
    if(array[i] > 0 && array[i] > array[i   1]){
        return true;
    }
}

When i is zero, it will pass the array[0] > 0 test but will fail the array[0] > array[1] test. This last test will continue to fail until i is 4. At this point, array[4] is 7 and array[5] is 4. Since array[4] > 0 AND array[4] > array[5], it will return true. This is clearly a mistake because you can see the array I created is not sorted.

What you need to do is to continue checking the array for array[i] > array[i 1] until you reach the end of the array. At that point, you will return true. If on the other hand, the array[i] > array[i 1] fails at any point, you need to return false.

Based on this, you need to rewrite you isSorted function as follows:

public static boolean isSorted(int[] array) {
    if(array[0] <= 0) return false;

    for (int i = 0; i < array.length - 1; i  ){
        if(array[i] < array[i   1]){
            return false;
        }
    }
    return true;
}

You may be asking why I extracted this test from the for loop: if(array[0] <= 0) return false;?

The reason is simple. If the first element is greater than zero, for the array to be properly sorted, all other elements must also be greater than zero. If one happens to be zero, it will automatically fail the evaluation inside the for loop.

  •  Tags:  
  • Related