Skip to content

Commit d089bcb

Browse files
author
zhangbo
committed
add order statistics
1 parent 6aacb78 commit d089bcb

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package algorithm.other;
2+
3+
import common.utils.ArrayUtils;
4+
5+
/**
6+
* Created by boyce on 2014/9/10.
7+
*/
8+
public class OrderStatistics<T extends Comparable> {
9+
10+
/**
11+
* find min element from array
12+
* @param array
13+
* @return min element
14+
* list all array to find min element, O(n)
15+
*/
16+
public T findMin(T[] array) {
17+
if (ArrayUtils.isEmpty(array))
18+
return null;
19+
20+
if (array.length == 1)
21+
return array[0];
22+
23+
T min = array[0];
24+
for (int i=1; i<array.length; i++)
25+
if (array[i].compareTo(min) < 0)
26+
min = array[i];
27+
28+
return min;
29+
}
30+
31+
/**
32+
* find max element from array
33+
* @param array
34+
* @return max element
35+
* list all array to find max element, O(n)
36+
*/
37+
public T findMax(T[] array) {
38+
if (ArrayUtils.isEmpty(array))
39+
return null;
40+
41+
if (array.length == 1)
42+
return array[0];
43+
44+
T max = array[0];
45+
for (int i=1; i<array.length; i++)
46+
if (array[i].compareTo(max) > 0)
47+
max = array[i];
48+
49+
return max;
50+
}
51+
52+
public T findArticleN(T[] array, int n) {
53+
if (n >= array.length)
54+
throw new ArrayIndexOutOfBoundsException("");
55+
56+
return this.findArticleN(array, 0, array.length-1, n);
57+
}
58+
59+
/**
60+
* find article n element from array
61+
* @param array resource array
62+
* @param p from p index in array
63+
* @param q end q index in array
64+
* @param i article i
65+
* @return
66+
*/
67+
private T findArticleN(T[] array, int p, int q, int i) {
68+
69+
if (p == q)
70+
return array[p];
71+
72+
int t = this.partition(array, p, q);
73+
if (t == i-1)
74+
return array[t];
75+
76+
else if (t < i-1)
77+
return findArticleN(array, t+1, q, i-t);
78+
79+
else
80+
return findArticleN(array, p, t-1, i);
81+
}
82+
83+
private int partition(T[] array, int p, int q) {
84+
T x = array[q];
85+
int i = p -1;
86+
for (int j=p; j<q; j++) {
87+
// if the target[j] < x , swap the index ++i element and index j element
88+
if (array[j].compareTo(x) < 0) {
89+
this.swap(array, ++i, j);
90+
91+
// if the target[j] >= x, do nothing, continue j++ to handle the next element
92+
} else
93+
;
94+
95+
}
96+
this.swap(array, i+1, q);
97+
98+
return i+1;
99+
}
100+
101+
private void swap(T[] target, int p, int q) {
102+
T temp = target[p];
103+
target[p] = target[q];
104+
target[q] = temp;
105+
}
106+
107+
public static void main(String[] args) {
108+
OrderStatistics<Integer> orderStatistics = new OrderStatistics<Integer>();
109+
Integer[] array = {2, 3, 11, 4, 6, 2, 0, 13, 7, 9, 12};
110+
111+
System.out.println(orderStatistics.findMax(array));
112+
System.out.println(orderStatistics.findMin(array));
113+
}
114+
}

0 commit comments

Comments
 (0)