How to implement duplicates in QuickSelect











up vote
2
down vote

favorite












I have made the quick select algorithm, which is to find the kth smallest number in an array. My problem is, it only works with an array without duplicates.
If I have an array




arr = {1,2,2,3,5,5,8,2,4,8,8}




It will say that the third smallest number is 2, but it is actually 3.



I am stuck on what to do, here are my two methods quickSelect and Partition:



private int quickselect(int array, int leftIndex, int rightIndex, int kthSmallest) {

if(kthSmallest > array.length - 1){
System.out.print("Number does not exist. Please enter a number less than: ");
return array.length - 1;
}

if (leftIndex == rightIndex) {
return array[leftIndex];
}

int indexOfPivot = generatePivot(leftIndex, rightIndex);

indexOfPivot = quickSelectPartition(array, leftIndex, rightIndex, indexOfPivot);

if (kthSmallest == indexOfPivot) {

return array[kthSmallest];

} else if (kthSmallest < indexOfPivot) {

return quickselect(array, leftIndex, indexOfPivot - 1, kthSmallest);

} else {

return quickselect(array, indexOfPivot + 1, rightIndex, kthSmallest);
}
}


private int quickSelectPartition(int array, int left, int right, int pivotIndex) {

int pivotValue = array[pivotIndex];

swapIndexes(array, pivotIndex, right);

int firstPointer = left;

for(int secondPointer = left; secondPointer < right; secondPointer++) {

if(array[secondPointer] < pivotValue) {

swapIndexes(array, firstPointer, secondPointer);

firstPointer++;
}
}

swapIndexes(array, right, firstPointer);

return firstPointer;
}









share|improve this question






















  • I think that is impossible to do in O(n) best case any more. You can use hash tables, but that gives you only expected time.
    – Yola
    Nov 11 at 15:58















up vote
2
down vote

favorite












I have made the quick select algorithm, which is to find the kth smallest number in an array. My problem is, it only works with an array without duplicates.
If I have an array




arr = {1,2,2,3,5,5,8,2,4,8,8}




It will say that the third smallest number is 2, but it is actually 3.



I am stuck on what to do, here are my two methods quickSelect and Partition:



private int quickselect(int array, int leftIndex, int rightIndex, int kthSmallest) {

if(kthSmallest > array.length - 1){
System.out.print("Number does not exist. Please enter a number less than: ");
return array.length - 1;
}

if (leftIndex == rightIndex) {
return array[leftIndex];
}

int indexOfPivot = generatePivot(leftIndex, rightIndex);

indexOfPivot = quickSelectPartition(array, leftIndex, rightIndex, indexOfPivot);

if (kthSmallest == indexOfPivot) {

return array[kthSmallest];

} else if (kthSmallest < indexOfPivot) {

return quickselect(array, leftIndex, indexOfPivot - 1, kthSmallest);

} else {

return quickselect(array, indexOfPivot + 1, rightIndex, kthSmallest);
}
}


private int quickSelectPartition(int array, int left, int right, int pivotIndex) {

int pivotValue = array[pivotIndex];

swapIndexes(array, pivotIndex, right);

int firstPointer = left;

for(int secondPointer = left; secondPointer < right; secondPointer++) {

if(array[secondPointer] < pivotValue) {

swapIndexes(array, firstPointer, secondPointer);

firstPointer++;
}
}

swapIndexes(array, right, firstPointer);

return firstPointer;
}









share|improve this question






















  • I think that is impossible to do in O(n) best case any more. You can use hash tables, but that gives you only expected time.
    – Yola
    Nov 11 at 15:58













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have made the quick select algorithm, which is to find the kth smallest number in an array. My problem is, it only works with an array without duplicates.
If I have an array




arr = {1,2,2,3,5,5,8,2,4,8,8}




It will say that the third smallest number is 2, but it is actually 3.



I am stuck on what to do, here are my two methods quickSelect and Partition:



private int quickselect(int array, int leftIndex, int rightIndex, int kthSmallest) {

if(kthSmallest > array.length - 1){
System.out.print("Number does not exist. Please enter a number less than: ");
return array.length - 1;
}

if (leftIndex == rightIndex) {
return array[leftIndex];
}

int indexOfPivot = generatePivot(leftIndex, rightIndex);

indexOfPivot = quickSelectPartition(array, leftIndex, rightIndex, indexOfPivot);

if (kthSmallest == indexOfPivot) {

return array[kthSmallest];

} else if (kthSmallest < indexOfPivot) {

return quickselect(array, leftIndex, indexOfPivot - 1, kthSmallest);

} else {

return quickselect(array, indexOfPivot + 1, rightIndex, kthSmallest);
}
}


