{"id":786,"date":"2017-03-27T06:01:30","date_gmt":"2017-03-27T14:01:30","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=786"},"modified":"2017-03-27T11:10:31","modified_gmt":"2017-03-27T19:10:31","slug":"find-kth-largest-element-in-an-unsorted-array","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=786","title":{"rendered":"Find kth largest element in an unsorted array"},"content":{"rendered":"<p>Method 1 (Simple Solution)<br \/>\nA Simple Solution is to sort the given array using a O(nlogn) sorting algorithm like Merge Sort, Heap Sort, etc and return the element at index k-1 in the sorted array. Time Complexity of this solution is O(nLogn).<\/p>\n<p>Method 2 (QuickSelect)<br \/>\nUsing technique similar to quicksort.<br \/>\nComplexity is O(n^2) but avg case is O(n)<\/p>\n<p>Method 3 (Using Min Heap \u2013 HeapSelect)<br \/>\nWe can find k\u2019th smallest element in time complexity better than O(nLogn). A simple optomization is to create a Min Heap of the given n elements and call extractMin() k times.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Method 1 (Simple Solution) A Simple Solution is to sort the given array using a O(nlogn) sorting algorithm like Merge Sort, Heap Sort, etc and return the element at index k-1 in the sorted array. Time Complexity of this solution is O(nLogn). Method 2 (QuickSelect) Using technique similar to quicksort. Complexity is O(n^2) but avg &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=786\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Find kth largest element in an unsorted array<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-786","post","type-post","status-publish","format-standard","hentry","category-algorithms"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=786"}],"version-history":[{"count":3,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/786\/revisions"}],"predecessor-version":[{"id":789,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/786\/revisions\/789"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=786"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}