Advertisement
PhoxFyre

program1

Mar 29th, 2024 (edited)
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.25 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <iomanip>
  6.  
  7.  
  8. const int MAX_WORDS = 100000;
  9. const int MAX_WORD_LENGTH = 20;
  10.  
  11. //Prototypes
  12. int strcasecmp(char *a, char *b);
  13. void quicksort(std::string arr[], int left, int right);
  14. void sort(std::string A[], int n);
  15. void mergeSort(std::string A[], int left, int right, std::string temp[]);
  16. void merge(std::string A[], int left, int leftEnd, int right, int rightEnd, std::string temp[]);
  17.  
  18. int main()
  19. {
  20.     std::string input_file_name, output_file_name;
  21.     std::cout << "Welcome to the sorting words program." << "\n";
  22.     std::cout << "Please enter the name of your input data file: ";
  23.     std::cin >> input_file_name;
  24.     std::cout << "Please enter the name of your output data file: ";
  25.     std::cin >> output_file_name;
  26.  
  27.     std::ifstream input_file(input_file_name);
  28.     if (!input_file)
  29.     {
  30.         std::cerr << "Error: Unable to open input file.\n";
  31.         return 1;
  32.     }
  33.  
  34.     std::string *words = new std::string[MAX_WORDS];
  35.     std::string *words2 = new std::string[MAX_WORDS];
  36.     int count = 0;
  37.     std::string word;
  38.     while (input_file >> word && count < MAX_WORDS)
  39.     {
  40.         words[count] = word;
  41.         words2[count] = word;
  42.         count++;
  43.     }
  44.  
  45.     int N;
  46.     std::cout << "\n" << count << " words were found in the file " << input_file_name << "\n";
  47.     std::cout << "How many words per line should be printed? ";
  48.     std::cin >> N;
  49.  
  50.     // Sort using Quicksort
  51.     quicksort(words, 0, count - 1);
  52.     // Sort using Mergesort
  53.     sort(words2, count);
  54.  
  55.     std::ofstream output_file(output_file_name);
  56.     if (!output_file)
  57.     {
  58.         std::cerr << "Error: Unable to open output file.\n";
  59.         return 1;
  60.     }
  61.     output_file << count << " words sorted using quicksort" << "\n" << "\n";
  62.  
  63.     // Write sorted data to the output file for quicksort
  64.     for (int i = 0; i < count; i += N)
  65.     {
  66.         for (int j = i; j < i + N && j < count; ++j)
  67.         {
  68.             output_file << std::setw(MAX_WORD_LENGTH) << std::left << words[j]; // Assuming each word has a maximum length of MAX_WORD_LENGTH characters
  69.         }
  70.         output_file << '\n';
  71.     }
  72.  
  73.     output_file << "\n" << "\n" << count << " words sorted using merge sort, printed in reverse order" << "\n" << "\n";
  74.  
  75.     // Write sorted data to the output file for quicksort
  76.     for (int i = 0; i < count; i += N)
  77.     {
  78.         for (int j = i; j < i + N && j <= count; ++j)
  79.         {
  80.             output_file << std::setw(MAX_WORD_LENGTH) << std::left << words2[count - (j-1)]; // Assuming each word has a maximum length of MAX_WORD_LENGTH characters
  81.         }
  82.         output_file << '\n';
  83.     }
  84.  
  85.  
  86.     std::cout << "End program.\n";
  87.  
  88.     return 0;
  89. }
  90.  
  91. // -----------------------------------------
  92. // ------------- Quick Sort ----------------
  93. // -----------------------------------------
  94.  
  95. // Case-insensitive string comparison function
  96. int strcasecmp(const char *a, const char *b)
  97. {
  98.     while (*a && *b)
  99.     {
  100.         int diff = tolower(*a) - tolower(*b);
  101.         if (diff != 0)
  102.         {
  103.             return diff;
  104.         }
  105.         ++a;
  106.         ++b;
  107.     }
  108.     return tolower(*a) - tolower(*b);
  109. }
  110.  
  111. // Quicksort implementation
  112. void quicksort(std::string arr[], int left, int right)
  113. {
  114.     if (left >= right)
  115.         return;
  116.  
  117.     std::string pivot = arr[(left + right) / 2];
  118.  
  119.     int i = left, j = right;
  120.     while (i <= j)
  121.     {
  122.         while (strcasecmp(arr[i].c_str(), pivot.c_str()) < 0)
  123.         {
  124.             ++i;
  125.         }
  126.         while (strcasecmp(arr[j].c_str(), pivot.c_str()) > 0)
  127.         {
  128.             --j;
  129.         }
  130.         if (i <= j)
  131.         {
  132.             std::swap(arr[i], arr[j]);
  133.             ++i;
  134.             --j;
  135.         }
  136.     }
  137.     quicksort(arr, left, j);
  138.     quicksort(arr, i, right);
  139. }
  140.  
  141. // -----------------------------------------
  142. // ------------- Merge sort ----------------
  143. // -----------------------------------------
  144.  
  145. void sort(std::string A[], int n)
  146. {
  147.     std::string *temp = new std::string[n];
  148.     mergeSort(A,0,n-1,temp);
  149.  
  150.     delete[] temp;
  151. }
  152.  
  153. void mergeSort(std::string A[], int left, int right, std::string temp[])
  154. {
  155.     if (left < right)
  156.     {
  157.         int mid = (left+right)/2;
  158.         mergeSort(A, left, mid, temp);
  159.         mergeSort(A, mid+1, right, temp);
  160.         merge(A, left, mid, mid+1, right, temp);
  161.     }
  162. }
  163.  
  164. void merge(std::string A[], int left, int leftEnd, int right, int rightEnd, std::string temp[])
  165. {
  166.     int saveStart = left;   // saves the starting index
  167.     int index = left;       // to walkthrough temp
  168.  
  169.     while (left <= leftEnd && right <= rightEnd)
  170.     {
  171.         if (strcasecmp(A[left].c_str(), A[right].c_str()) < 0)
  172.         {
  173.             temp[index] = A[left];
  174.             index++;
  175.             left++;
  176.         }
  177.         else
  178.         {
  179.             temp[index] = A[right];
  180.             index++;
  181.             right++;
  182.         }
  183.     }
  184.  
  185.     while (left <= leftEnd)
  186.     {
  187.         temp[index] = A[left];
  188.         index++;
  189.         left++;
  190.     }
  191.  
  192.     while (right <= rightEnd)
  193.     {
  194.         temp[index] = A[right];
  195.         index++;
  196.         right++;
  197.     }
  198.  
  199.     for (int i = saveStart; i <= rightEnd; i++)
  200.     {
  201.         A[i] = temp[i];
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement