-
Notifications
You must be signed in to change notification settings - Fork 0
/
215_KthLargestElementInAnArray.java
175 lines (164 loc) · 4.9 KB
/
215_KthLargestElementInAnArray.java
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
// For example,
// Given [3,2,1,5,6,4] and k = 2, return 5.
// Note:
// You may assume k is always valid, 1 ≤ k ≤ array's length.
//quickSort modified for O(n) time
//T(n) = T(n / 2) + O(n); //O(n) is partition time
public class Solution {
private void shuffle(int[] nums) {//shuffle array, random [0, 1)
for(int i=0; i<nums.length; i++) {
int s = i + (int) (Math.random() * (nums.length - i));
swap(nums, i, s);
}
}
public int findKthLargest(int[] nums, int k) {
shuffle(nums);
int start = 0;
int end = nums.length - 1;
while(true) {
int pivot = partition(nums, start, end);
if(pivot == nums.length - k) return nums[pivot];
else if(pivot > nums.length - k) end = pivot - 1;
else start = pivot + 1;
}
}
private int partition(int[] nums, int start, int end) {
int pivot = nums[end];
int pIndex = start;
for(int i=start; i< end; i++) {
if(nums[i] < pivot) {
swap(nums, i, pIndex);
pIndex++;
}
}
swap(nums, pIndex, end);
return pIndex;
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
//quickSort
public class Solution {
public int findKthLargest(int[] nums, int k) {
quickSort(nums, 0, nums.length -1 );
return nums[nums.length - k];
}
private void quickSort(int[] nums, int start, int end) {
if(start < end) {
int pivot = partition(nums, start, end);
quickSort(nums, start, pivot - 1);
quickSort(nums, pivot + 1, end);
}
}
private int partition(int[] nums, int start, int end) {
int pivot = nums[end];
int pIndex = start;
for(int i=start; i< end; i++) {
if(nums[i] < pivot) {
swap(nums, i, pIndex);
pIndex++;
}
}
swap(nums, pIndex, end);
return pIndex;
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
//MergeSort
public class Solution {
public int findKthLargest(int[] nums, int k) {
mergeSort(nums);
return nums[nums.length - k];
}
private void mergeSort(int[] nums) {
if(nums.length < 2) return;
int[] a = new int[nums.length / 2];
int[] b = new int[nums.length - nums.length / 2];
for(int i=0; i<a.length; i++) a[i] = nums[i];
for(int j=0; j<b.length; j++) b[j] = nums[a.length + j];
mergeSort(a);
mergeSort(b);
merge(nums, a, b);
}
private void merge(int[] nums, int[] a, int[] b) {
int i = 0, j = 0, k = 0;
while(i < a.length && j < b.length) {
if(a[i] < b[j]) {
nums[k++] = a[i++];
} else {
nums[k++] = b[j++];
}
}
while(i < a.length) {
nums[k++] = a[i++];
}
while(j < b.length) {
nums[k++] = b[j++];
}
}
}
//insertion sort
public class Solution {
public int findKthLargest(int[] nums, int k) {
insertionSort(nums);
return nums[nums.length - k];
}
private void insertionSort(int[] nums) {
for(int i = 1; i<nums.length; i++) {
int value = nums[i];
int hole = i;
while(hole > 0 && nums[hole - 1] > value) {
nums[hole] = nums[hole - 1];
hole--;
}
nums[hole] = value;
}
}
}
//seletion sort
public class Solution {
public int findKthLargest(int[] nums, int k) {
selectionSort(nums);
return nums[nums.length - k];
}
private void selectionSort(int[] nums) {
for(int i=0; i<nums.length - 1; i++) {
int iMin = i;
for(int j = i + 1; j<nums.length; j++) {
if(nums[j] < nums[iMin]) iMin = j;
}
int temp = nums[iMin];
nums[iMin] = nums[i];
nums[i] = temp;
}
}
}
//bubble sort
public class Solution {
public int findKthLargest(int[] nums, int k) {
bubbleSort(nums);
return nums[nums.length - k];
}
private void bubbleSort(int[] nums) {
for(int i=0; i<nums.length; i++) {
int flag = 0;
for(int j=0; j<nums.length - i - 1; j++) {
if(nums[j] > nums[j + 1]) {
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
flag = 1;
}
}
if(flag == 0) return;
}
}
}