private int quickSelectPartition(int array, int left, int right, int pivotIndex) {

int pivotValue = array[pivotIndex];

swapIndexes(array, pivotIndex, right);

int firstPointer = left;

for(int secondPointer = left; secondPointer < right; secondPointer++) {

if(array[secondPointer] < pivotValue) {

swapIndexes(array, firstPointer, secondPointer);

firstPointer++;
}
}

swapIndexes(array, right, firstPointer);

return firstPointer;
}









share|improve this question













I have made the quick select algorithm, which is to find the kth smallest number in an array. My problem is, it only works with an array without duplicates.
If I have an array




arr = {1,2,2,3,5,5,8,2,4,8,8}




It will say that the third smallest number is 2, but it is actually 3.



I am stuck on what to do, here are my two methods quickSelect and Partition:



private int quickselect(int array, int leftIndex, int rightIndex, int kthSmallest) {

if(kthSmallest > array.length - 1){
System.out.print("Number does not exist. Please enter a number less than: ");
return array.length - 1;
}

if (leftIndex == rightIndex) {
return array[leftIndex];
}

int indexOfPivot = generatePivot(leftIndex, rightIndex);

indexOfPivot = quickSelectPartition(array, leftIndex, rightIndex, indexOfPivot);

if (kthSmallest == indexOfPivot) {

return array[kthSmallest];

} else if (kthSmallest < indexOfPivot) {

return quickselect(array, leftIndex, indexOfPivot - 1, kthSmallest);

} else {

return quickselect(array, indexOfPivot + 1, rightIndex, kthSmallest);
}
}


private int quickSelectPartition(int array, int left, int right, int pivotIndex) {

int pivotValue = array[pivotIndex];

swapIndexes(array, pivotIndex, right);

int firstPointer = left;

for(int secondPointer = left; secondPointer < right; secondPointer++) {

if(array[secondPointer] < pivotValue) {

swapIndexes(array, firstPointer, secondPointer);

firstPointer++;
}
}

swapIndexes(array, right, firstPointer);

return firstPointer;
}






java algorithm big-o quickselect






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 10:39







user7722505



















  • I think that is impossible to do in O(n) best case any more. You can use hash tables, but that gives you only expected time.
    – Yola
    Nov 11 at 15:58


















  • I think that is impossible to do in O(n) best case any more. You can use hash tables, but that gives you only expected time.
    – Yola
    Nov 11 at 15:58
















I think that is impossible to do in O(n) best case any more. You can use hash tables, but that gives you only expected time.
– Yola
Nov 11 at 15:58




I think that is impossible to do in O(n) best case any more. You can use hash tables, but that gives you only expected time.
– Yola
Nov 11 at 15:58












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










If adding 2xN to the running time is acceptable, you could start by copying distinct elements into a new array:



private int getDistinct(int array) {
Set<Integer> distinct = new HashSet<>();
int endIdx = 0;

for (int element : array) {

if (distinct.add(element)) {
array[endIdx++] = element;
}
}

return Arrays.copyOfRange(array, 0, endIdx);
}


and then do quickselect on that:



int arr = new int {1, 2, 2, 3, 5, 5, 8, 2, 4, 8, 8};
int kthSmallest = 6;

int distinctArray = getDistinct(arr);
int kthSmallestElement = quickselect(distinctArray, 0, distinctArray.length - 1, kthSmallest - 1);

System.out.println(kthSmallestElement);


Output:



8





share|improve this answer























  • The running time of QuickSelect is best case and average case O(n), so adding 2N would still be O(n) I suppose?
    – user7722505
    Nov 11 at 16:12










  • Yes, that is correct.
    – runcoderun
    Nov 11 at 16:16










  • I guess that is the answer then, thank you.
    – user7722505
    Nov 11 at 16:17










  • You are welcome!
    – runcoderun
    Nov 11 at 16:18











Your Answer






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: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53247925%2fhow-to-implement-duplicates-in-quickselect%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



accepted










If adding 2xN to the running time is acceptable, you could start by copying distinct elements into a new array:



private int getDistinct(int array) {
Set<Integer> distinct = new HashSet<>();
int endIdx = 0;

for (int element : array) {

if (distinct.add(element)) {
array[endIdx++] = element;
}
}

return Arrays.copyOfRange(array, 0, endIdx);
}


and then do quickselect on that:



int arr = new int {1, 2, 2, 3, 5, 5, 8, 2, 4, 8, 8};
int kthSmallest = 6;

int distinctArray = getDistinct(arr);
int kthSmallestElement = quickselect(distinctArray, 0, distinctArray.length - 1, kthSmallest - 1);

System.out.println(kthSmallestElement);


Output:



8





