sidrs

OOP Ass 5 - Selection Sort und Templates

Sep 13th, 2024
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 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.  
  38.             temp = arr[min];
  39.             arr[min] = arr[i];
  40.             arr[i] = temp;
  41.         }
  42.     }
  43.  
  44.     void display() {
  45.         // cout << "\nSorted Array: " << endl;
  46.         cout << "[ ";
  47.         for (int i = 0; i < size; i++) {
  48.             if (i != size - 1) {
  49.                 cout << arr[i] << ", ";
  50.             } else if (i == size - 1) {
  51.                 cout << arr[i] << " ]";
  52.             }
  53.  
  54.         }
  55.         cout << endl;
  56.     }
  57. };
  58.  
  59.  
  60. int main() {
  61.  
  62.     int choice;
  63.  
  64.     while (true) {
  65.         cout << "\n---------- MENU ----------" << endl;
  66.         cout << "1. Sort Integer Values"  << endl;
  67.         cout << "2. Sort Float Values" << endl;
  68.         cout << "3. Exit" << endl;
  69.         cout << "---------------------------" << endl;
  70.         cout << "Enter your choice: "; cin >> choice;
  71.         cout << endl;
  72.  
  73.         if (choice == 1) {
  74.             int size;
  75.             cout << "Enter the number of elements: "; cin >> size;
  76.             Sort<int> zuck(size);
  77.             zuck.populate();
  78.             cout << "\nThe Entered Array is: " << endl;
  79.             zuck.display();
  80.             zuck.selectSort();
  81.             cout << "\nThe Sorted Array is: " << endl;
  82.             zuck.display();
  83.         }
  84.  
  85.         else if (choice == 2) {
  86.             int size;
  87.             cout << "Enter the number of elements: "; cin >> size;
  88.             Sort<float> zuck(size);
  89.             zuck.populate();
  90.             cout << "\nThe Entered Array is: " << endl;;
  91.             zuck.display();
  92.             zuck.selectSort();
  93.             cout << "\nThe Sorted Array is: " << endl;
  94.             zuck.display();
  95.        
  96.         }
  97.  
  98.         else if (choice == 3) {
  99.             cout << endl;
  100.             cout << "Terminating program..." << endl;
  101.             cout << "Byee" << endl;
  102.             cout << endl;
  103.             break;
  104.         }
  105.  
  106.         else {
  107.             cout << endl;
  108.             cout << "ERROR! Please Enter a Valid Choice." << endl;
  109.             cout << endl;
  110.         }
  111.     }
  112.  
  113.  
  114.  
  115.  
  116.     return 0;
  117. }
  118.  
  119.  
  120. /* OUTPUT:
  121.  
  122. ---------- MENU ----------
  123. 1. Sort Integer Values
  124. 2. Sort Float Values
  125. 3. Exit
  126. ---------------------------
  127. Enter your choice: 1
  128.  
  129. Enter the number of elements: 5
  130. 16
  131. 34
  132. 78
  133. 59
  134. 45
  135.  
  136. The Entered Array is:
  137. [ 16, 34, 78, 59, 45 ]
  138.  
  139. The Sorted Array is:
  140. [ 16, 34, 45, 59, 78 ]
  141.  
  142. ---------- MENU ----------
  143. 1. Sort Integer Values
  144. 2. Sort Float Values
  145. 3. Exit
  146. ---------------------------
  147. Enter your choice: 2
  148.  
  149. Enter the number of elements: 3
  150. 12.45
  151. 57.34
  152. 37.42
  153.  
  154. The Entered Array is:
  155. [ 12.45, 57.34, 37.42 ]
  156.  
  157. The Sorted Array is:
  158. [ 12.45, 37.42, 57.34 ]
  159.  
  160. ---------- MENU ----------
  161. 1. Sort Integer Values
  162. 2. Sort Float Values
  163. 3. Exit
  164. ---------------------------
  165. Enter your choice: 3
  166.  
  167.  
  168. Terminating program...
  169. Byee
  170.  
  171. */
Advertisement
Add Comment
Please, Sign In to add comment