Skip to content

Commit b6abb7c

Browse files
authored
Merge pull request thuva4#267 from AtoMc/master
Add Quickselect for Java
2 parents a812795 + d93b684 commit b6abb7c

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,5 @@ Unfortunately, sometimes the bug can be only reproduced in your project or in yo
130130
- [tushar-dtu](https://github.com/tushar-dtu)
131131
- [AymanASamyM](https://github.com/AymanASamyM)
132132
- [arunpyasi](https://github.com/arunpyasi)
133-
- [Akos Kovacs](https://github.com/plaidshirtakos)
133+
- [Akos Kovacs](https://github.com/plaidshirtakos)
134+
- [AtoMc](https://github.com/AtoMc)

Doomsday/Java/Doomsday.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Doomsday {
1818
public static int dow(int y, int m, int d) {
1919
final int[] t = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
2020
y -= (m < 3) ? 1 : 0;
21-
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
21+
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
2222
}
2323

2424
/**

QuickSelect/Java/QuickSelect.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Find the kth smallest element in an unordered array.
3+
*
4+
* @author Atom
5+
* @see <a href="https://en.wikipedia.org/wiki/Quickselect">Quickselect</a>
6+
*/
7+
public class QuickSelect {
8+
9+
/**
10+
* Lomuto partition scheme
11+
*
12+
* @param arr
13+
* @param low
14+
* @param high
15+
* @return the pivot's final location
16+
*/
17+
private static <T extends Comparable<? super T>> int partition(final T[] arr, final int low, final int high) {
18+
T pivot = arr[high];
19+
int index = low;
20+
for (int i = low; i < high; i++) {
21+
if (arr[i].compareTo(pivot) <= 0) {
22+
swap(arr, i, index);
23+
index++;
24+
}
25+
}
26+
swap(arr, index, high);
27+
return index;
28+
}
29+
30+
/**
31+
* Find the kth smallest element in an unordered array.
32+
*
33+
* @param arr
34+
* @param kth from 0 to arr.length - 1
35+
* @return kth smallest element
36+
*/
37+
public static <T extends Comparable<? super T>> T select(final T[] arr, final int kth) {
38+
if (kth < 0 || kth > arr.length - 1) throw new IllegalArgumentException("elements in the array = " + arr.length + ", kth(0..length-1) = " + kth);
39+
40+
int left = 0;
41+
int right = arr.length - 1;
42+
while (right >= left) {
43+
int pivotIndex = partition(arr, left, right);
44+
if (kth == pivotIndex) {
45+
return arr[pivotIndex];
46+
} else if (kth < pivotIndex) {
47+
right = pivotIndex - 1;
48+
} else {
49+
left = pivotIndex + 1;
50+
}
51+
}
52+
return null;
53+
}
54+
55+
private static void swap(Object arr[], int i1, int i2) {
56+
if (i1 == i2) { return; }
57+
58+
Object temp = arr[i1];
59+
arr[i1] = arr[i2];
60+
arr[i2] = temp;
61+
}
62+
63+
public static void main(String[] args) {
64+
for (int kth = 0; kth < 10; kth++) {
65+
Integer[] a = { 1, 4, 2, 5, 0, 3, 9, 7, 6, 8 };
66+
System.out.println("kth(" + kth + ") smallest element = " + select(a, kth));
67+
}
68+
}
69+
70+
}

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ MiniMaxWithABPruning | :+1: | | | | | | | |
4444
Modified_Binary_Search | | | | :+1: | | | | |
4545
Postman Sort | | | | :+1: | | | | |
4646
Quick Sort | :+1: | :+1: | | | :+1: | :+1: | :+1: | :+1: | :+1:
47-
Quick Select | | :+1: | | | | | :+1: | |
47+
Quick Select | :+1: | :+1: | | | | | :+1: | |
4848
Uniform-cost search | :+1: | | | | | :+1: | :+1: | |
4949
RadixSort | :+1: | :+1: | | | :+1: | | | |
5050
RobinCarp | :+1: | | | | | | | |
@@ -591,6 +591,8 @@ Union Find |:+1:| | | | | | | |
591591

592592
* Q-learning : learn an action-value function that gives the expected utility of taking a given action in a given state and following a fixed policy thereafter
593593

594+
* [Quickselect](QuickSelect) : selection algorithm to find the kth smallest element in an unordered list
595+
594596
* [Quicksort](QuickSort) : divide list into two, with all items on the first list coming before all items on the second list.; then sort the two lists. Often the method of choice
595597

596598
* Quine–McCluskey algorithm : Also called as Q-M algorithm, programmable method for simplifying the boolean equations.

0 commit comments

Comments
 (0)