{"id":181,"date":"2018-03-06T07:42:28","date_gmt":"2018-03-06T07:42:28","guid":{"rendered":"http:\/\/www.datastructure.org\/?p=181"},"modified":"2025-04-21T13:49:11","modified_gmt":"2025-04-21T13:49:11","slug":"find-the-kth-largest-number-or-median-of-medians","status":"publish","type":"post","link":"https:\/\/www.bhargaviurs.com\/index.php\/2018\/03\/06\/find-the-kth-largest-number-or-median-of-medians\/","title":{"rendered":"Find the kth largest number or Median of medians"},"content":{"rendered":"<h6><strong>Finding the Kth biggest using Heap Sort<\/strong><\/h6>\n<ol>\n<li>Sort it by getting the biggest, then the next biggest, then the next and so on. . .<\/li>\n<li>Every time to get the next biggest &#8211; get the biggest and fix the heap&nbsp; takes &#8212;-&gt; logn<br \/>\nHence, for kth biggest &#8211;&gt; klogn<br \/>\n2nd biggest&#8212;&gt;2logn<br \/>\n3rd biggest &#8212;&gt;3logn<br \/>\nMiddle one &#8212;&gt; (n\/2)logn<br \/>\n(we want this in linear time)<\/li>\n<\/ol>\n<p><strong>Note:<\/strong> Calculating the median is a special case of finding the kth biggest element.<\/p>\n<h6><strong>Method to calculate the median (or) to find the kth biggest element<\/strong><\/h6>\n<p>Fill the numbers in a 2D array with 5 rows and &#8216;n\/5&#8217; columns, where &#8216;n&#8217; is the number of elements in your list.<br \/>\nGoal is to find the middle element in each column.<\/p>\n<h6><strong>Steps:<\/strong><\/h6>\n<ol>\n<li>Find the middle element in each column.(it takes 6 comparisons for 5 elements).<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-191\" src=\"http:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-05-at-12.37.35-AM.png\" alt=\"\" width=\"3008\" height=\"1856\" srcset=\"https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-05-at-12.37.35-AM.png 3008w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-05-at-12.37.35-AM-300x185.png 300w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-05-at-12.37.35-AM-1024x632.png 1024w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-05-at-12.37.35-AM-768x474.png 768w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-05-at-12.37.35-AM-1536x948.png 1536w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-05-at-12.37.35-AM-2048x1264.png 2048w\" sizes=\"auto, (max-width: 3008px) 100vw, 3008px\" \/><\/li>\n<li>Partition the column such that smaller ones are above and larger ones are below the middle element.(constant number of moves since there are only 5 elements)<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-187\" src=\"http:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-04-at-5.38.15-PM.png\" alt=\"\" width=\"3342\" height=\"1864\" srcset=\"https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-04-at-5.38.15-PM.png 3342w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-04-at-5.38.15-PM-300x167.png 300w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-04-at-5.38.15-PM-1024x571.png 1024w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-04-at-5.38.15-PM-768x428.png 768w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-04-at-5.38.15-PM-1536x857.png 1536w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-04-at-5.38.15-PM-2048x1142.png 2048w\" sizes=\"auto, (max-width: 3342px) 100vw, 3342px\" \/><\/li>\n<li>Find the median of middle row and partition it left to right(as in Quick Sort) such that smaller elements are on left and bigger ones are on the right of the median.<br \/>\nMove the complete length 5 columns around appropriately after the partition.<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-196\" src=\"http:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-05-at-1.00.45-AM.png\" alt=\"\" width=\"3272\" height=\"1856\" srcset=\"https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-05-at-1.00.45-AM.png 3272w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-05-at-1.00.45-AM-300x170.png 300w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-05-at-1.00.45-AM-1024x581.png 1024w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-05-at-1.00.45-AM-768x436.png 768w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-05-at-1.00.45-AM-1536x871.png 1536w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-05-at-1.00.45-AM-2048x1162.png 2048w\" sizes=\"auto, (max-width: 3272px) 100vw, 3272px\" \/><\/li>\n<li>Go through each element whose relationship to the big median is unknown and place it in the correct set.<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-185\" style=\"font-size: 14px;\" src=\"http:\/\/www.datastructure.org\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-04-at-4.14.51-PM.png\" alt=\"\" width=\"3342\" height=\"1850\" srcset=\"https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-04-at-4.14.51-PM.png 3342w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-04-at-4.14.51-PM-300x166.png 300w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-04-at-4.14.51-PM-1024x567.png 1024w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-04-at-4.14.51-PM-768x425.png 768w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-04-at-4.14.51-PM-1536x850.png 1536w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/03\/Screen-Shot-2018-03-04-at-4.14.51-PM-2048x1134.png 2048w\" sizes=\"auto, (max-width: 3342px) 100vw, 3342px\" \/><\/li>\n<\/ol>\n<p>For example:<br \/>\nSay, set1 = 31 elements (smaller)<br \/>\nset 2 = 68 elements (larger)<br \/>\nset1&#8212;&#8212;median&#8212;&#8212;-set2<br \/>\n31&#8212;&#8212;&#8212;&#8211;(32)&#8212;&#8212;&#8212;&#8211;68<br \/>\n50th biggest &#8212;&gt; 50 &#8211; 32 = 18 &#8211;&gt; Find the 18th biggest from set2, gives 50th biggest in the original. (note that this is recursive)<\/p>\n<h6><strong>Analysis<br \/>\n<\/strong><\/h6>\n<p><strong>Step 1:<\/strong> Partition Columns<br \/>\n6 comparisons each column, multiply by (n\/5) columns<br \/>\nT(n) = 6(n\/5)<br \/>\n<strong>Step 2<\/strong>: partition the middle row on the median and move the length 5 columns around appropriately.<\/p>\n<ol>\n<li>Finding median &#8211;&gt; recursive call &#8211;&gt; T(n\/5)<\/li>\n<li>Move things around &#8211;&gt; partitioning &#8211;&gt; length of the array.<br \/>\nEvery time we swap, we swap 5 elements, length = (n\/5), swap = 5 for each &#8211;&gt; (C1) some constant<br \/>\nso, T(n) = 6(n\/5) + T(n\/5) + C1(n\/5)<\/li>\n<\/ol>\n<p><strong>Step 3:<\/strong> put unknowns to appropriate sets.<br \/>\nWorst case &#8211; (n\/2) i.e half of the elements may be unknowns.<br \/>\nso, T(n) =&nbsp;6(n\/5) + T(n\/5) + C1(n\/5) + (n\/2)<br \/>\n<strong>Step 4:<\/strong> Run the problem in set1 or set2 based on kth number(position).<br \/>\nExample: 31&#8212;-median(32)&#8212;&#8212;-68<br \/>\nIf we want 6th largest, run in set1.<br \/>\nWorst case =&nbsp; T(3n\/4)<br \/>\nmeaning, all unknown may fall into one set, making it 3\/4th of the whole space, and we have to run our algorithm on that whole space.<br \/>\nWhen,&nbsp; set1: set2 = 25 : 75 , and the kth biggest element falls in set2, and hence worst case is 3\/4th of the whole set.<br \/>\nTherefore, T(n) =&nbsp;6(n\/5) + T(n\/5) + C1(n\/5) + (n\/2)&nbsp; + T(3n\/4)<br \/>\nHence, <strong>T(n) &lt;= T((constant fraction) n)<\/strong><br \/>\nReference:&nbsp;<a href=\"https:\/\/www.youtube.com\/watch?v=1W3x0f_RmUo\" target=\"_blank\" rel=\"noopener noreferrer\">Algorithms &#8211; Searching &amp; Data Structures &#8211; Lecture 4<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Finding the Kth biggest using Heap Sort Sort it by getting the biggest, then the next biggest, then the next and so on. . . Every time to get the next biggest &#8211; get the biggest and fix the heap&nbsp; takes &#8212;-&gt; logn Hence, for kth biggest &#8211;&gt; klogn 2nd biggest&#8212;&gt;2logn 3rd biggest &#8212;&gt;3logn Middle &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-181","post","type-post","status-publish","format-standard","hentry","category-algorithms","entry"],"_links":{"self":[{"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/posts\/181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/comments?post=181"}],"version-history":[{"count":1,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/posts\/181\/revisions"}],"predecessor-version":[{"id":446,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/posts\/181\/revisions\/446"}],"wp:attachment":[{"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/media?parent=181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/categories?post=181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/tags?post=181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}