-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGetMedian.java
More file actions
86 lines (66 loc) · 2.06 KB
/
GetMedian.java
File metadata and controls
86 lines (66 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.company;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
public class GetMedian {
ArrayList<Integer> mArray = new ArrayList<>();
public void Insert(Integer num) {
mArray.add(num);
}
public Double GetMedian() {
Object[] objects = mArray.toArray();
int[] tempArray = new int[objects.length];
int i = 0;
for (Object a : objects) {
tempArray[i++] = (int) a;
}
Arrays.sort(tempArray);
// quickSort(tempArray, 0, tempArray.length - 1);
int index = tempArray.length - 1;
if (index % 2 == 1)
return (double) (tempArray[index / 2] +
tempArray[index / 2 + 1]) / 2;
else
return (double) tempArray[index / 2];
}
public void quickSort(int[] data, int left, int right) {
if (left > right) {
return;
}
int i = left;
int j = right;
int x = data[i];
while (i < j) {
while (i < j && data[j] > x) j--;
if (i < j) data[i] = data[j];
while (i < j && data[i] < x) i++;
if (i < j) data[j] = data[i];
}
data[i] = x;
quickSort(data, left, i - 1);
quickSort(data, i + 1, right);
}
@Test
public void test() {
//5,2,3,4,1,6,7,0,8
Insert(5);
System.out.print(GetMedian() + " ");
Insert(2);
System.out.print(GetMedian() + " ");
Insert(3);
System.out.print(GetMedian() + " ");
Insert(4);
System.out.print(GetMedian() + " ");
Insert(1);
System.out.print(GetMedian() + " ");
Insert(6);
System.out.print(GetMedian() + " ");
Insert(7);
System.out.print(GetMedian() + " ");
Insert(0);
System.out.print(GetMedian() + " ");
Insert(8);
System.out.print(GetMedian() + " ");
}
//"5.00 3.50 3.00 3.50 3.00 3.50 4.00 3.50 4.00 "
}