Checking whether given array is sorted by divide-and-conquer
up vote
2
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
2
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
4 hours ago
add a comment |
up vote
2
down vote
favorite
up vote
2
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 4 hours ago
Martin R
15.3k12264
15.3k12264
asked 7 hours ago
itsnotmyrealname
1475
1475
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
4 hours ago
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
4 hours ago
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
4 hours ago
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
4 hours ago
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]
.
Holy Moley! You've thrown me a curve sir. I've edited the code according to your proposals. Would you mind glancing at again?
– itsnotmyrealname
5 hours ago
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]
.
Holy Moley! You've thrown me a curve sir. I've edited the code according to your proposals. Would you mind glancing at again?
– itsnotmyrealname
5 hours ago
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]
.
Holy Moley! You've thrown me a curve sir. I've edited the code according to your proposals. Would you mind glancing at again?
– itsnotmyrealname
5 hours ago
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 6 hours ago
AJNeufeld
3,700317
3,700317
Holy Moley! You've thrown me a curve sir. I've edited the code according to your proposals. Would you mind glancing at again?
– itsnotmyrealname
5 hours ago
add a comment |
Holy Moley! You've thrown me a curve sir. I've edited the code according to your proposals. Would you mind glancing at again?
– itsnotmyrealname
5 hours ago
Holy Moley! You've thrown me a curve sir. I've edited the code according to your proposals. Would you mind glancing at again?
– itsnotmyrealname
5 hours ago
Holy Moley! You've thrown me a curve sir. I've edited the code according to your proposals. Would you mind glancing at again?
– itsnotmyrealname
5 hours ago
add a comment |
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
4 hours ago