Checking whether given array is sorted by divide-and-conquer
up vote
3
down vote
favorite
I've written a code that I try to use divide and conquer approach to determine if the given array is sorted. I wonder whether I apply the approach accurately.
public static boolean isSorted(List<Integer> arr, int start, int end) {
if (end - start == 1) // base case to compare two elements
return arr.get(end) > arr.get(start);
int middle = (end + start) >>> 1; // division by two
boolean leftPart = isSorted(arr, start, middle);
boolean rightPart = isSorted(arr, middle, end);
return leftPart && rightPart;
}
To use call,
isSorted(list, 0, list.size() - 1)
java algorithm array recursion divide-and-conquer
add a comment |
up vote
3
down vote
favorite
I've written a code that I try to use divide and conquer approach to determine if the given array is sorted. I wonder whether I apply the approach accurately.
public static boolean isSorted(List<Integer> arr, int start, int end) {
if (end - start == 1) // base case to compare two elements
return arr.get(end) > arr.get(start);
int middle = (end + start) >>> 1; // division by two
boolean leftPart = isSorted(arr, start, middle);
boolean rightPart = isSorted(arr, middle, end);
return leftPart && rightPart;
}
To use call,
isSorted(list, 0, list.size() - 1)
java algorithm array recursion divide-and-conquer
3
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
– Martin R
Nov 24 at 22:24
Test your code on3, 4, 1, 2
.
– vnp
Nov 25 at 6:43
@vnpisSorted(list, 0, 3)
will callisSorted(arr, 1, 3)
which will callisSorted(arr, 1, 2)
which will returnfalse
. That test case is fine.
– AJNeufeld
Nov 25 at 18:02
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I've written a code that I try to use divide and conquer approach to determine if the given array is sorted. I wonder whether I apply the approach accurately.
public static boolean isSorted(List<Integer> arr, int start, int end) {
if (end - start == 1) // base case to compare two elements
return arr.get(end) > arr.get(start);
int middle = (end + start) >>> 1; // division by two
boolean leftPart = isSorted(arr, start, middle);
boolean rightPart = isSorted(arr, middle, end);
return leftPart && rightPart;
}
To use call,
isSorted(list, 0, list.size() - 1)
java algorithm array recursion divide-and-conquer
I've written a code that I try to use divide and conquer approach to determine if the given array is sorted. I wonder whether I apply the approach accurately.
public static boolean isSorted(List<Integer> arr, int start, int end) {
if (end - start == 1) // base case to compare two elements
return arr.get(end) > arr.get(start);
int middle = (end + start) >>> 1; // division by two
boolean leftPart = isSorted(arr, start, middle);
boolean rightPart = isSorted(arr, middle, end);
return leftPart && rightPart;
}
To use call,
isSorted(list, 0, list.size() - 1)
java algorithm array recursion divide-and-conquer
java algorithm array recursion divide-and-conquer
edited Nov 24 at 22:24
Martin R
15.4k12264
15.4k12264
asked Nov 24 at 19:24
itsnotmyrealname
1525
1525
3
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
– Martin R
Nov 24 at 22:24
Test your code on3, 4, 1, 2
.
– vnp
Nov 25 at 6:43
@vnpisSorted(list, 0, 3)
will callisSorted(arr, 1, 3)
which will callisSorted(arr, 1, 2)
which will returnfalse
. That test case is fine.
– AJNeufeld
Nov 25 at 18:02
add a comment |
3
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
– Martin R
Nov 24 at 22:24
Test your code on3, 4, 1, 2
.
– vnp
Nov 25 at 6:43
@vnpisSorted(list, 0, 3)
will callisSorted(arr, 1, 3)
which will callisSorted(arr, 1, 2)
which will returnfalse
. That test case is fine.
– AJNeufeld
Nov 25 at 18:02
3
3
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
– Martin R
Nov 24 at 22:24
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
– Martin R
Nov 24 at 22:24
Test your code on
3, 4, 1, 2
.– vnp
Nov 25 at 6:43
Test your code on
3, 4, 1, 2
.– vnp
Nov 25 at 6:43
@vnp
isSorted(list, 0, 3)
will call isSorted(arr, 1, 3)
which will call isSorted(arr, 1, 2)
which will return false
. That test case is fine.– AJNeufeld
Nov 25 at 18:02
@vnp
isSorted(list, 0, 3)
will call isSorted(arr, 1, 3)
which will call isSorted(arr, 1, 2)
which will return false
. That test case is fine.– AJNeufeld
Nov 25 at 18:02
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
It looks like your are doing it mostly right. You have problems with length zero and length 1 arrays, but you should be able to fix those pretty quick.
You may be doing more work than necessary. If an array is not sorted, you might find leftPart
is false
, but you unconditionally go on to determine the value of rightPart
anyway, despite it not mattering. The simplest way to avoid that is to combine that recursive calls and the &&
operation. Ie:
return isSorted(arr, start, middle) && isSorted(arr, middle, end);
Lastly, if the array contains duplicates, can it still be considered sorted? You return false
for [1, 2, 2, 3]
.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
It looks like your are doing it mostly right. You have problems with length zero and length 1 arrays, but you should be able to fix those pretty quick.
You may be doing more work than necessary. If an array is not sorted, you might find leftPart
is false
, but you unconditionally go on to determine the value of rightPart
anyway, despite it not mattering. The simplest way to avoid that is to combine that recursive calls and the &&
operation. Ie:
return isSorted(arr, start, middle) && isSorted(arr, middle, end);
Lastly, if the array contains duplicates, can it still be considered sorted? You return false
for [1, 2, 2, 3]
.
add a comment |
up vote
2
down vote
It looks like your are doing it mostly right. You have problems with length zero and length 1 arrays, but you should be able to fix those pretty quick.
You may be doing more work than necessary. If an array is not sorted, you might find leftPart
is false
, but you unconditionally go on to determine the value of rightPart
anyway, despite it not mattering. The simplest way to avoid that is to combine that recursive calls and the &&
operation. Ie:
return isSorted(arr, start, middle) && isSorted(arr, middle, end);
Lastly, if the array contains duplicates, can it still be considered sorted? You return false
for [1, 2, 2, 3]
.
add a comment |
up vote
2
down vote
up vote
2
down vote
It looks like your are doing it mostly right. You have problems with length zero and length 1 arrays, but you should be able to fix those pretty quick.
You may be doing more work than necessary. If an array is not sorted, you might find leftPart
is false
, but you unconditionally go on to determine the value of rightPart
anyway, despite it not mattering. The simplest way to avoid that is to combine that recursive calls and the &&
operation. Ie:
return isSorted(arr, start, middle) && isSorted(arr, middle, end);
Lastly, if the array contains duplicates, can it still be considered sorted? You return false
for [1, 2, 2, 3]
.
It looks like your are doing it mostly right. You have problems with length zero and length 1 arrays, but you should be able to fix those pretty quick.
You may be doing more work than necessary. If an array is not sorted, you might find leftPart
is false
, but you unconditionally go on to determine the value of rightPart
anyway, despite it not mattering. The simplest way to avoid that is to combine that recursive calls and the &&
operation. Ie:
return isSorted(arr, start, middle) && isSorted(arr, middle, end);
Lastly, if the array contains duplicates, can it still be considered sorted? You return false
for [1, 2, 2, 3]
.
answered Nov 24 at 20:09
AJNeufeld
3,784317
3,784317
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208343%2fchecking-whether-given-array-is-sorted-by-divide-and-conquer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.
– Martin R
Nov 24 at 22:24
Test your code on
3, 4, 1, 2
.– vnp
Nov 25 at 6:43
@vnp
isSorted(list, 0, 3)
will callisSorted(arr, 1, 3)
which will callisSorted(arr, 1, 2)
which will returnfalse
. That test case is fine.– AJNeufeld
Nov 25 at 18:02