Advertisement
kartikkukreja

Nuts and Bolts Problem

Oct 29th, 2013
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.76 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7. class Bolt; // forward declaration of class Bolt
  8.  
  9. class Nut {
  10.     int size;
  11. public:
  12.     Nut (int size) {
  13.         this->size = size;
  14.     }
  15.     void to_str(char* str) {
  16.         sprintf(str, "NUT(%d)", size);
  17.     }
  18.     // comparators are implemented as friend functions so that
  19.     // they have access to the private member 'size', which is
  20.     // otherwise inaccessible
  21.  
  22.     // A Nut can be compared against a Bolt and vice-versa
  23.     // but a Nut cannot be compared against another Nut
  24.     // and a Bolt cannot be compared against another Bolt
  25.     friend int ::compare (const Nut*, const Bolt*);
  26.     friend int ::compare (const Bolt*, const Nut*);
  27. };
  28.  
  29. class Bolt {
  30.     int size;
  31. public:
  32.     Bolt (int size) {
  33.         this->size = size;
  34.     }
  35.     void to_str(char* str) {
  36.         sprintf(str, "BOLT(%d)", size);
  37.     }
  38.     friend int ::compare (const Nut*, const Bolt*);
  39.     friend int ::compare (const Bolt*, const Nut*);
  40. };
  41.  
  42. // Compare a bolt with a nut
  43. // Returns -1 if the bolt is smaller than the nut,
  44. // 0 if they are the same size, 1 if the bolt is larger
  45. int compare (const Bolt* a, const Nut* b) {
  46.     if (a->size < b->size)
  47.         return -1;
  48.     else if (a->size == b->size)
  49.         return 0;
  50.     return 1;
  51. }
  52.  
  53. // Compare a nut with a bolt
  54. // Returns -1 if the nut is smaller than the bolt,
  55. // 0 if they are the same size, 1 if the nut is larger
  56. int compare (const Nut* a, const Bolt* b) {
  57.     if (a->size < b->size)
  58.         return -1;
  59.     else if (a->size == b->size)
  60.         return 0;
  61.     return 1;
  62. }
  63.  
  64. // partition A[lo...hi] by comparing with B
  65. // creating a template avoids duplicating the code
  66. template <typename U, typename V>
  67. int partition(U** A, int lo, int hi, V* B)  {
  68.     int i, j, cmp;
  69.     U* temp;
  70.     // partition A[lo...hi-1] into A[lo...x] and A[x+1...hi-1]
  71.     // such that A[lo...x] has all elements smaller than B
  72.     // and A[x+1...hi-1] has all elements larger than B
  73.     // After the loop ends, A[hi] will match B
  74.     for (i = j = lo; j < hi; j++)   {
  75.         cmp = compare(A[j], B);
  76.         if (cmp < 0)    {
  77.             temp = A[i]; A[i] = A[j]; A[j] = temp;
  78.             i++;
  79.         } else if (cmp == 0)    {
  80.             temp = A[j]; A[j] = A[hi]; A[hi] = temp;
  81.             j--;
  82.         }
  83.     }
  84.     // swap A[i] with A[hi]
  85.     temp = A[i]; A[i] = A[hi]; A[hi] = temp;
  86.     return i;
  87. }
  88.  
  89. void sort(Nut** nuts, Bolt** bolts, int lo, int hi) {
  90.     if (lo >= hi)
  91.         return;
  92.     // choose a random nut to partition the bolts in the interval [lo...hi]
  93.     int choice = lo + (rand() % (hi - lo + 1));
  94.     Nut *pNut = nuts[choice];
  95.     // partition the bolts[lo...hi] according to the chosen nut
  96.     int pivot = partition(bolts, lo, hi, pNut);
  97.     // partition the nuts in the interval [lo, hi] according to the bolt at position pivot
  98.     // the matching nut will end up at position pivot
  99.     partition(nuts, lo, hi, bolts[pivot]);
  100.     // recursively sort nuts and bolts in ranges [lo...pivot-1] and [pivot+1...hi]
  101.     sort(nuts, bolts, lo, pivot-1);
  102.     sort(nuts, bolts, pivot+1, hi);
  103. }
  104.  
  105. void sort(Nut** nuts, Bolt** bolts, int N)  {
  106.     srand(time(NULL));
  107.     sort(nuts, bolts, 0, N-1);
  108. }
  109.  
  110. int main()  {
  111.     int N, i, size;
  112.     Nut **nuts;
  113.     Bolt **bolts;
  114.     char str[100];
  115.  
  116.     printf("Enter the number of nuts (or equivalently bolts) : ");
  117.     scanf("%d", &N);
  118.     nuts = new Nut*[N];
  119.     bolts = new Bolt*[N];
  120.  
  121.     printf("Enter sizes of %d nuts :\n", N);
  122.     for (i = 0; i < N; i++) {
  123.         scanf("%d", &size);
  124.         nuts[i] = new Nut(size);
  125.     }
  126.  
  127.     printf("Enter sizes of %d bolts (nuts and bolts must have 1-1 correspondence) :\n", N);
  128.     for (i = 0; i < N; i++) {
  129.         scanf("%d", &size);
  130.         bolts[i] = new Bolt(size);
  131.     }
  132.  
  133.     // sort the nuts and bolts together
  134.     sort(nuts, bolts, N);
  135.  
  136.     printf("Matching nuts and bolts :\n");
  137.     for (i = 0; i < N; i++) {
  138.         nuts[i]->to_str(str);
  139.         printf("%s ", str);
  140.         bolts[i]->to_str(str);
  141.         printf("%s\n", str);
  142.     }
  143.  
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement