Merge two sorted arrays in Python using heapq
Last Updated :
04 Mar, 2023
Given two sorted arrays, the task is to merge them in a sorted manner. Examples:
Input : arr1 = [1, 3, 4, 5]
arr2 = [2, 4, 6, 8]
Output : arr3 = [1, 2, 3, 4, 4, 5, 6, 8]
Input : arr1 = [5, 8, 9]
arr2 = [4, 7, 8]
Output : arr3 = [4, 5, 7, 8, 8, 9]
This problem has existing solution please refer Merge two sorted arrays link. We will solve this problem in python using heapq.merge() in a single line of code.
Implementation:
Python3
# Function to merge two sorted arrays
from heapq import merge
def mergeArray(arr1,arr2):
return list(merge(arr1, arr2))
# Driver function
if __name__ == "__main__":
arr1 = [1,3,4,5]
arr2 = [2,4,6,8]
print (mergeArray(arr1, arr2))
Output[1, 2, 3, 4, 4, 5, 6, 8]
The time complexity is O(n log n).
The space complexity is O(n).
Properties of heapq module ?
This module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm. To create a heap, use a list initialized to [], or you can transform a populated list into a heap via function heapify().The following functions are provided:
- heapq.heappush(heap,item) : Push the value item onto the heap, maintaining the heap invariant.
- heapq.heappop(heap) : Pop and return the smallest item from the heap, maintaining the heap invariant. If the heap is empty, IndexError is raised. To access the smallest item without popping it, use heap[0].
- heapq.heappushpop(heap, item) : Push item on the heap, then pop and return the smallest item from the heap. The combined action runs more efficiently than heappush() followed by a separate call to heappop().
- heapq.heapify(x) : Transform list x into a heap, in-place, in linear time.
- heapq.merge(*iterables) : Merge multiple sorted inputs into a single sorted output (for example, merge timestamped entries from multiple log files). Returns an iterator over the sorted values.
Similar Reads
Sorted merge in one array Given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B. Merge B into A in sorted order. Examples: Input : a[] = {10, 12, 13, 14, 18, NA, NA, NA, NA, NA} b[] = {16, 17, 19, 20, 22};; Output : a[] = {10, 12, 13, 14, 16, 17, 18, 19, 20, 22} One way is to merge the two
7 min read
Merge two sorted arrays Given two sorted arrays, the task is to merge them in a sorted manner.Examples: Input: arr1[] = { 1, 3, 4, 5}, arr2[] = {2, 4, 6, 8} Output: arr3[] = {1, 2, 3, 4, 4, 5, 6, 8}Input: arr1[] = { 5, 8, 9}, arr2[] = {4, 7, 8} Output: arr3[] = {4, 5, 7, 8, 8, 9} Table of Content[Naive Approach] Concatenat
10 min read
K-ary Heap using Python Ak-ary heap is also called n-ary or d-ary heap. It is a generalized form of the binary heap structure where each node has up to K children, unlike the binary heap structure with only 2 children. Because of this feature, the k-ary heap is a flexible and efficient data structure for priority queue imp
11 min read
Creating a Sorted Merged List of Two Unsorted Lists in Python Creating a sorted merged list of two unsorted lists involves combining the elements of both lists and sorting the resulting list in ascending order. For example: If we have list1 = [25, 18, 9] and list2 = [45, 3, 32] the output will be [3, 9, 18, 25, 32, 45].Using + OperatorThis method merges the tw
2 min read
How to get the indices of the sorted array using NumPy in Python? We can get the indices of the sorted elements of a given array with the help of argsort() method. This function is used to perform an indirect sort along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as arr that would sort the arra
2 min read
Javascript Program to Merge 3 Sorted Arrays Given 3 arrays (A, B, C) which are sorted in ascending order, we are required to merge them together in ascending order and output the array D. Examples: Input : A = [1, 2, 3, 4, 5] B = [2, 3, 4] C = [4, 5, 6, 7] Output : D = [1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6, 7] Input : A = [1, 2, 3, 5] B = [6, 7, 8
8 min read