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;
}
java algorithm big-o quickselect
add a comment |
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;
}
java algorithm big-o quickselect
I think that is impossible to do inO(n)
best case any more. You can use hash tables, but that gives you only expected time.
– Yola
Nov 11 at 15:58
add a comment |
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;
}
java algorithm big-o quickselect
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
java algorithm big-o quickselect
asked Nov 11 at 10:39
user7722505
I think that is impossible to do inO(n)
best case any more. You can use hash tables, but that gives you only expected time.
– Yola
Nov 11 at 15:58
add a comment |
I think that is impossible to do inO(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
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
If adding 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
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
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
accepted
If adding 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
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
add a comment |
up vote
0
down vote
accepted
If adding 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
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
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
If adding 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
If adding 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
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
add a comment |
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
add a comment |
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.
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%2fstackoverflow.com%2fquestions%2f53247925%2fhow-to-implement-duplicates-in-quickselect%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 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