Find the kth largest element in an unsorted array

11 篇文章 0 订阅

  This is should be an old problem, I had written some code related to this problem long time ago. This time I just write the code for review. The idea is the same as quick sort. There are many other methods too, but the divide and conquer way should be a better one.

No more words , just paste the code now.

package leetcode.num_215;

import java.util.Random;

/*
 * 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.
 */
public class Solution {
	public int findKthLargest(int[] nums, int k) {
		int newK = nums.length - k + 1;
		
		return randomizedSelect(nums, 0, nums.length-1, newK);
	}
	
	public int randomizedSelect(int[] nums, int left, int right, int k) {
		if (k < 0 || k > nums.length) throw new IllegalArgumentException();
		
		int pivot = randomPartition(nums, left, right);
		
		if (left == right) return nums[left];
		
		int d = pivot - left + 1;
		
		if (k == d) {
			return nums[pivot];
		} else if (k < d) {
			return randomizedSelect(nums, left, pivot-1, k);
		} else {
			return randomizedSelect(nums, pivot+1, right, k-d);
		}
	}
	
	private int partition(int[] nums, int left, int right) {
		int x = nums[right];
		int i = left-1;
		
		for (int j = left; j < right; j++) {
			if (nums[j] < x) {
				i += 1;
				swap(nums, i, j);
			}
		}
		swap(nums, i+1, right);
		return i+1;
	}
	
	private void swap(int[] nums, int i, int j) {
		int temp = nums[i];
		nums[i] = nums[j];
		nums[j] = temp;
	}
	
	private int randomPartition(int[] nums, int left, int right) {
		Random random = new Random();
		int r = random.nextInt(right+1-left) + left; // This place needs to pay attention.
		swap(nums, r, right);
		
		return partition(nums, left, right);
	}
	
	public void randomQuickSort(int[] nums, int left, int right) {
		if (left < 0 || right > nums.length) return;
		
		if (left < right) {
			int q = randomPartition(nums, left, right);
			randomQuickSort(nums, left, q-1);
			randomQuickSort(nums, q+1, right);
		}
	}
	
	public void printNums(int[] nums) {
		for (int i = 0; i < nums.length; i++) {
			System.out.print(nums[i] + " ");
		}
		System.out.println();
	}
	
	public static void main(String[] args) {
		int[] nums = { 3, 2, 1, 5, 6, 4 };
		Solution s = new Solution();
		s.printNums(nums);
		s.randomQuickSort(nums, 0, nums.length-1);
		s.printNums(nums);
		
		int num = s.findKthLargest(nums, 2);
		System.out.println(num);
		
		int[] nums1 = { 1 };
		int num1 = s.findKthLargest(nums1, 1);
		System.out.println(num1);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值