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?










share|improve this question
















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 to Lists. 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















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?










share|improve this question
















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 to Lists. 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 to Lists. 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 Lists. 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 Lists. 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 Lists. 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










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.






share|improve this answer





















  • 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 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











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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.






share|improve this answer





















  • 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 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















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.






share|improve this answer





















  • 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 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













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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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 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


















  • 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 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
















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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Quarter-circle Tiles

build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

Mont Emei