-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kth largest element in unsorted array LC215
- Loading branch information
1 parent
238252a
commit 2f9c7d7
Showing
2 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package ArraysAndStrings; | ||
|
||
public class Kthlargest | ||
{ | ||
public static int findKthLargest(int[] nums, int k) | ||
{ | ||
if (null == nums || nums.length == 0) | ||
{ | ||
return Integer.MIN_VALUE; | ||
} | ||
|
||
return qSort(nums, 0, nums.length-1, k); | ||
} | ||
|
||
private static int qSort(int[] nums, int left, int right, int k) | ||
{ | ||
if (nums[left] >= nums[right]) | ||
{ | ||
return nums[right]; | ||
} | ||
|
||
int i = left; | ||
for (int j = left + 1; j <= right; j++) | ||
{ | ||
if (nums[j] > nums[i]) { | ||
i++; | ||
swap(nums, i, j); | ||
} | ||
} | ||
swap(nums, i, left); | ||
|
||
if (k == i + 1) | ||
{ | ||
return nums[i]; | ||
} | ||
else if (k > i + 1) | ||
{ | ||
return qSort(nums, i+1, right, k); | ||
} | ||
else | ||
{ | ||
return qSort(nums, left, i-1, k); | ||
} | ||
} | ||
|
||
private static void swap(int[] nums, int i, int j) | ||
{ | ||
int tmp = nums[i]; | ||
nums[i] = nums[j]; | ||
nums[j] = tmp; | ||
} | ||
|
||
public static void main(String[] args) | ||
{ | ||
int secondlargest = findKthLargest(new int[]{3,2,5,1,6,4}, 2); | ||
} | ||
} |