Quicksort implementation with pivotal calculated as middle element
up vote
0
down vote
favorite
I read quick sort algorhitm and implemented in like this:
public List<Integer> sort(List<Integer> list) {
if (list.size() <= 1) {
return list;
}
int pivotalValue = list.get(list.size() / 2);
List<Integer> left = new ArrayList<>();
List<Integer> pivotalValues = new ArrayList<>();
List<Integer> right = new ArrayList<>();
for (Integer element : list) {
if (element < pivotalValue) {
left.add(element);
} else if (element > pivotalValue) {
right.add(element);
} else {
pivotalValues.add(element);
}
}
List<Integer> sortedLeft = sort(left);
List<Integer> sortedRight = sort(right);
sortedLeft.addAll(pivotalValues);
sortedLeft.addAll(sortedRight);
return sortedLeft;
}
What do you think about my implementation?
java algorithm sorting quick-sort
bumped to the homepage by Community♦ 2 days ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
0
down vote
favorite
I read quick sort algorhitm and implemented in like this:
public List<Integer> sort(List<Integer> list) {
if (list.size() <= 1) {
return list;
}
int pivotalValue = list.get(list.size() / 2);
List<Integer> left = new ArrayList<>();
List<Integer> pivotalValues = new ArrayList<>();
List<Integer> right = new ArrayList<>();
for (Integer element : list) {
if (element < pivotalValue) {
left.add(element);
} else if (element > pivotalValue) {
right.add(element);
} else {
pivotalValues.add(element);
}
}
List<Integer> sortedLeft = sort(left);
List<Integer> sortedRight = sort(right);
sortedLeft.addAll(pivotalValues);
sortedLeft.addAll(sortedRight);
return sortedLeft;
}
What do you think about my implementation?
java algorithm sorting quick-sort
bumped to the homepage by Community♦ 2 days ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
I think the title fails to name the distinguishing feature of this implementation: 3-way distributing values toList
s. You present uncommented/undocumented code. You needlessly forsake genericity. (I was tempted to down-vote the question out of considering the code presented abysmal - the voting hints stress usefulness of the question.)
– greybeard
2 days ago
@greybeard I think code is self explained
– gstackoverflow
2 days ago
Try using your method in a separate source file. See what tool support you get from your IDE of choice with or without doc comments. Revisit your code a couple of years later.
– greybeard
2 days ago
@greybeard, could you provide concrete points you don't like? It is really unclear what you to say
– gstackoverflow
2 days ago
could you provide concrete points you [have an opinion on]?
I try to refrain from getting into details of uncommented code.
– greybeard
2 days ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I read quick sort algorhitm and implemented in like this:
public List<Integer> sort(List<Integer> list) {
if (list.size() <= 1) {
return list;
}
int pivotalValue = list.get(list.size() / 2);
List<Integer> left = new ArrayList<>();
List<Integer> pivotalValues = new ArrayList<>();
List<Integer> right = new ArrayList<>();
for (Integer element : list) {
if (element < pivotalValue) {
left.add(element);
} else if (element > pivotalValue) {
right.add(element);
} else {
pivotalValues.add(element);
}
}
List<Integer> sortedLeft = sort(left);
List<Integer> sortedRight = sort(right);
sortedLeft.addAll(pivotalValues);
sortedLeft.addAll(sortedRight);
return sortedLeft;
}
What do you think about my implementation?
java algorithm sorting quick-sort
I read quick sort algorhitm and implemented in like this:
public List<Integer> sort(List<Integer> list) {
if (list.size() <= 1) {
return list;
}
int pivotalValue = list.get(list.size() / 2);
List<Integer> left = new ArrayList<>();
List<Integer> pivotalValues = new ArrayList<>();
List<Integer> right = new ArrayList<>();
for (Integer element : list) {
if (element < pivotalValue) {
left.add(element);
} else if (element > pivotalValue) {
right.add(element);
} else {
pivotalValues.add(element);
}
}
List<Integer> sortedLeft = sort(left);
List<Integer> sortedRight = sort(right);
sortedLeft.addAll(pivotalValues);
sortedLeft.addAll(sortedRight);
return sortedLeft;
}
What do you think about my implementation?
java algorithm sorting quick-sort
java algorithm sorting quick-sort
edited Oct 25 at 14:46
asked Oct 25 at 14:39
gstackoverflow
301214
301214
bumped to the homepage by Community♦ 2 days ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 2 days ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
I think the title fails to name the distinguishing feature of this implementation: 3-way distributing values toList
s. You present uncommented/undocumented code. You needlessly forsake genericity. (I was tempted to down-vote the question out of considering the code presented abysmal - the voting hints stress usefulness of the question.)
– greybeard
2 days ago
@greybeard I think code is self explained
– gstackoverflow
2 days ago
Try using your method in a separate source file. See what tool support you get from your IDE of choice with or without doc comments. Revisit your code a couple of years later.
– greybeard
2 days ago
@greybeard, could you provide concrete points you don't like? It is really unclear what you to say
– gstackoverflow
2 days ago
could you provide concrete points you [have an opinion on]?
I try to refrain from getting into details of uncommented code.
– greybeard
2 days ago
add a comment |
I think the title fails to name the distinguishing feature of this implementation: 3-way distributing values toList
s. You present uncommented/undocumented code. You needlessly forsake genericity. (I was tempted to down-vote the question out of considering the code presented abysmal - the voting hints stress usefulness of the question.)
– greybeard
2 days ago
@greybeard I think code is self explained
– gstackoverflow
2 days ago
Try using your method in a separate source file. See what tool support you get from your IDE of choice with or without doc comments. Revisit your code a couple of years later.
– greybeard
2 days ago
@greybeard, could you provide concrete points you don't like? It is really unclear what you to say
– gstackoverflow
2 days ago
could you provide concrete points you [have an opinion on]?
I try to refrain from getting into details of uncommented code.
– greybeard
2 days ago
I think the title fails to name the distinguishing feature of this implementation: 3-way distributing values to
List
s. You present uncommented/undocumented code. You needlessly forsake genericity. (I was tempted to down-vote the question out of considering the code presented abysmal - the voting hints stress usefulness of the question.)– greybeard
2 days ago
I think the title fails to name the distinguishing feature of this implementation: 3-way distributing values to
List
s. You present uncommented/undocumented code. You needlessly forsake genericity. (I was tempted to down-vote the question out of considering the code presented abysmal - the voting hints stress usefulness of the question.)– greybeard
2 days ago
@greybeard I think code is self explained
– gstackoverflow
2 days ago
@greybeard I think code is self explained
– gstackoverflow
2 days ago
Try using your method in a separate source file. See what tool support you get from your IDE of choice with or without doc comments. Revisit your code a couple of years later.
– greybeard
2 days ago
Try using your method in a separate source file. See what tool support you get from your IDE of choice with or without doc comments. Revisit your code a couple of years later.
– greybeard
2 days ago
@greybeard, could you provide concrete points you don't like? It is really unclear what you to say
– gstackoverflow
2 days ago
@greybeard, could you provide concrete points you don't like? It is really unclear what you to say
– gstackoverflow
2 days ago
could you provide concrete points you [have an opinion on]?
I try to refrain from getting into details of uncommented code.– greybeard
2 days ago
could you provide concrete points you [have an opinion on]?
I try to refrain from getting into details of uncommented code.– greybeard
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
What do you think about my implementation?
To tell you the truth, I don't think much of your implementation. Instead of in-place swaps you're creating a bunch of temporary lists and combining them after. It probably more closely resembles a merge sort than a quick sort.
Creating all those lists and merging them together requires many more iterations than the usual quick sort.
Altogether, it seems to me, that both the time and space complexity is worse than the usual quick sort.
I am agree about memory complexity, but I don't understand why time complexity worse than usual quick sort
– gstackoverflow
Oct 25 at 17:22
@gstackoverflow - merging the lists together requires extra iterations over the data.
– tinstaafl
Oct 25 at 17:25
Are you sure? lists are not arrays
– gstackoverflow
Oct 25 at 17:33
@gstackoverflow - a quote from theaddAll
doc pageThe new elements will appear in this list in the order that they are returned by the specified collection's iterator.
– tinstaafl
Oct 25 at 17:41
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
What do you think about my implementation?
To tell you the truth, I don't think much of your implementation. Instead of in-place swaps you're creating a bunch of temporary lists and combining them after. It probably more closely resembles a merge sort than a quick sort.
Creating all those lists and merging them together requires many more iterations than the usual quick sort.
Altogether, it seems to me, that both the time and space complexity is worse than the usual quick sort.
I am agree about memory complexity, but I don't understand why time complexity worse than usual quick sort
– gstackoverflow
Oct 25 at 17:22
@gstackoverflow - merging the lists together requires extra iterations over the data.
– tinstaafl
Oct 25 at 17:25
Are you sure? lists are not arrays
– gstackoverflow
Oct 25 at 17:33
@gstackoverflow - a quote from theaddAll
doc pageThe new elements will appear in this list in the order that they are returned by the specified collection's iterator.
– tinstaafl
Oct 25 at 17:41
add a comment |
up vote
0
down vote
What do you think about my implementation?
To tell you the truth, I don't think much of your implementation. Instead of in-place swaps you're creating a bunch of temporary lists and combining them after. It probably more closely resembles a merge sort than a quick sort.
Creating all those lists and merging them together requires many more iterations than the usual quick sort.
Altogether, it seems to me, that both the time and space complexity is worse than the usual quick sort.
I am agree about memory complexity, but I don't understand why time complexity worse than usual quick sort
– gstackoverflow
Oct 25 at 17:22
@gstackoverflow - merging the lists together requires extra iterations over the data.
– tinstaafl
Oct 25 at 17:25
Are you sure? lists are not arrays
– gstackoverflow
Oct 25 at 17:33
@gstackoverflow - a quote from theaddAll
doc pageThe new elements will appear in this list in the order that they are returned by the specified collection's iterator.
– tinstaafl
Oct 25 at 17:41
add a comment |
up vote
0
down vote
up vote
0
down vote
What do you think about my implementation?
To tell you the truth, I don't think much of your implementation. Instead of in-place swaps you're creating a bunch of temporary lists and combining them after. It probably more closely resembles a merge sort than a quick sort.
Creating all those lists and merging them together requires many more iterations than the usual quick sort.
Altogether, it seems to me, that both the time and space complexity is worse than the usual quick sort.
What do you think about my implementation?
To tell you the truth, I don't think much of your implementation. Instead of in-place swaps you're creating a bunch of temporary lists and combining them after. It probably more closely resembles a merge sort than a quick sort.
Creating all those lists and merging them together requires many more iterations than the usual quick sort.
Altogether, it seems to me, that both the time and space complexity is worse than the usual quick sort.
answered Oct 25 at 17:05
tinstaafl
6,330727
6,330727
I am agree about memory complexity, but I don't understand why time complexity worse than usual quick sort
– gstackoverflow
Oct 25 at 17:22
@gstackoverflow - merging the lists together requires extra iterations over the data.
– tinstaafl
Oct 25 at 17:25
Are you sure? lists are not arrays
– gstackoverflow
Oct 25 at 17:33
@gstackoverflow - a quote from theaddAll
doc pageThe new elements will appear in this list in the order that they are returned by the specified collection's iterator.
– tinstaafl
Oct 25 at 17:41
add a comment |
I am agree about memory complexity, but I don't understand why time complexity worse than usual quick sort
– gstackoverflow
Oct 25 at 17:22
@gstackoverflow - merging the lists together requires extra iterations over the data.
– tinstaafl
Oct 25 at 17:25
Are you sure? lists are not arrays
– gstackoverflow
Oct 25 at 17:33
@gstackoverflow - a quote from theaddAll
doc pageThe new elements will appear in this list in the order that they are returned by the specified collection's iterator.
– tinstaafl
Oct 25 at 17:41
I am agree about memory complexity, but I don't understand why time complexity worse than usual quick sort
– gstackoverflow
Oct 25 at 17:22
I am agree about memory complexity, but I don't understand why time complexity worse than usual quick sort
– gstackoverflow
Oct 25 at 17:22
@gstackoverflow - merging the lists together requires extra iterations over the data.
– tinstaafl
Oct 25 at 17:25
@gstackoverflow - merging the lists together requires extra iterations over the data.
– tinstaafl
Oct 25 at 17:25
Are you sure? lists are not arrays
– gstackoverflow
Oct 25 at 17:33
Are you sure? lists are not arrays
– gstackoverflow
Oct 25 at 17:33
@gstackoverflow - a quote from the
addAll
doc page The new elements will appear in this list in the order that they are returned by the specified collection's iterator.
– tinstaafl
Oct 25 at 17:41
@gstackoverflow - a quote from the
addAll
doc page The new elements will appear in this list in the order that they are returned by the specified collection's iterator.
– tinstaafl
Oct 25 at 17:41
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%2f206270%2fquicksort-implementation-with-pivotal-calculated-as-middle-element%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
I think the title fails to name the distinguishing feature of this implementation: 3-way distributing values to
List
s. You present uncommented/undocumented code. You needlessly forsake genericity. (I was tempted to down-vote the question out of considering the code presented abysmal - the voting hints stress usefulness of the question.)– greybeard
2 days ago
@greybeard I think code is self explained
– gstackoverflow
2 days ago
Try using your method in a separate source file. See what tool support you get from your IDE of choice with or without doc comments. Revisit your code a couple of years later.
– greybeard
2 days ago
@greybeard, could you provide concrete points you don't like? It is really unclear what you to say
– gstackoverflow
2 days ago
could you provide concrete points you [have an opinion on]?
I try to refrain from getting into details of uncommented code.– greybeard
2 days ago