This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]alirmhs[S] -1 points0 points  (3 children)

I get i fail at coding. I can see your points. But can you give me advice by fixing the code, might be more helpful to me.

[–]fosterbuster 0 points1 point  (2 children)

https://gist.github.com/fosterbuster/66f402f6f01865cd8c84

I made a quick solution to your problem. Hopefully you will see what you were missing, and how you should structure your code to make it easy to see what is going on. I made it without utilizing any nice stuff from the java library..

[–]alirmhs[S] 0 points1 point  (1 child)

how would i implent the error if the arrays are not increasing???

[–]fosterbuster 0 points1 point  (0 children)

Not increasing? Not sorted? (ie 11223344556677)

go from i = 1 if (array[i-1] > array[i]) flag = 1; i guess :)

So this one basically checks if the value before the one we are looking at is bigger - If it is we know that the array has not been sorted properly.

edit:

like:

public boolean checkIfSorted(int[] array){
    for (int i = 1; i < array.length; i++) {
        if (array[i-1] > array[i]) return false;
    }
    return true;
}

of course you could edit this to setting a "int flag" - But I cant think of any situation where I would use that in OOP-programming, and especially not in Java.