# Sort numbers stored on different machines

Last Updated : 29 Oct, 2023

Given N machines. Each machine contains some numbers in sorted form. But the amount of numbers, each machine has is not fixed. Output the numbers from all the machine in sorted non-decreasing form.
Example:

Machine M1 contains 3 numbers: {30, 40, 50}
Machine M2 contains 2 numbers: {35, 45}
Machine M3 contains 5 numbers: {10, 60, 70, 80, 100}
Output: {10, 30, 35, 40, 45, 50, 60, 70, 80, 100}

Representation of stream of numbers on each machine is considered as a linked list. A Min Heap can be used to print all numbers in sorted order. Following is the detailed process

1. Store the head pointers of the linked lists in a minHeap of size N where N is a number of machines.
2. Extract the minimum item from the minHeap. Update the minHeap by replacing the head of the minHeap with the next number from the linked list or by replacing the head of the minHeap with the last number in the minHeap followed by decreasing the size of heap by 1.
3. Repeat the above step 2 until heap is not empty. Below is C++ implementation of the above approach.

Implementation:

## Javascript

Output

10 30 35 40 45 50 60 70 80 100

Time complexity: O(N) for min heap
Auxiliary Space: O(N)

Approach : Using DP

This code uses a priority queue (min heap) to efficiently merge the sorted lists from different machines. The mergeLists function takes a vector of linked lists and merges them into a single sorted linked list using a min heap. The externalSort function converts the array of linked lists into a vector and then calls mergeLists to obtain the sorted list, which is then printed.

Note that this implementation assumes ascending order sorting. If you want to sort in descending order, you can modify the CompareNode struct and change the comparison operator in the operator() function accordingly.

## C++

 #include #include #include   using namespace std;   struct ListNode {     int data;     ListNode* next; };   struct CompareNode {     bool operator()(const ListNode* a, const ListNode* b) {         return a->data > b->data;     } };   ListNode* createNode(int data) {     ListNode* newNode = new ListNode;     newNode->data = data;     newNode->next = nullptr;     return newNode; }   void push(ListNode** head, int data) {     if (*head == nullptr) {         *head = createNode(data);     } else {         ListNode* newNode = createNode(data);         newNode->next = *head;         *head = newNode;     } }   void printList(ListNode* head) {     while (head != nullptr) {         cout << head->data << " ";         head = head->next;     } }   ListNode* mergeLists(vector& lists) {     ListNode* dummy = createNode(0);     ListNode* tail = dummy;       priority_queue, CompareNode> minHeap;       for (ListNode* list : lists) {         if (list != nullptr) {             minHeap.push(list);         }     }       while (!minHeap.empty()) {         ListNode* node = minHeap.top();         minHeap.pop();           tail->next = node;         tail = tail->next;           if (node->next != nullptr) {             minHeap.push(node->next);         }     }       return dummy->next; }   void externalSort(ListNode* array[], int N) {     vector lists(array, array + N);     ListNode* sortedList = mergeLists(lists);     printList(sortedList); }   int main() {     int N = 3; // Number of machines       ListNode* array[N];       array[0] = nullptr;     push(&array[0], 50);     push(&array[0], 40);     push(&array[0], 30);       array[1] = nullptr;     push(&array[1], 45);     push(&array[1], 35);       array[2] = nullptr;     push(&array[2], 100);     push(&array[2], 80);     push(&array[2], 70);     push(&array[2], 60);     push(&array[2], 10);       externalSort(array, N);       return 0; }

## Javascript

Output:

10 30 35 40 45 50 60 70 80 100

space complexity: O(N + K)

time complexity: O(N + K log N)

Previous
Next