-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0215-Kth-largest-element-in-an-array.cs
More file actions
51 lines (44 loc) · 1.27 KB
/
0215-Kth-largest-element-in-an-array.cs
File metadata and controls
51 lines (44 loc) · 1.27 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
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0215.Kth_largest_element_in_an_array
{
public class _0215_Kth_largest_element_in_an_array
{
public int FindKthLargest(int[] nums, int k)
{
quickSort(nums, 0, nums.Length - 1, nums.Length - k);
return nums[nums.Length - k];
}
private void quickSort(int[] nums, int left, int right, int k)
{
int i = left;
int j = right;
int pivot = nums[left];
int temp;
while (i < j)
{
// begin left.
while (nums[j] >= pivot && i < j)
j--;
// begin right.
while (nums[i] <= pivot && i < j)
i++;
// swap
if (i < j)
{
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
// change i & j position.
nums[left] = nums[j];
nums[j] = pivot;
if (j < k)
quickSort(nums, j + 1, right, k);
else if (j > k)
quickSort(nums, left, j - 1, k);
}
}
}