215. Kth largest Element in an Array

Source: Internet
Author: User

First review the quicksort.

First understand the partition function, that is, the last number of the selected sequence as pivot, smaller than it to its left, larger than it on its right, it is in the middle. But the left and right parts of it are not sorted.

The return value is where the pivot is located, where the pivot position is its final position, i.e. index is 5, indicating that it is the 6th largest number.

Quicksort inside is called partition, constantly sorting around it until low >= high

Partition Pseudo-code

1 paritition (arr, Low, hight): 2 3 index = low; // index is the first index of a number that is not less than pivot 4 5 for the number from low to High: 6 7 If this number is smaller than pivot, then: 8 Exchange Index and this number 9 index++  swap pivot and index number of each of the  returned index

Quicksort Pseudo-code

1 quicksort (arr, Low, high): 2 3 If low< high:45 partitionindex = partition (arr, low, high); 6 partition (arr, low, partitionIndex-1); 7 partition (arr, Partitionindex + 1, high);

So the idea of this problem is, first partition once, and then if it happens to be the first k-1, then return, otherwise if partition finish the left add up the number has exceeded k-1, then the right side anyway is smaller than this pivot, only to the left hand recursion, Or the right hand recursion.

1     Public intFindkthlargest (int[] Nums,intk) {2         if(Nums.length = = 1) {3           returnNums[0];4         }5         intIDX =-1;6         intleft = 0, right = nums.length-1;7          while(IDX! = K-1) {8IDX =partition (Nums, left, right);9           if(Idx > K-1) {Tenright = Idx-1; One}Else { Aleft = idx + 1; -           } -         } the         returnNums[idx]; -    } -      -     Private intPartitionint[] Nums,intLowintHigh ) { +         intindex =Low ; -         intPivot =Nums[high]; +          for(inti = low; I < high; i++) { A             if(Nums[i] >pivot) { at swap (nums, index, i); -index++; -             } -         } - swap (nums, index, high); -         returnindex; in     } -      to     Private voidSwapint[] Nums,intIDX1,intidx2) { +         intTMP =nums[idx1]; -NUMS[IDX1] =NUMS[IDX2]; theNUMS[IDX2] =tmp; *}

Another way is to use Priorityqueue.

The PQ is worth recording:

1. Automatic maintenance from small to large order, you can poll (), Peek (). But it's inconvenient to get the elements in a certain position.

2. Of course you can write your own comparator, but if you simply want to reverse the word, you can new Pq<> (Collections.reverseorder ())

1      Public intFindkthlargest (int[] Nums,intk) {2Priorityqueue<integer> PQ =NewPriorityqueue<integer>();3          for(inti = 0; i < nums.length; i++) {4 Pq.offer (Nums[i]);5             if(Pq.size () >k) {6 Pq.poll ();7             }8         }9         returnPq.poll ();Ten}

215. Kth largest Element in an Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.