Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge K sorted array #1862

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions Sorting Algorithms/Merge_k_Sorted_Lists/Merge k sorted lists.c
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
25 changes: 25 additions & 0 deletions Sorting Algorithms/Merge_k_Sorted_Lists/Readme.md
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.
Loading