share|improve this answer























  • The running time of QuickSelect is best case and average case O(n), so adding 2N would still be O(n) I suppose?
    – user7722505
    Nov 11 at 16:12










  • Yes, that is correct.
    – runcoderun
    Nov 11 at 16:16










  • I guess that is the answer then, thank you.
    – user7722505
    Nov 11 at 16:17










  • You are welcome!
    – runcoderun
    Nov 11 at 16:18















up vote
0
down vote



accepted










If adding 2xN to the running time is acceptable, you could start by copying distinct elements into a new array:



private int getDistinct(int array) {
Set<Integer> distinct = new HashSet<>();
int endIdx = 0;

for (int element : array) {

if (distinct.add(element)) {
array[endIdx++] = element;
}
}

return Arrays.copyOfRange(array, 0, endIdx);
}


and then do quickselect on that:



int arr = new int {1, 2, 2, 3, 5, 5, 8, 2, 4, 8, 8};
int kthSmallest = 6;

int distinctArray = getDistinct(arr);
int kthSmallestElement = quickselect(distinctArray, 0, distinctArray.length - 1, kthSmallest - 1);

System.out.println(kthSmallestElement);


Output:



8





share|improve this answer























  • The running time of QuickSelect is best case and average case O(n), so adding 2N would still be O(n) I suppose?
    – user7722505
    Nov 11 at 16:12










  • Yes, that is correct.
    – runcoderun
    Nov 11 at 16:16










  • I guess that is the answer then, thank you.
    – user7722505
    Nov 11 at 16:17










  • You are welcome!
    – runcoderun
    Nov 11 at 16:18













up vote
0
down vote



accepted







up vote
0
down vote



accepted






If adding 2xN to the running time is acceptable, you could start by copying distinct elements into a new array:



private int getDistinct(int array) {
Set<Integer> distinct = new HashSet<>();
int endIdx = 0;

for (int element : array) {

if (distinct.add(element)) {
array[endIdx++] = element;
}
}

return Arrays.copyOfRange(array, 0, endIdx);
}


and then do quickselect on that:



int arr = new int {1, 2, 2, 3, 5, 5, 8, 2, 4, 8, 8};
int kthSmallest = 6;

int distinctArray = getDistinct(arr);
int kthSmallestElement = quickselect(distinctArray, 0, distinctArray.length - 1, kthSmallest - 1);

System.out.println(kthSmallestElement);


Output:



8





share|improve this answer














If adding 2xN to the running time is acceptable, you could start by copying distinct elements into a new array:



private int getDistinct(int array) {
Set<Integer> distinct = new HashSet<>();
int endIdx = 0;

for (int element : array) {

if (distinct.add(element)) {
array[endIdx++] = element;
}
}

return Arrays.copyOfRange(array, 0, endIdx);
}


and then do quickselect on that:



int arr = new int {1, 2, 2, 3, 5, 5, 8, 2, 4, 8, 8};
int kthSmallest = 6;

int distinctArray = getDistinct(arr);
int kthSmallestElement = quickselect(distinctArray, 0, distinctArray.length - 1, kthSmallest - 1);

System.out.println(kthSmallestElement);


Output:



8






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 16:14

























answered Nov 11 at 15:56









runcoderun

36417




36417












  • The running time of QuickSelect is best case and average case O(n), so adding 2N would still be O(n) I suppose?
    – user7722505
    Nov 11 at 16:12










  • Yes, that is correct.
    – runcoderun
    Nov 11 at 16:16










  • I guess that is the answer then, thank you.
    – user7722505
    Nov 11 at 16:17










  • You are welcome!
    – runcoderun
    Nov 11 at 16:18


















  • The running time of QuickSelect is best case and average case O(n), so adding 2N would still be O(n) I suppose?
    – user7722505
    Nov 11 at 16:12










  • Yes, that is correct.
    – runcoderun
    Nov 11 at 16:16










  • I guess that is the answer then, thank you.
    – user7722505
    Nov 11 at 16:17










  • You are welcome!
    – runcoderun
    Nov 11 at 16:18
















The running time of QuickSelect is best case and average case O(n), so adding 2N would still be O(n) I suppose?
– user7722505
Nov 11 at 16:12




The running time of QuickSelect is best case and average case O(n), so adding 2N would still be O(n) I suppose?
– user7722505
Nov 11 at 16:12












Yes, that is correct.
– runcoderun
Nov 11 at 16:16




Yes, that is correct.
– runcoderun
Nov 11 at 16:16












I guess that is the answer then, thank you.
– user7722505
Nov 11 at 16:17




I guess that is the answer then, thank you.
– user7722505
Nov 11 at 16:17












You are welcome!
– runcoderun
Nov 11 at 16:18




You are welcome!
– runcoderun
Nov 11 at 16:18


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


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





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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247925%2fhow-to-implement-duplicates-in-quickselect%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

Bressuire

Vorschmack

Quarantine