-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1862 from Dipanita45/kl
Merge K sorted array
- Loading branch information
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
Sorting Algorithms/Merge_k_Sorted_Lists/Merge k sorted lists.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
// Definition for singly-linked list. | ||
struct ListNode { | ||
int val; | ||
struct ListNode *next; | ||
}; | ||
|
||
// Min-Heap Node | ||
struct MinHeapNode { | ||
struct ListNode *listNode; | ||
}; | ||
|
||
// MinHeap | ||
struct MinHeap { | ||
int size; | ||
struct MinHeapNode **array; | ||
}; | ||
|
||
// Function to create a new MinHeap Node | ||
struct MinHeapNode* newMinHeapNode(struct ListNode *listNode) { | ||
struct MinHeapNode* minHeapNode = (struct MinHeapNode*)malloc(sizeof(struct MinHeapNode)); | ||
minHeapNode->listNode = listNode; | ||
return minHeapNode; | ||
} | ||
|
||
// Function to create a MinHeap | ||
struct MinHeap* createMinHeap(int capacity) { | ||
struct MinHeap* minHeap = (struct MinHeap*)malloc(sizeof(struct MinHeap)); | ||
minHeap->size = 0; | ||
minHeap->array= (struct MinHeapNode*)malloc(capacity * sizeof(struct MinHeapNode)); | ||
return minHeap; | ||
} | ||
|
||
// Function to swap two MinHeap Nodes | ||
void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b) { | ||
struct MinHeapNode* t = *a; | ||
*a = *b; | ||
*b = t; | ||
} | ||
|
||
// Function to min-heapify at a given index | ||
void minHeapify(struct MinHeap* minHeap, int idx) { | ||
int smallest = idx; | ||
int left = 2 * idx + 1; | ||
int right = 2 * idx + 2; | ||
|
||
if (left < minHeap->size && minHeap->array[left]->listNode->val < minHeap->array[smallest]->listNode->val) | ||
smallest = left; | ||
|
||
if (right < minHeap->size && minHeap->array[right]->listNode->val < minHeap->array[smallest]->listNode->val) | ||
smallest = right; | ||
|
||
if (smallest != idx) { | ||
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]); | ||
minHeapify(minHeap, smallest); | ||
} | ||
} | ||
|
||
// Function to extract the minimum node from the heap | ||
struct ListNode* extractMin(struct MinHeap* minHeap) { | ||
if (minHeap->size == 0) | ||
return NULL; | ||
|
||
struct ListNode* root = minHeap->array[0]->listNode; | ||
|
||
if (minHeap->size > 1) { | ||
minHeap->array[0] = minHeap->array[minHeap->size - 1]; | ||
minHeapify(minHeap, 0); | ||
} | ||
minHeap->size--; | ||
return root; | ||
} | ||
|
||
// Function to insert a new node into the heap | ||
void insertMinHeap(struct MinHeap* minHeap, struct MinHeapNode* minHeapNode) { | ||
minHeap->size++; | ||
int i = minHeap->size - 1; | ||
while (i && minHeapNode->listNode->val < minHeap->array[(i - 1) / 2]->listNode->val) { | ||
minHeap->array[i] = minHeap->array[(i - 1) / 2]; | ||
i = (i - 1) / 2; | ||
} | ||
minHeap->array[i] = minHeapNode; | ||
} | ||
|
||
// Function to merge k sorted linked |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
## Merge k Sorted Lists | ||
|
||
# Problem Description | ||
The problem of merging k sorted linked lists involves taking k linked lists, each sorted in ascending order, and merging them into a single sorted linked list. The challenge is to do this efficiently, both in terms of time and space. | ||
|
||
# Example | ||
Given the following k sorted linked lists: | ||
|
||
List 1: 1 -> 4 -> 5 | ||
List 2: 1 -> 3 -> 4 | ||
List 3: 2 -> 6 | ||
The merged sorted linked list should be: | ||
|
||
1 -> 1 -> 2 -> 3 -> 4 -> 4 -> 5 -> 6 | ||
|
||
# Solution Approach | ||
Min-Heap (Priority Queue): | ||
|
||
Use a min-heap (or priority queue) to efficiently retrieve the smallest element among the heads of the k lists. | ||
Push the head of each linked list into the min-heap. | ||
Repeatedly extract the minimum element from the heap, adding it to the merged list, and push the next element from the same linked list into the heap. | ||
Continue this process until all elements from all lists have been processed. | ||
Iterative Merging: | ||
|
||
Alternatively, you could merge the lists iteratively, but this is less efficient than using a min-heap. |