Open In App

Benefits of Heap over Sorted Arrays

Array:

An array is a collection of similar data types which is stored in contiguous memory locations. Arrays are static data structures with limited size. The elements stored in the arrays are accessed by their unique indices. The array combines the data of similar types.

When the elements within the array are sorted in an order then the array is called a sorted array. There can be two sorting orders: Ascending and Descending. 



SORTED ARRAY

Heap:

Heap is a special tree data structure that follows the heap property. Heap is constructed using the complete or almost complete binary trees. Heaps are of two types: Max Heap and Min Heap 

Max Heap: The parent node must be greater than the child node. If A is the parent of B and C then A should be greater than both B and C.



MAX HEAP

Min Heap: The parent node must be lesser than the child node. If A is the parent of B and C then A should be lesser than both B and C.

MIN HEAP

Benefits of Heap over Sorted arrays:

Data structure  Insert   Search  Find min  Delete min
Sorted array  O(n) O(log n) O(1)     O(n)
Min heap O(log n)  O(n)  O(1)    O(log n)

Related article: Differences between Heap and Sorted Arrays

Article Tags :