Sort numbers stored on different machines


In today’s world with a large amount of data and interconnected systems, a vast amount of data is created and stored across various machines. One challenging challenge is to sort this data stored across multiple devices. Sorting being a fundamental operation in computations is used for optimal retrieval, search and analysis of data. But with distributed systems and various interconnected machines, this task of sorting becomes difficult and important.

Problem Statement

Given an array containing N linked lists that depict N different machines. Each of these linked lists contains some variable number of numbers in sorted order. The task is to return all the numbers stored on these N machines in increasing order.

Sample Example 1

Input

Machine 1: 1, 4, 10
Machine 2: 2, 7, 8, 9

Output

1, 2, 4, 7, 8, 9, 10

Explanation

The minimum number among the machines is 1 then followed by the next greatest number to it. In the end, we get 10 as the highest number.

Sample Example 2

Input

Machine 1: 1, 5
Machine 2: 2, 7, 8, 9
Machine 3: 3, 4, 12
Machine 4: 0, 6, 15

Output

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 15

Explanation

The minimum number among the machines is 0 then followed by the next greatest number to it. In the end, we get 15 as the highest number.

Solution: Minimum Heap

A minimum heap can be used to get the sort numbers stored on different machines. Take a min heap and insert only the head of the linked list first and extract the minimum element. After getting the minimum element, push the next node of the node removed. Repeat teh above steps to get a sorted order of numbers.

Pseudocode

define a structure named node:
   data: integer
   next: node pointer
define a structure named compare:
   define a function operator() that takes two node pointers a and b:
      return a->data > b->data
define a function named new_node that takes an integer data:
   node = create a new node
   node.data = data
   node.next = null
   return node
define a function named push that takes a node pointer head and an integer data:
   if head is null:
      head = new_node(data)
   else:
      node = new_node(data)
      node.next = head
      head = node
define a function named sort_on_diff_machines that takes an array of node pointers arr and an integer N:
   create a vector of node pointers named lists from arr[0] to arr[N-1]
   ptr = new_node(0)
   q = ptr
   create a priority queue named min_heap with compare comparator
   for each node pointer list in lists:
      if list is not null:
         push list into min_heap
   while min_heap is not empty:
      node = pop the top node from min_heap
      connect q's next to node
      q = q's next
      if node's next is not null:
         push node's next into min_heap
   res = ptr's next
   while res is not null:
      print res's data
      move res to res's next

Example: C++ Implementation

The following code sorts number stored on different machines.

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

// Structure of node of linked list
struct Node {
   int data;
   Node *next;
};
// Comparator used by priority queue
struct Compare {
   bool operator()(const Node *a, const Node *b) {
      return a->data > b->data;
   }
};
// Helper function to create a new node in linked list
Node *newNode(int data) {
   Node *node = new Node;
   node->data = data;
   node->next = nullptr;
   return node;
}
// Helper function to create array of linked list by adding numbers
void push(Node **head, int data) {
   if (*head == nullptr) {
      *head = newNode(data);
   }
   else {
      Node *node = newNode(data);
      node->next = *head;
      *head = node;
   }
}
// Function to sort number son various machines
void sortOnDiffMachines(Node *arr[], int N) {
   vector<Node *> lists(arr, arr + N);
   Node *ptr = newNode(0);
   Node *q = ptr;
   priority_queue<Node *, vector<Node *>, Compare> minHeap;
   for (Node *list : lists) {
      if (list != nullptr) {
         minHeap.push(list);
      }
   }
   while (!minHeap.empty()) {
      Node *node = minHeap.top();
      minHeap.pop();
      q->next = node;
      q = q->next;
      if (node->next != nullptr) {
         minHeap.push(node->next);
      }
   }
   Node *res = ptr->next;
   while (res != nullptr) {
      cout << res->data << " ";
      res = res->next;
   }
}
int main() {
   int N = 2;
   Node *arr[N];
   arr[0] = nullptr;
   push(&arr[0], 10);
   push(&arr[0], 4);
   push(&arr[0], 1);
   arr[1] = nullptr;
   push(&arr[1], 9);
   push(&arr[1], 8);
   push(&arr[1], 7);
   push(&arr[1], 2);
   sortOnDiffMachines(arr, N);
   return 0;
}

Output

1 2 4 7 8 9 10

Time Complexity − O(N * logN) where N is the number of machines.

Space Complexity − O(1N)

Conclusion

In conclusion, the given solution efficiently solves the problem to sort the number stored on different machines. By using various data structures like linked lists and priority queues, the solution merges the different lists and sorts them. The provided solution provides a time complexity of O(N * logN) and a space complexity of O(N).

Updated on: 03-Nov-2023

368 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements