bsc

Comprehensive codebase and coursework for the 4-year B.Sc. Computer Science Honours degree (WBSU)

git clone https://github.com/notamitgamer/bsc.git
· rebuilt hourlyOpen on GitHub
root / semester_2 / assignments / assignment_03.cpp

assignment_03.cpp (3407B)


      1 /* WAP to sort a list of elements. Give user the option to perform sorting using Insertion sort, Bubble sort or Selection sort. */
      2 
      3 #include <iostream>
      4 #include <iomanip>
      5 using namespace std;
      6 
      7 typedef struct MenuItem {
      8     int id;
      9     const char* name;
     10 } menu;
     11 
     12 class SortList {
     13 private:
     14     int *arr;
     15     int *tempArr;
     16     int n;
     17 
     18     void swapElements(int &a, int &b) {
     19         int temp = a;
     20         a = b;
     21         b = temp;
     22     }
     23 
     24     void reset() {
     25         for (int i = 0; i < n; i++) tempArr[i] = arr[i];
     26     }
     27 
     28 public:
     29     SortList(int size) {
     30         n = size;
     31         arr = new int[n];
     32         tempArr = new int[n];
     33     }
     34 
     35     void inputElements() {
     36         cout << "\nEnter " << n << " elements: ";
     37         for (int i = 0; i < n; i++) {
     38             cin >> arr[i];
     39             tempArr[i] = arr[i];
     40         }
     41     }
     42 
     43     void insertionSort() {
     44         reset();
     45         for (int i = 1; i < n; i++) {
     46             int key = tempArr[i];
     47             int j = i - 1;
     48             while (j >= 0 && tempArr[j] > key) {
     49                 tempArr[j + 1] = tempArr[j];
     50                 j--;
     51             }
     52             tempArr[j + 1] = key;
     53         }
     54         display();
     55     }
     56 
     57     void bubbleSort() {
     58         reset();
     59         for (int i = 0; i < n - 1; i++) {
     60             bool swapped = false;
     61             for (int j = 0; j < n - i - 1; j++) {
     62                 if (tempArr[j] > tempArr[j + 1]) {
     63                     swapElements(tempArr[j], tempArr[j + 1]);
     64                     swapped = true;
     65                 }
     66             }
     67             if (!swapped) break;
     68         }
     69         display();
     70     }
     71 
     72     void selectionSort() {
     73         reset();
     74         for (int i = 0; i < n - 1; i++) {
     75             int minIdx = i;
     76             for (int j = i + 1; j < n; j++) {
     77                 if (tempArr[j] < tempArr[minIdx]) minIdx = j;
     78             }
     79             if (minIdx != i) swapElements(tempArr[i], tempArr[minIdx]);
     80         }
     81         display();
     82     }
     83 
     84     void display() {
     85         cout << "Sorted Array: ";
     86         for (int i = 0; i < n; i++) cout << tempArr[i] << " ";
     87         cout << endl;
     88     }
     89 
     90     void displayOriginal() {
     91         cout << "\nOriginal List: ";
     92         for (int i = 0; i < n; i++) cout << arr[i] << " ";
     93         cout << endl;
     94     }
     95 
     96     void printMenu() {
     97         menu items[] = {
     98             {1, "Insertion Sort"}, {2, "Bubble Sort"},
     99             {3, "Selection Sort"}, {0, "Display Original"},
    100             {-1, "Exit"}
    101         };
    102         for (int i = 0; i < 5; i++) {
    103             cout << items[i].id << ". " << items[i].name << "\n";
    104         }
    105     }
    106 
    107     ~SortList() {
    108         delete[] arr;
    109         delete[] tempArr;
    110     }
    111 };
    112 
    113 int main() {
    114     int n, choice;
    115     cout << "Enter number of elements: ";
    116     cin >> n;
    117     if (n <= 0) {
    118         cout << "Invalid size!\n";
    119         return 0;
    120     }
    121 
    122     SortList sorter(n);
    123     sorter.inputElements();
    124 
    125     while (true) {
    126         sorter.printMenu();
    127         cout << "Enter your choice: ";
    128         cin >> choice;
    129 
    130         switch (choice) {
    131             case 1: sorter.insertionSort(); break;
    132             case 2: sorter.bubbleSort(); break;
    133             case 3: sorter.selectionSort(); break;
    134             case 0: sorter.displayOriginal(); break;
    135             case -1:
    136                 cout << "\nExiting program.....\nMemory cleared!\nExit 0\n";
    137                 return 0;
    138             default:
    139                 cout << "\nEntered wrong choice. Try again.\n";
    140         }
    141     }
    142 }