diff --git a/Sorting Algorithms/Merge_k_Sorted_Lists/Merge k sorted lists.c b/Sorting Algorithms/Merge_k_Sorted_Lists/Merge k sorted lists.c new file mode 100644 index 00000000..4d7ea611 --- /dev/null +++ b/Sorting Algorithms/Merge_k_Sorted_Lists/Merge k sorted lists.c @@ -0,0 +1,87 @@ +#include +#include + +// 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 \ No newline at end of file diff --git a/Sorting Algorithms/Merge_k_Sorted_Lists/Readme.md b/Sorting Algorithms/Merge_k_Sorted_Lists/Readme.md new file mode 100644 index 00000000..d23c5cbf --- /dev/null +++ b/Sorting Algorithms/Merge_k_Sorted_Lists/Readme.md @@ -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. \ No newline at end of file