LeetCode:215 数组中的第K个最大元素(堆排序)
class Solution { public void HeapAdjust(int[] nums,int s,int d){ //int t = nums[s]; int k = s; for(int i=s*2+1;i<=d;i=i*2+1){ if((i+1<=d)&&nums[i]<nums[i+1]){ i++; } if(nums[i]>nums[k]){ int a = nums[i]; nums[i] = nums[k]; nums[k] = a; k=i; } else{ return; } } } public int findKthLargest(int[] nums, int k) { int len = nums.length; for(int i=len/2-1;i>=0;i--){ HeapAdjust(nums,i,len-1); } for(int i=0;i<k-1;i++){ nums[0]=nums[len-1-i]; HeapAdjust(nums,0,len-1-i); } return nums[0]; } }