+ 2
Can anyone tell me why it is not working when I turn sorted into true ,but it's working when I turn it into false?
17 Respostas
+ 1
Atul
The trick is that both booleans should be true at the beginning.
And only if the conditions are not fulfilled they should get false.
For example:
4
1 3 2 5
In your case, the first boolean gets true but not false again. So it prints sorted. You would need an extra condition.
But if you do it the other way around:
1 < 3 sorted1 keeps true
3 < 2 sorted1 gets false
Same for sorted2:
1 > 3 sorted2 gets false
And this solves also your problem with one value arrays.
Btw: If you want you can add a break statement: if(sorted1 == false && sorted2 == false) break;
If both are false you don't need to check the other values.
+ 1
Atul
Maybe you confused it or I explained it not good enough.
boolean sorted = false; makes your code complicated.
if you find a sorted element it need to get true
if it is true and you find unsorted elements it need to get false
if it is false you need to prevent that it gets true again
But you also need to provide that sorted can get true at the first time.
boolean sorted = true;
Here you only need to check if you find unsorted elements, sorted gets false and you are done. The other elements are not relevant.
+ 1
In general if you are not sure if you should initialize a boolean with true or false:
What do you want to check?
What is easier to check?
Just two examples:
You want to check if an array contains a number.
Searching for the number is easy, you just need one condition. So you can initialize your boolean with false.
boolean containsNum = false;
for(int i = 0; i < arr.length; i++){
if(yourNumber == arr[i]) containsNum = true;
}
if you find the number -> containsNum gets true -> number found
else containsNum stays false -> number not found
You want to check if a number is prime. In this case it is much easier to check if it is has divisors, instead to proof that this number has no divisors.
You initialize your boolean with true and if you find divisors your boolean gets false.
boolean isPrime = true;
for(int i = 2; i < yourNumber; i++){
if(yourNumber % i == 0)
isPrime = false;
}
If you find a divisor -> isPrime gets false -> not a prime
else isPrime stays true -> prime number
0
EverVev What can be done for it? Any hint?
0
Denise Roßberg Can I make a function for both ascending and descending sorts and compare them with the input value?
0
Atul
You mean all in one?
Then I would just copy your loop into the method.
public static boolean isSorted(int[] arr]{
if(arr.length <= 1) return true;
boolean sorted1 = true;
boolean sorted2 = true;
//your loop goes here
return sorted1 || sorted2;
}
0
Denise Roßberg I mean that I will make 2 separate functions for both ascending order and descending order. And compare it to the input values in the main method
0
Btw: I tested a bit around and I would say it is just too complicated
if you start with sorted = false.
1) sorted need to get true if it is sorted
2) it need to get false if it is not sorted
3) you need to prevent getting true again
Starting with false keeps your code easier. (Just for the clarification of your first question: It would work but it is laborious)
About your two methods:
public static boolean isDescending(int[] arr){
if(arr.length <= 1) return true;
put your loop here, just change the condition:
if(arr[i] < arr[i+1]) return false;
//end loop
return true;
}
Same for ascending.
0
Denise Roßberg Can you please tell me the benefits of initialising Boolean sorted=false?
0
For example theese two cases:
a) 1 2 3 4 5
b) 1 2 4 3 5
Look at first to case b)
sorted gets true, but need to get false after 4 < 3.
3 < 5 would make it true but sorted is false and should not get true again.
Now look at the sorted case a)
sorted is false but need to get true.
Rember for cases like b) you need to prevent a switch from false to true.
I guess you would need a another boolean:
boolean firstTime = false;
boolean sorted = false;
if(firstTime == false && arr[i] < arr[i+1]){
sorted = true;
firstTime = true;
}
if(arr[i] > arr[i+1]) sorted = false;
You are producing more lines of code than you would need to solve your problem.
0
Oh means first initialise it with true. And after that prove that it is sorted or not? Yes or no
0
Don't know why the answers and the question is getting downvotes?
0
Hm I am not sure if I understand your question.
About the downvotings, I have no idea who did it.
0
Denise Roßberg No you understood the question and thanks for detailed explanation.
0
Denise Roßberg Do initialising Boolean with false saves time complexity sometimes?
0
Atul
I am not sure but I guess you would need a really large array to see a difference.
- 1
Don't know why are you doing all this nonsense, you just want to check if the array is in increasing or decreasing order, right? You can just loop through the array and check just the last element if it was big or small (depending on your requirement), if the check returns true, return "not sorted" from the function, if all checks have returned false, then just return "sorted" from the function.