{"id":18087,"date":"2020-11-07T14:50:10","date_gmt":"2020-11-07T09:20:10","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/"},"modified":"2024-09-03T11:24:38","modified_gmt":"2024-09-03T05:54:38","slug":"heap-sort","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/","title":{"rendered":"Heap Sort Tutorial how to use this in C, C++, Java and Python"},"content":{"rendered":"\n<p>Heap Sort is one of the best sorting methods being in-place and with no quadratic worst-case running time. It is a comparison based sorting technique based on a Binary Heap data structure. In this article, we are going to cover all these subtopics:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-heap\"><strong>What is Heap<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is a data structure which is a complete binary tree<\/li>\n\n\n\n<li>All the levels are completely filled except the last level<\/li>\n\n\n\n<li>Heap has some order of values to be maintained between parents and their children<\/li>\n\n\n\n<li>There are 2 variations of heap possible\n<ul class=\"wp-block-list\">\n<li>MIN HEAP\n<ul class=\"wp-block-list\">\n<li>Here the value of parent is always less than the value of its children<\/li>\n\n\n\n<li>Hence root will be the minimum in the entire heap<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>MAX HEAP\n<ul class=\"wp-block-list\">\n<li>Here the value of parent is always more than the value of its children<\/li>\n\n\n\n<li>Hence root will be the maximum in the entire heap<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-heap-sort\"><strong>What is Heap Sort<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is one of the efficient sorting algorithm based on heap data structure<\/li>\n\n\n\n<li>Here the given array to be sorted is assumed to be a heap<\/li>\n\n\n\n<li>So for ith index\n<ul class=\"wp-block-list\">\n<li>The left child will become the element present at the 2*i+1 index in the array<\/li>\n\n\n\n<li>The right child will become the element present at the 2*i+2\u00a0 index in the array<\/li>\n\n\n\n<li>Parent of the ith index will be element present at (i-1)\/2 index in the array<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>There are 2 major operations which are responsible for maintaining the heap\n<ul class=\"wp-block-list\">\n<li>Heapify\n<ul class=\"wp-block-list\">\n<li>This operation sets the order of values between the parent and its child<\/li>\n\n\n\n<li>If we are dealing with the max heap, it will find the index having max value among the node and its children<\/li>\n\n\n\n<li>If the index holding max value is not the parent, it will wap the parent with the child having the max value<\/li>\n\n\n\n<li>Algo<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>heapify(arr , i)\n\tleftChild = arr &#91;2*0 + 1];\n\trightChild = arr &#91;2*0 + 2];\n    \tmaxIndex = max( arr&#91;i], leftChild, rightChild)\n    \tif(i != maxIndex)\n          \t\tswap(arr&#91;i], arr&#91;maxIndex])<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Build MAXHEAP or MINHEAP\n<ul class=\"wp-block-list\">\n<li>This is responsible for setting parent-child order in the entire heap<\/li>\n\n\n\n<li>This is the first function which is called to build the heap<\/li>\n\n\n\n<li>It starts from the last parent in the tree because that is the first instance where the order may get disturbed<\/li>\n\n\n\n<li>So it iterates from i=n\/2-1 to 0 and call heapify on every parent<\/li>\n\n\n\n<li>Algo<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>buildMaxHeap(arr)\n\tfor(int i = n \/ 2 - 1; i &gt;= 0; i--)\n     \t\t heapify(arr, i);\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In Heapsort, we deal with Maxheap.<\/li>\n\n\n\n<li>As we know root will have the max value possible in the heap, we will swap it with the last element in the heap and reduce the heap size by 1.<\/li>\n\n\n\n<li>Heap size is the variable that maintains the total number of elements to be considered in the heap<\/li>\n\n\n\n<li>Then we call downward heapify on the root. It starts from setting the relationship between the root n d its children. If either of the children was maximum then heapify is called on it.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-heapify\"><strong>What is Heapify<\/strong><\/h2>\n\n\n\n<p>The process of reshaping a binary tree into a Heap data structure is known as heapify. A binary tree is a tree data structure that has two child nodes at max. If a node's children nodes are heapified, then only heapify process can be applied over that node. A heap should always be a complete binary tree, i.e., except the bottom level of the tree, all the levels are completely filled. The bottom level is filled from left to right. Bottom-up order should be used to perform heapification.<\/p>\n\n\n\n<p><strong>Example of Max-Heapify:<\/strong><\/p>\n\n\n\n<p>Let's take an input array R=[11,22,25,5,14,17,2,18].<br>Step 1: To create a binary tree from the array:<\/p>\n\n\n<figure class=\"wp-block-image size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-65.png\"><img decoding=\"async\" width=\"325\" height=\"220\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-65.png\" alt=\"\" class=\"wp-image-29339\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-65.png 325w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-65-300x203.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-65-150x102.png 150w\" sizes=\"(max-width: 325px) 100vw, 325px\" \/><\/figure>\n\n\n\n<p>Step 2: Take a subtree at the lowest level and start checking if it follows the max-heap property or not:<\/p>\n\n\n<figure class=\"wp-block-image size-large td-caption-align-https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-66.png zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-66.png\"><img decoding=\"async\" width=\"1024\" height=\"342\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-66-1024x342.png\" alt=\"\" class=\"wp-image-29340\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-66-1024x342.png 1024w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-66-300x100.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-66-768x257.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-66-1536x514.png 1536w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-66-696x233.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-66-1068x357.png 1068w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-66-1256x420.png 1256w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-66-150x50.png 150w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-66.png 1684w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Step 3: Now, we can see that the subtree doesn't follow the max-heap property. The children node must contain a lesser value than its parent node. We swap the key values between the parent node and the children node to ensure that the tree follows the max-heap property.<br>Let's continue examining all the subtrees from the lowest level to the top level :<\/p>\n\n\n<figure class=\"wp-block-image size-large td-caption-align-https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-67.png zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-67.png\"><img decoding=\"async\" width=\"1024\" height=\"357\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-67-1024x357.png\" alt=\"\" class=\"wp-image-29341\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-67-1024x357.png 1024w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-67-300x104.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-67-768x267.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-67-1536x535.png 1536w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-67-696x242.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-67-1068x372.png 1068w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-67-1206x420.png 1206w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-67-150x52.png 150w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-67.png 1786w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Step 4: We don't need to change anything here as this subtree follows the max-heap property. Now, we look at the branches on the right side:<\/p>\n\n\n<figure class=\"wp-block-image size-large td-caption-align-https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-68.png zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-68.png\"><img decoding=\"async\" width=\"1024\" height=\"343\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-68-1024x343.png\" alt=\"\" class=\"wp-image-29342\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-68-1024x343.png 1024w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-68-300x101.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-68-768x258.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-68-1536x515.png 1536w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-68-696x233.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-68-1068x358.png 1068w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-68-1252x420.png 1252w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-68-150x50.png 150w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-68.png 1765w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Step 5: Again the subtree follows the max-heap property so, let's continue this process:<\/p>\n\n\n<figure class=\"wp-block-image size-large td-caption-align-https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-69.png zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-69.png\"><img decoding=\"async\" width=\"1024\" height=\"356\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-69-1024x356.png\" alt=\"\" class=\"wp-image-29343\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-69-1024x356.png 1024w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-69-300x104.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-69-768x267.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-69-1536x533.png 1536w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-69-696x242.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-69-1068x371.png 1068w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-69-1210x420.png 1210w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-69-150x52.png 150w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-69.png 1780w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Step 6: Here again, we can see that the root node's key value is not the largest among all the nodes in the tree. Hence, we swapped the root node's key values with the key value of its right children node to match the max-heap property.<br>Now, after the swap, we need to check the right subtree from the root node to see whether it follows the max-heap property or not:<\/p>\n\n\n<figure class=\"wp-block-image size-large td-caption-align-https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-70.png zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-70.png\"><img decoding=\"async\" width=\"1024\" height=\"340\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-70-1024x340.png\" alt=\"\" class=\"wp-image-29344\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-70-1024x340.png 1024w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-70-300x100.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-70-768x255.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-70-1536x510.png 1536w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-70-696x231.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-70-1068x355.png 1068w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-70-1265x420.png 1265w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-70-150x50.png 150w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-70.png 1675w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Step 7: Finally, we have to check the whole tree and see if it satisfies the max-heapify property, and then we'll get our final max-heap tree:<\/p>\n\n\n<figure class=\"wp-block-image size-large td-caption-align-https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-72.png zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-72.png\"><img decoding=\"async\" width=\"1024\" height=\"338\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-72-1024x338.png\" alt=\"\" class=\"wp-image-29346\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-72-1024x338.png 1024w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-72-300x99.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-72-768x253.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-72-1536x507.png 1536w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-72-696x230.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-72-1068x352.png 1068w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-72-1273x420.png 1273w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-72-150x49.png 150w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-72.png 1667w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heapsort-pseudo-code\"><strong>Heapsort Pseudo-code<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, call build max heap to set the heap initially<\/li>\n\n\n\n<li>Once the heap is created, take the root and wap it will the last element of the heap<\/li>\n\n\n\n<li>Reduce the size of the heap<\/li>\n\n\n\n<li>Call max heapify of index 0 i.e, the new root of the heap<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heapsort-algorithm\"><strong>Heapsort Algorithm<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Heapsort(arr)\n\tbuildMaxHeap(arr)\n\tfor (int i = n - 1; i &gt;= 0; i--) {\n      \t  swap(&amp;arr&#91;0], &amp;arr&#91;i]);\n\t  heapsize--;\n\t  maxHeapify(arr,0);\n  \t}\n\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heapsort-algorithm-dry-run\"><strong>Heapsort Algorithm Dry Run<\/strong><\/h2>\n\n\n\n<p>input:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><\/tr><tr><td>23<\/td><td>10<\/td><td>16<\/td><td>11<\/td><td>20<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The first step - call build max heap<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><\/tr><tr><td>23<\/td><td>20<\/td><td>16<\/td><td>11<\/td><td>10<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>For i=4<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><\/tr><tr><td>20<\/td><td>11<\/td><td>16<\/td><td>10<\/td><td>23<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>After i=3<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><\/tr><tr><td>16<\/td><td>11<\/td><td>10<\/td><td>20<\/td><td>23<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>After i=2<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><\/tr><tr><td>11<\/td><td>10<\/td><td>16<\/td><td>20<\/td><td>23<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>After i=1<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><\/tr><tr><td>10<\/td><td>11<\/td><td>16<\/td><td>20<\/td><td>23<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>After i=0<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><\/tr><tr><td>10<\/td><td>11<\/td><td>16<\/td><td>20<\/td><td>23<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heapsort-time-complexity\"><strong>Heapsort Time Complexity<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Build max heap takes O(n\/2) time<\/li>\n\n\n\n<li>We are calling for heapify inside the for loop, which may take the height of the heap in the worst case for all comparison. Therefore time complexity will become O(nlogn)<\/li>\n\n\n\n<li>Best Time Complexity: O(nlogn)<\/li>\n\n\n\n<li>Average Time Complexity: O(nlogn)<\/li>\n\n\n\n<li>Worst Time Complexity: O(nlogn)<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heapsort-space-complexity\"><strong>Heapsort Space Complexity<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>No auxiliary space is required in Heapsort implementation that is we are not using any arrays, linked list, stack, queue, etc to store our elements<\/li>\n\n\n\n<li>Hence space complexity is: O(1)<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"applications-of-heap-sort\"><strong>Applications of Heap Sort<\/strong>&nbsp;<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>K sorted array<\/li>\n\n\n\n<li>K largest or smallest elements in an array\u00a0<\/li>\n<\/ol>\n\n\n\n<p>As Quicksort and Merge sort are better in practice, the Heap sort algorithm has limited usage.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heapsort-in-c\"><strong>Heapsort in C&nbsp;<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Heap Sort Program in C | Heap Sort Algorithm | Heap Sort in Data Structure | Great Learning\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/ScxY9kG4ZDA?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n  \n  void swap(int *a, int *b) {\n    int tmp = *a;\n    *a = *b;\n    *b = tmp;\n  }\n  \n  void heapify(int arr&#91;], int n, int i) {\n    int max = i; \/\/Initialize max as root\n    int leftChild = 2 * i + 1;\n    int rightChild = 2 * i + 2;\n  \n    \/\/If left child is greater than root\n    if (leftChild &lt; n &amp;&amp; arr&#91;leftChild] &gt; arr&#91;max])\n      max = leftChild;\n  \n    \/\/If right child is greater than max\n    if (rightChild &lt; n &amp;&amp; arr&#91;rightChild] &gt; arr&#91;max])\n      max = rightChild;\n  \n    \/\/If max is not root\n    if (max != i) {\n      swap(&amp;arr&#91;i], &amp;arr&#91;max]);\n      \/\/heapify the affected sub-tree recursively\n      heapify(arr, n, max);\n    }\n  }\n\n  \/\/Main function to perform heap sort\n  void heapSort(int arr&#91;], int n) {\n    \/\/Rearrange array (building heap)\n    for (int i = n \/ 2 - 1; i &gt;= 0; i--)\n      heapify(arr, n, i);\n \n    \/\/Extract elements from heap one by one\n    for (int i = n - 1; i &gt;= 0; i--) {\n      swap(&amp;arr&#91;0], &amp;arr&#91;i]); \/\/Current root moved to the end\n  \n      heapify(arr, i, 0); \/\/calling max heapify on the heap reduced\n    }\n  }\n\n  \/\/print size of array n using utility function\n  void display(int arr&#91;], int n) {\n    for (int i = 0; i &lt; n; ++i)\n      printf(\"%d \", arr&#91;i]);\n    printf(\"n\");\n  }\n\n  \/\/Driver code\n  int main() {\n    int arr&#91;] = {11, 34, 9, 5, 16, 10};\n    int n = sizeof(arr) \/ sizeof(arr&#91;0]);\n\t\n    printf(\"Original array:n\");\n    display(arr, n);\n    heapSort(arr, n);\n  \n    printf(\"Sorted array:n\");\n    display(arr, n);\n  }\n\n \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output of the program:\n Original array:\n 11 34 9 5 16 10 \n Sorted array:\n 5 9 10 11 16 34 <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heapsort-in-java\"><strong>Heapsort in Java<\/strong><br><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>  public class HeapSort {\n  \n    public void sort(int arr&#91;]) {\n      int n = arr.length;\n  \n      \/\/Rearrange array (building heap)\n      for (int i = n \/ 2 - 1; i &gt;= 0; i--) {\n        heapify(arr, n, i);\n      }\n  \n      \/\/Extract elements from heap one by one\n      for (int i = n - 1; i &gt;= 0; i--) {\n        \/\/Current root moved to the end\n        int tmp = arr&#91;0];\n        arr&#91;0] = arr&#91;i];\n        arr&#91;i] = tmp;\n  \n        heapify(arr, i, 0);\/\/calling max heapify on the heap reduced\n      }\n    }\n  \n    void heapify(int arr&#91;], int n, int i) {\n      int max = i; \/\/Initialize max as root\n      int leftChild = 2 * i + 1;\n      int rightChild = 2 * i + 2;\n  \n      \/\/If left child is greater than root\n      if (leftChild &lt; n &amp;&amp; arr&#91;leftChild] &gt; arr&#91;max])\n        max = leftChild;\n  \n      \/\/If right child is greater than max\n      if (rightChild &lt; n &amp;&amp; arr&#91;rightChild] &gt; arr&#91;max])\n        max = rightChild;\n  \n      \/\/If max is not root\n      if (max != i) {\n        int swap = arr&#91;i];\n        arr&#91;i] = arr&#91;max];\n        arr&#91;max] = swap;\n\n        \/\/heapify the affected sub-tree recursively\n        heapify(arr, n, max);\n      }\n    }\n  \n    \/\/print size of array n using utility function\n    static void display(int arr&#91;]) {\n      int n = arr.length;\n      for (int i = 0; i &lt; n; ++i)\n        System.out.print(arr&#91;i] + \" \");\n      System.out.println();\n    }\n  \n    \/\/Driver code\n    public static void main(String args&#91;]) {\n      int arr&#91;] = {11, 34, 9, 5, 16, 10};\n  \n      HeapSort hs = new HeapSort();\n      System.out.println(\"Original array:\");\n      display(arr);\n\t  hs.sort(arr);\n  \n      System.out.println(\"Sorted array:\");\n      display(arr);\n    }\n  }\n\n\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output of the program:\n Original array:\n 11 34 9 5 16 10 \n Sorted array:\n 5 9 10 11 16 34 <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heapsort-in-c\"><strong>Heapsort in C++&nbsp;<\/strong><br><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>  \n  #include &lt;iostream&gt;\n  using namespace std;\n  \n  void heapify(int arr&#91;], int n, int i) {\n    int max = i; \/\/Initialize max as root\n    int leftChild = 2 * i + 1;\n    int rightChild = 2 * i + 2;\n  \n    \/\/If left child is greater than root\n    if (leftChild &lt; n &amp;&amp; arr&#91;leftChild] &gt; arr&#91;max])\n      max = leftChild;\n  \n    \/\/If right child is greater than max\n    if (rightChild &lt; n &amp;&amp; arr&#91;rightChild] &gt; arr&#91;max])\n      max = rightChild;\n  \n    \/\/If max is not root\n    if (max != i) {\n      swap(arr&#91;i], arr&#91;max]);\n      heapify(arr, n, max); \/\/heapify the affected sub-tree recursively\n    }\n  }\n  \n  \/\/Main function to perform heap sort\n  void heapSort(int arr&#91;], int n) {\n    \/\/Rearrange array (building heap)\n    for (int i = n \/ 2 - 1; i &gt;= 0; i--)\n      heapify(arr, n, i);\n  \n    \/\/Extract elements from heap one by one\n    for (int i = n - 1; i &gt;= 0; i--) {\n      swap(arr&#91;0], arr&#91;i]); \/\/Current root moved to the end\n  \n      heapify(arr, i, 0); \/\/calling max heapify on the heap reduced\n    }\n  }\n  \n  \/\/print size of array n using utility function\n  void display(int arr&#91;], int n) {\n    for (int i = 0; i &lt; n; ++i)\n      cout &lt;&lt; arr&#91;i] &lt;&lt; \" \";\n    cout &lt;&lt; \"n\";\n  }\n  \n  \/\/Driver code\n  int main() {\n    int arr&#91;] = {11, 34, 9, 5, 16, 10};\n    int n = sizeof(arr) \/ sizeof(arr&#91;0]);\n    cout &lt;&lt; \"Original array:n\";\n    display(arr, n);\n    heapSort(arr, n);\n  \n    cout &lt;&lt; \"Sorted array:n\";\n    display(arr, n);\n  }\n\n\n\n\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output of the program:\n Original array:\n 11 34 9 5 16 10 \n Sorted array:\n 5 9 10 11 16 34 <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heapsort-in-python\"><strong>Heapsort in Python<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>def heapify(arr, n, i): \n    largest = i #Initialize max as root\n    l = 2 * i + 1\n    r = 2 * i + 2\n\n    \/\/If left child is greater than root\n    if l &lt; n and arr&#91;i] &lt; arr&#91;l]:\n      largest = l\n    \n    \/\/If right child is greater than max\n    if r &lt; n and arr&#91;largest] &lt; arr&#91;r]:\n      largest = r\n\n    \/\/If max is not root\n    if largest != i:\n      arr&#91;i], arr&#91;largest] = arr&#91;largest], arr&#91;i]\n      heapify(arr, n, largest) \/\/heapify the root\n\n\/\/Main function to perform heap sort\ndef heapSort(arr):\n  n = len(arr)\n\n  #Build MaxHeap\n  for i in range(n \/\/2, -1, -1):\n    heapify(arr, n, i)\n\n  #Extract elements from heap one by one\n  for i in range(n - 1, 0, -1): \n    arr&#91;i], arr&#91;0] = arr&#91;0], arr&#91;i]\n    heapify(arr, i, 0)\n\n#Driver code\narr = &#91;11, 34, 9, 5, 16, 10] \nn = len(arr) \nprint(\"Original array:\") \nfor i in range(n):       \n    print(\"%d \" % arr&#91;i], end = '')\nheapSort(arr) \nprint(\"Sorted array:\") \nfor i in range(n):       \n    print(\"%d \" % arr&#91;i], end = '')\n\n\n\n\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output of the program:\n Original array:\n 11 34 9 5 16 10 \n Sorted array:\n 5 9 10 11 16 34 <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heapsort-example\"><strong>Heapsort Example<\/strong><\/h2>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Find the kth largest element in the array<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Input : 11 34 9 5 16 10<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">k= 3<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output: 11<\/pre>\n\n\n\n<p><strong>C Code<\/strong><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> #include &lt;stdio.h&gt;\n  \n  void swap(int *a, int *b) {\n    int tmp = *a;\n    *a = *b;\n    *b = tmp;\n  }\n  \n  void heapify(int arr&#91;], int n, int i) {\n    int max = i;\n    int leftChild = 2 * i + 1;\n    int rightChild = 2 * i + 2;\n  \n    if (leftChild &lt; n &amp;&amp; arr&#91;leftChild] &gt; arr&#91;max])\n      max = leftChild;\n  \n    if (rightChild &lt; n &amp;&amp; arr&#91;rightChild] &gt; arr&#91;max])\n      max = rightChild;\n  \n    if (max != i) {\n      swap(&amp;arr&#91;i], &amp;arr&#91;max]);\n      heapify(arr, n, max);\n    }\n  }\n  \n  void heapSort(int arr&#91;], int n, int k) {\n    for (int i = n \/ 2 - 1; i &gt;= 0; i--)\n      heapify(arr, n, i);\n  \n    for (int i = n - 1; i &gt; k; i--) {\n      swap(&amp;arr&#91;0], &amp;arr&#91;i]);\n  \n      heapify(arr, i, 0);\n    }\n    \n    printf(\"%d \",arr&#91;0]);\n  }\n  \n\n  \n  int main() {\n    int arr&#91;] = {11, 34, 9, 5, 16, 10};\n    int n = sizeof(arr) \/ sizeof(arr&#91;0]);\n\n    heapSort(arr, n, 3);\n  }\n<\/code><\/pre>\n\n\n\n<p><strong>Java Code<\/strong><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> public class HeapSort {\n  \n    public void sort(int arr&#91;], int k) {\n      int n = arr.length;\n  \n      for (int i = n \/ 2 - 1; i &gt;= 0; i--) {\n        heapify(arr, n, i);\n      }\n  \n      for (int i = n - 1; i &gt; k; i--) {\n        int tmp = arr&#91;0];\n        arr&#91;0] = arr&#91;i];\n        arr&#91;i] = tmp;\n  \n        heapify(arr, i, 0);\n      }\n      System.out.println(arr&#91;0]);\n    }\n  \n    void heapify(int arr&#91;], int n, int i) {\n      int max = i;\n      int leftChild = 2 * i + 1;\n      int rightChild = 2 * i + 2;\n  \n      if (leftChild &lt; n &amp;&amp; arr&#91;leftChild] &gt; arr&#91;max])\n        max = leftChild;\n  \n      if (rightChild &lt; n &amp;&amp; arr&#91;rightChild] &gt; arr&#91;max])\n        max = rightChild;\n  \n      if (max != i) {\n        int swap = arr&#91;i];\n        arr&#91;i] = arr&#91;max];\n        arr&#91;max] = swap;\n  \n        heapify(arr, n, max);\n      }\n    }\n  \n    static void display(int arr&#91;]) {\n      int n = arr.length;\n      for (int i = 0; i &lt; n; ++i)\n        System.out.print(arr&#91;i] + \" \");\n      System.out.println();\n    }\n  \n    public static void main(String args&#91;]) {\n      int arr&#91;] = {11, 34, 9, 5, 16, 10};\n  \n      HeapSort hs = new HeapSort();\n\n\t  hs.sort(arr,3);\n\n    }\n  }\n<\/code><\/pre>\n\n\n\n<p><strong>C++ Code<\/strong><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n  using namespace std;\n  \n  void heapify(int arr&#91;], int n, int i) {\n    int max = i;\n    int leftChild = 2 * i + 1;\n    int rightChild = 2 * i + 2;\n  \n    if (leftChild &lt; n &amp;&amp; arr&#91;leftChild] &gt; arr&#91;max])\n      max = leftChild;\n  \n    if (rightChild &lt; n &amp;&amp; arr&#91;rightChild] &gt; arr&#91;max])\n      max = rightChild;\n  \n    if (max != i) {\n      swap(arr&#91;i], arr&#91;max]);\n      heapify(arr, n, max);\n    }\n  }\n  \n  void heapSort(int arr&#91;], int n) {\n    for (int i = n \/ 2 - 1; i &gt;= 0; i--)\n      heapify(arr, n, i);\n  \n    for (int i = n - 1; i &gt;= 0; i--) {\n      swap(arr&#91;0], arr&#91;i]);\n  \n      heapify(arr, i, 0);\n    }\n  }\n  \n  void display(int arr&#91;], int n) {\n    for (int i = 0; i &lt; n; ++i)\n      cout &lt;&lt; arr&#91;i] &lt;&lt; \" \";\n    cout &lt;&lt; \"n\";\n  }\n  \n  int main() {\n    int arr&#91;] = {11, 34, 9, 5, 16, 10};\n    int n = sizeof(arr) \/ sizeof(arr&#91;0]);\n    cout &lt;&lt; \"Original array:n\";\n    display(arr, n);\n    heapSort(arr, n);\n  \n    cout &lt;&lt; \"Sorted array:n\";\n    display(arr, n);\n  }\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"difference-between-min-heap-max-heap\"><strong>Difference between Min Heap &amp; Max Heap<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sno.<\/strong><\/td><td><strong>Min Heap<\/strong><\/td><td><strong>Max Heap<\/strong><\/td><\/tr><tr><td>1.<\/td><td>The keys present at all of the children nodes should be greater than or equal to the key present at the root node. &nbsp;<\/td><td>The keys present at all the root nodes should be less than or equal to the key present at the root node.<\/td><\/tr><tr><td>2.<\/td><td>The key element present at the root node is the minimum key element. &nbsp;<\/td><td>The key element present at the root node is the maximum key element.<\/td><\/tr><tr><td>3.<\/td><td>Ascending priority is used in Min-Heap.<\/td><td>Descending priority is used in Max-Heap.<\/td><\/tr><tr><td>4.<\/td><td>The smallest element has the priority in the construction of a Min-Heap. &nbsp;<\/td><td>The largest element has the priority in the construction of a Max-Heap.<\/td><\/tr><tr><td>5.<\/td><td>The first element to be popped from the heap in a Min-Heap is the smallest element. &nbsp;<\/td><td>The first element to be popped from the heap in a Min-Heap is the smallest element.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-63.png\"><img decoding=\"async\" width=\"906\" height=\"419\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-63.png\" alt=\"\" class=\"wp-image-29333\" style=\"width:449px;height:199px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-63.png 906w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-63-300x139.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-63-768x355.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-63-696x322.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-63-150x69.png 150w\" sizes=\"(max-width: 906px) 100vw, 906px\" \/><\/figure>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-64.png\"><img decoding=\"async\" width=\"908\" height=\"412\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-64.png\" alt=\"\" class=\"wp-image-29334\" style=\"width:454px;height:198px\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-64.png 908w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-64-300x136.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-64-768x348.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-64-696x316.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/image-64-150x68.png 150w\" sizes=\"(max-width: 908px) 100vw, 908px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"which-is-better-mergesort-or-heapsort\"><strong>Which is better Mergesort or Heapsort<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>In terms of algorithm<\/strong>\n<ul class=\"wp-block-list\">\n<li>In <a aria-label=\"merge sort (opens in a new tab)\" href=\"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/\" target=\"_blank\" rel=\"noreferrer noopener\">merge sort<\/a>, in every iteration, we divide the problem into 2 almost equal subproblems. Once no more dividing is possible, we start merging upwards<\/li>\n\n\n\n<li>In heap sort, there are 2 major operations that basically aids heapsort that is heapify and build heap<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>In terms of time and space complexity<\/strong>\n<ul class=\"wp-block-list\">\n<li>Merge sort take n extra space<\/li>\n\n\n\n<li>Heap sort make all the changes in the input array itself hence space requirement is constant here<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><br><\/td><td><strong>Best<\/strong><\/td><td><strong>Average<\/strong><\/td><td><strong>Worst<\/strong><\/td><td><strong>Space<\/strong><\/td><\/tr><tr><td><strong>Heap<\/strong><\/td><td>O(nlogn)<\/td><td>O(nlogn)<\/td><td>O(nlogn)<\/td><td>O(1)<\/td><\/tr><tr><td><strong>Merge<\/strong><\/td><td>O(nlogn)<\/td><td>O(nlogn)<\/td><td>O(nlogn)<\/td><td>O(n)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>In terms of speed<\/strong>\n<ul class=\"wp-block-list\">\n<li>Heapsort is a bit slower than merge sort<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>In terms of in-place<\/strong>\n<ul class=\"wp-block-list\">\n<li>In-place states that the algorithm is in-place if it does not need extra memory barring some variable creation which counts to constant space.<\/li>\n\n\n\n<li>Heap sort does not require any auxiliary memory but merge sort is out place.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>In terms of stability<\/strong>\n<ul class=\"wp-block-list\">\n<li>Stability states that the algorithm is stable if the relative ordering of the same elements in the input and output array remains the same.<\/li>\n\n\n\n<li>Merge sort is stable algorithms but heap sort is not as swapping may cost stability.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>This brings us to the end of this article where we learned about heap sort. To get a free course on data structures and algorithms, click on the banner below. Also, visit the<strong><em> <a href=\"https:\/\/www.mygreatlearning.com\/academy\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"great learning academy (opens in a new tab)\">great learning academy<\/a><\/em><\/strong> to see all the free courses we are providing.<\/p>\n\n\n<figure class=\"wp-block-image size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-data-structures-1.png\"><a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/data-structures-and-algorithms-in-java\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1000\" height=\"242\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-data-structures-1.png\" alt=\"heap sort\" class=\"wp-image-16657\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-data-structures-1.png 1000w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-data-structures-1-300x73.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-data-structures-1-768x186.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-data-structures-1-696x168.png 696w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Heap Sort is one of the best sorting methods being in-place and with no quadratic worst-case running time. It is a comparison based sorting technique based on a Binary Heap data structure. In this article, we are going to cover all these subtopics: What is Heap What is Heap Sort What is Heapify The process [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":18202,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36858],"content_type":[],"class_list":["post-18087","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-sorting-algorithm"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Heap Sort Algorithm: C, C++, Java and Python Implementation | Great Learning<\/title>\n<meta name=\"description\" content=\"Heap sort Algorithm: is a comparison based sorting technique based on a Binary Heap data structure.In this article we will implement it i C,C++, java and Python\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Heap Sort Tutorial how to use this in C, C++, Java and Python\" \/>\n<meta property=\"og:description\" content=\"Heap sort Algorithm: is a comparison based sorting technique based on a Binary Heap data structure.In this article we will implement it i C,C++, java and Python\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-07T09:20:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-03T05:54:38+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Algorithm-23-7-2020-03.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"700\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/heap-sort\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/heap-sort\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Heap Sort Tutorial how to use this in C, C++, Java and Python\",\"datePublished\":\"2020-11-07T09:20:10+00:00\",\"dateModified\":\"2024-09-03T05:54:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/heap-sort\\\/\"},\"wordCount\":1415,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/heap-sort\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/Blog-Algorithm-23-7-2020-03.jpg\",\"keywords\":[\"sorting algorithm\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/heap-sort\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/heap-sort\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/heap-sort\\\/\",\"name\":\"Heap Sort Algorithm: C, C++, Java and Python Implementation | Great Learning\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/heap-sort\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/heap-sort\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/Blog-Algorithm-23-7-2020-03.jpg\",\"datePublished\":\"2020-11-07T09:20:10+00:00\",\"dateModified\":\"2024-09-03T05:54:38+00:00\",\"description\":\"Heap sort Algorithm: is a comparison based sorting technique based on a Binary Heap data structure.In this article we will implement it i C,C++, java and Python\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/heap-sort\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/heap-sort\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/heap-sort\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/Blog-Algorithm-23-7-2020-03.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/Blog-Algorithm-23-7-2020-03.jpg\",\"width\":1000,\"height\":700,\"caption\":\"HeapSort_GL\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/heap-sort\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Heap Sort Tutorial how to use this in C, C++, Java and Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Heap Sort Algorithm: C, C++, Java and Python Implementation | Great Learning","description":"Heap sort Algorithm: is a comparison based sorting technique based on a Binary Heap data structure.In this article we will implement it i C,C++, java and Python","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/","og_locale":"en_US","og_type":"article","og_title":"Heap Sort Tutorial how to use this in C, C++, Java and Python","og_description":"Heap sort Algorithm: is a comparison based sorting technique based on a Binary Heap data structure.In this article we will implement it i C,C++, java and Python","og_url":"https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2020-11-07T09:20:10+00:00","article_modified_time":"2024-09-03T05:54:38+00:00","og_image":[{"width":1000,"height":700,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Algorithm-23-7-2020-03.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Heap Sort Tutorial how to use this in C, C++, Java and Python","datePublished":"2020-11-07T09:20:10+00:00","dateModified":"2024-09-03T05:54:38+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/"},"wordCount":1415,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Algorithm-23-7-2020-03.jpg","keywords":["sorting algorithm"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/","url":"https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/","name":"Heap Sort Algorithm: C, C++, Java and Python Implementation | Great Learning","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Algorithm-23-7-2020-03.jpg","datePublished":"2020-11-07T09:20:10+00:00","dateModified":"2024-09-03T05:54:38+00:00","description":"Heap sort Algorithm: is a comparison based sorting technique based on a Binary Heap data structure.In this article we will implement it i C,C++, java and Python","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Algorithm-23-7-2020-03.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Algorithm-23-7-2020-03.jpg","width":1000,"height":700,"caption":"HeapSort_GL"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/heap-sort\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Heap Sort Tutorial how to use this in C, C++, Java and Python"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Algorithm-23-7-2020-03.jpg",1000,700,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Algorithm-23-7-2020-03-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Algorithm-23-7-2020-03-300x210.jpg",300,210,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Algorithm-23-7-2020-03-768x538.jpg",768,538,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Algorithm-23-7-2020-03.jpg",1000,700,false],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Algorithm-23-7-2020-03.jpg",1000,700,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Algorithm-23-7-2020-03.jpg",1000,700,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Algorithm-23-7-2020-03.jpg",640,448,false],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Algorithm-23-7-2020-03.jpg",96,67,false],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Algorithm-23-7-2020-03.jpg",150,105,false]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"Heap Sort is one of the best sorting methods being in-place and with no quadratic worst-case running time. It is a comparison based sorting technique based on a Binary Heap data structure. In this article, we are going to cover all these subtopics: What is Heap What is Heap Sort What is Heapify The process&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/18087","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=18087"}],"version-history":[{"count":25,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/18087\/revisions"}],"predecessor-version":[{"id":104932,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/18087\/revisions\/104932"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/18202"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=18087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=18087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=18087"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=18087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}