Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 1.19 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. void MergeSort(char * array1, char * array2, int Max1, int Max2);
  5. void sort(char * array1, int n);
  6. const int Size = 100;
  7. const int Max1 = 50;
  8. const int Max2 = 50;
  9. int main()
  10. {
  11. char array1[Size];
  12. char array2[Size];
  13. cout << "Please enter the content of array number 1:\n";
  14. cin >> array1;
  15. sort(array1,Size);
  16. cout << "Please enter the content of array number 2:\n";
  17. cin >> array2;
  18. sort(array2,Size);
  19. MergeSort(array1,array2,Max1,Max2);
  20. system("pause");
  21. return 0;
  22. }
  23.  
  24. void sort(char * array1, int n) {
  25.    for (int j=1; j < n; j++)  
  26.        for (int i=0; i < n-j; i++)
  27.            if (array1[i] > array1[i+1]) {
  28.                int temp = array1[i]; array1[i] = array1[i+1]; array1[i+1] = temp;
  29.            }
  30.      }
  31. void MergeSort(char * array1, char * array2, int Max1, int Max2) {
  32.      char * array3 = new char[Size];
  33.      for(int i=0;i<Max1;i++)
  34.      array3[i] = array1[i];
  35.      for(int i=strlen(array1);i<Max2;i++)
  36.      array3[i] = array2[i];
  37.      sort(array3,Size);
  38.      cout << "The sorted 3rd array is ";
  39.      for (int i =0;i<strlen(array3);i++)
  40.      cout << array3[i];
  41.      cout << endl;
  42.      delete[] array3;
  43.      }