Page 227 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 227

Fundamentals of Data Structures




                    Notes                  if(low >= high)
                                                   return;
                                           else
                                           {
                                                   mid =(low + high) / 2;
                                                   MergeSort(a, low, mid);
                                                   MergeSort(a, mid+1, high);
                                                   Merge(a, low, mid, high);
                                           }
                                   }
                                   int main()
                                   {
                                           int *a;
                                           int n;
                                           cout << “The number of elements is:”;
                                           cin>>n;
                                           a = (int*) calloc (n,sizeof(int));
                                           for(int i=0;i < n;i++)
                                           {
                                                   cout << “Input” << i << “element:”;
                                                   cin >> a[i]; // Adding the elements to the array
                                           }
                                           cout << “nUnsorted list:” << endl;      // Displaying the
                                   unsorted array
                                           for(int i=0;i < n;i++)
                                           {
                                                   cout << a[i] << “ ”;
                                           }
                                           MergeSort(a, 0, n-1);
                                           cout<<“nThe sorted list is:” << endl;
                                           for (int i=0;i < n;i++)
                                                   cout << a[i] << “ ”;
                                           return 0;
                                   }
                                   The output is shown as below:

















          220                               LOVELY PROFESSIONAL UNIVERSITY
   222   223   224   225   226   227   228   229   230   231   232