sidrs

[TODO] OOP Ass 5 - Merge Sort and Templates

Sep 13th, 2024
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1. //  OOP Assignment 5
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. template <class T>
  7. class Sort {
  8. private:
  9.     int size;
  10.     T *arr;
  11. public:
  12.     Sort(int size) {
  13.         this->size = size;
  14.         arr = new T[size];
  15.     }
  16.  
  17.     ~Sort() {
  18.         delete[] arr;
  19.     }
  20.  
  21.     void populate(){
  22.         for (int i = 0; i < size; i++) {
  23.             cin >> arr[i];
  24.         }
  25.     }
  26.  
  27.     void selectSort(){
  28.         T temp;
  29.         int min;
  30.         for (int i = 0; i < size; i++){
  31.             min = i;
  32.             for (int j = i + 1; j < size; j++) {
  33.                 if (arr[min] > arr[j]) {
  34.                     min = j;
  35.                 }
  36.             }
  37.             temp = arr[min];
  38.             arr[min] = arr[i];
  39.             arr[i] = temp;
  40.         }
  41.     }
  42.  
  43.     void merge(int left, int right, int mid) {
  44.         int s1 = mid - left + 1;
  45.         int s2 = right - mid;
  46.  
  47.         if (s1 <= 0 || s2 < 0) {
  48.                     cerr << "Error: Invalid array sizes for merging." << endl;
  49.                     return;
  50.             }
  51.  
  52.         T *L = new T[s1];
  53.         T *R = new T[s2];
  54.  
  55.         for (int i = 0; i < s1; i++) L[i] = arr[left + i];
  56.         for (int j = 0; j < s2; j++) R[j] = arr[mid + 1 + j];
  57.  
  58.         int i = 0, j = 0, k = left;
  59.         while (i < s1 and j < s2) {
  60.             if (L[i] <= R[j]) {
  61.                 arr[k] = L[i];
  62.                 i++;
  63.             } else {
  64.                 arr[k] = R[j];
  65.                 j++;
  66.             }
  67.             k++;
  68.         }
  69.  
  70.         while (i < s1) {
  71.             arr[k] = L[i];
  72.             i++;
  73.             k++;
  74.         }
  75.  
  76.         while (j < s2) {
  77.             arr[k] = R[j];
  78.             j++;
  79.             k++;
  80.         }
  81.  
  82.         delete[] L;
  83.         delete[] R;
  84.     }
  85.  
  86.     void mergeSort(int left, int right) {
  87.         if (left >= right) return;
  88.         int mid = left + (right - left) / 2;
  89.         mergeSort(left, mid);
  90.         mergeSort((mid + 1), right);
  91.         merge(left, mid, right);
  92.     }
  93.  
  94.    
  95.  
  96.     void display() {
  97.         // cout << "\nSorted Array: " << endl;
  98.         cout << "[ ";
  99.         for (int i = 0; i < size; i++) {
  100.             if (i != size - 1) {
  101.                 cout << arr[i] << ", ";
  102.             } else if (i == size - 1) {
  103.                 cout << arr[i] << " ]";
  104.             }
  105.  
  106.         }
  107.         cout << endl;
  108.     }
  109. };
  110.  
  111.  
  112. int main() {
  113.  
  114.     int choice;
  115.  
  116.     while (true) {
  117.         cout << "\n---------- MENU ----------" << endl;
  118.         cout << "1. Sort Integer Values"  << endl;
  119.         cout << "2. Sort Float Values" << endl;
  120.         cout << "3. Exit" << endl;
  121.         cout << "---------------------------" << endl;
  122.         cout << "Enter your choice: "; cin >> choice;
  123.         cout << endl;
  124.        
  125.         if (choice == 1) {
  126.             int size;
  127.             cout << "Enter the number of elements: "; cin >> size;
  128.             Sort<int> zuck(size);
  129.             zuck.populate();
  130.             cout << "\nThe Entered Array is: " << endl;
  131.             zuck.display();
  132.             //zuck.selectSort();
  133.             zuck.mergeSort(0, size - 1);
  134.             cout << "\nThe Sorted Array is: " << endl;
  135.             zuck.display();
  136.         }
  137.  
  138.         else if (choice == 2) {
  139.             int size;
  140.             cout << "Enter the number of elements: "; cin >> size;
  141.             Sort<float> zuck(size);
  142.             zuck.populate();
  143.             cout << "\nThe Entered Array is: " << endl;;
  144.             zuck.display();
  145.             //zuck.selectSort();
  146.             zuck.mergeSort(0, size - 1);
  147.             cout << "\nThe Sorted Array is: " << endl;
  148.             zuck.display();
  149.        
  150.         }
  151.  
  152.         else if (choice == 3) {
  153.             cout << endl;
  154.             cout << "Terminating program..." << endl;
  155.             cout << "Byee" << endl;
  156.             cout << endl;
  157.             break;
  158.         }
  159.  
  160.         else {
  161.             cout << endl;
  162.             cout << "ERROR! Please Enter a Valid Choice." << endl;
  163.             cout << endl;
  164.         }
  165.     }
  166.  
  167.  
  168.  
  169.  
  170.     return 0;
  171. }
  172.  
Advertisement
Add Comment
Please, Sign In to add comment