Skip to main content

Heap Sort


Heap sort is a comparison-based sorting algorithm that sorts an array by creating a binary heap data structure. A binary heap can either be a min heap or a max heap, where the parent node is either smaller or larger than its child nodes, respectively.

The algorithm starts by creating a binary heap from the given unsorted array. The process of converting an array into a binary heap is called heapifying. The max element is then removed from the root node and placed at the end of the array, effectively reducing the size of the heap. This process continues until the heap is empty, resulting in a sorted array in ascending order.

Heap sort has a time complexity of O(n log n), making it efficient for large arrays. However, its worst-case space complexity is O(n), as it requires additional memory to store the binary heap.

Heap sort is an in-place sorting algorithm, meaning that it sorts the array in place, without using any additional memory. This is an advantage over other sorting algorithms, such as merge sort, which require additional memory proportional to the size of the array.

Heap sort has several applications in computer science, including priority queue implementations, sorting algorithms for external memory, and sorting algorithms for databases.

Implementing heap sort can be done in two steps. The first step is to create a binary heap from the given array, which can be done using a bottom-up approach or a top-down approach. The second step is to repeatedly remove the max element from the root node and place it at the end of the array, and then reheapify the binary heap.

In conclusion, heap sort is an efficient and in-place sorting algorithm with a time complexity of O(n log n). It can be used for various applications, including priority queue implementations and sorting algorithms for external memory and databases.

ALGORITHM:

Heap sort is a comparison-based sorting algorithm that sorts an array by using a binary heap data structure. The steps to perform heap sort are:

1. Build a max heap from the input data. A max heap is a complete binary tree where the value of each parent node is greater than or equal to its children.

2. Swap the root node (the maximum value) with the last node in the heap.

3. Discard the last node from the heap and reheapify the root node to maintain the max heap property.

4. Repeat steps 2 and 3 until all nodes have been removed from the heap and added to the sorted array.

The time complexity of heap sort is O(n log n) in the average and worst case, making it more efficient than selection sort and bubble sort, but slower than quick sort.


PSEUDO CODE:

Here is an example of the pseudocode for the heap sort algorithm:



PYTHON CODE:

Here's a Python implementation of the heap sort algorithm:


  • heapSort is the main function that performs the heap sort algorithm. It first calls the heapify function to build a max heap from the input data.
  • heapify is a recursive function that rearranges the elements in the heap to maintain the max heap property.
  • The for loop in the heapSort function performs the steps 2 and 3 of the heap sort algorithm. In each iteration, it swaps the root node with the last node in the heap and calls the heapify function to reheapify the root node.
  • The time complexity of heap sort is O(n log n) in the average and worst case, making it more efficient than selection sort and bubble sort, but slower than quick sort.


JAVA CODE:

Here is a Java implementation of the heap sort algorithm:


  • sort is the main function that performs the heap sort algorithm. It first calls the heapify function to build a max heap from the input data.
  • heapify is a recursive function that rearranges the elements in the heap to maintain the max heap property.
  • The for loop in the sort function performs the steps 2.


C CODE:

Here is a C implementation of the heap sort algorithm:


  • heapify is a recursive function that rearranges the elements in the heap to maintain the max heap property.
  • heapSort is the main function that performs the heap sort algorithm.

C++ CODE:

Here is a C++ implementation of the heap sort algorithm:


  • heapify is a recursive function that rearranges the elements in the heap to maintain the max heap property.
  • heapSort is the main function that performs the heap sort algorithm.
  • swap is a standard C++ function that swaps the values of two variables.


JAVASCRIPT CODE:

Here is a JavaScript implementation of the heap sort algorithm:


  • heapify is a recursive function that rearranges the elements in the heap to maintain the max heap property.
  • heapSort is the main function that performs the heap sort algorithm.
  • [arr[i], arr[largest]] = [arr[largest], arr[i]] is destructuring assignment that swaps the values of two variables.

Comments

Popular posts from this blog

Leetcode 1: Two Sum

  Leetcode Link: https://leetcode.com/problems/two-sum/ 1. Problem Statement (Simple Explanation) You are given: An array of integers nums An integer target You need to  return the indices of two numbers in the array  such that: Conditions: Each input has  exactly one solution . You  cannot use the same element twice . You can return the answer in  any order . 2. Examples Example 1 Input:                                    nums = [2, 7, 11, 15], target = 9 Output:                                 [0, 1] Explanation:                     nums[0]+nums[1]=2+7=9              So we return [0, 1]. Example 2 Input:           ...

Leetcode 3: Longest Substring Without Repeating Characters

  Leetcode Link 1. Problem Statement (Simple Explanation) You are given a string s. You need to find the length of the longest substring of s that does not  contain any repeated characters. A substring is a contiguous sequence of characters in the string. You must return an integer length, not the substring itself. 2. Examples Example 1 Input: s = "abcabcbb" Output: 3 Explanation: Some substrings without repeating characters: "abc", "bca", "cab"The longest length is 3. Example 2 Input: s = "bbbbb" Output: 1 Explanation: All substrings that are valid are just "b". The longest length is 1. Example 3 Input:   s = "pwwkew" Output:   3 Explanation: Possible substrings without repetition: "pw", "wke", "kew". "wke" or "kew" have length 3. "pwke" is not valid because it’s not contiguous in the original string (p and k are separated by ww). Constraints...

Leetcode 2: Add Two Numbers

Leetcode Link: https://leetcode.com/problems/add-two-numbers/ 1. Problem Statement (Simple Explanation) You are given  two non-empty linked lists , l1 and l2, that represent two non-negative integers. Each node contains a  single digit . The digits are stored in  reverse order  (i.e., the 1’s digit is at the head). You need to  add the two numbers  and return the  sum as a linked list , also in reverse order. You can assume: There are  no leading zeros , except when the number itself is 0. 2. Examples Example 1 Input:                l1 = [2,4,3]                l2 = [5,6,4]           Interpreting linked lists as numbers (reverse order): l1 represents 342 l2 represents 465 Output:                [7,0,8] Explanation:        ...