0

My question is with the counting. In my problem the number of the operations after sorting is not giving the same amount as it should. I am using the general logic for the selection sort, insertion sort and merge sort but it is giving in the wrong operations number.

The expected output should look like this:

Enter the number of integer elements: 5

Enter the elements: 4 78 9 35 29

SelectionSort results: 4 9 29 35 78

Required number of operations: 14

InsertionSort results: 4 9 29 35 78

Required number of operations: 12

MergeSort results: 4 9 29 35 78

Required number of operations: 12

But my output is looking like this:

Enter the number of integer elements: 5

Enter the elements: 4 78 9 35 29

SelectionSort results: 4 9 29 35 78

Required number of operations: 10

InsertionSort results: 4 9 29 35 78

Required number of operations: 0

MergeSort results: 4 9 29 35 78

Required number of operations: 7

My code is given below:

#include <iostream>
using namespace std;

// Function to perform Selection Sort
int selectionSort(int arr[], int n)
{
    int operations = 0;
    for (int i = 0; i < n - 1; ++i)
    {
        int minIndex = i;
        for (int j = i + 1; j < n; ++j)
        {
            if (arr[j] < arr[minIndex])
            {
                minIndex = j;
            }
            operations++; // Counting the number of comparisons
        }
        swap(arr[i], arr[minIndex]);
    }
    return operations;
}

// Function to perform Insertion Sort
int insertionSort(int arr[], int n)
{
    int operations = 0;
    for (int i = 1; i < n; ++i)
    {
        int key = arr[i];
        int j = i - 1;
        while (j >= 0 && arr[j] > key)
        {
            arr[j + 1] = arr[j];
            j--;
            operations++; // Counting the number of comparisons and shifts
        }
        arr[j + 1] = key;
    }
    return operations;
}

// Function to merge two sorted subarrays in Merge Sort
void merge(int arr[], int left, int mid, int right, int &operations)
{
    int n1 = mid - left + 1;
    int n2 = right - mid;
    int L[n1], R[n2];
    for (int i = 0; i < n1; i++)
        L[i] = arr[left + i];
    for (int j = 0; j < n2; j++)
        R[j] = arr[mid + 1 + j];
    int i = 0, j = 0, k = left;
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
        operations++; // Counting the number of comparisons
    }
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

// Function to perform Merge Sort
void mergeSortHelper(int arr[], int left, int right, int &operations)
{
    if (left < right)
    {
        int mid = left + (right - left) / 2;
        mergeSortHelper(arr, left, mid, operations);
        mergeSortHelper(arr, mid + 1, right, operations);
        merge(arr, left, mid, right, operations);
    }
}

int mergeSort(int arr[], int n)
{
    int operations = 0;
    mergeSortHelper(arr, 0, n - 1, operations);
    return operations;
}

int main()
{
    int n;
    cout << "Enter the number of integer elements: ";
    cin >> n;
    int arr[n];
    cout << "Enter the elements: ";
    for (int i = 0; i < n; ++i)
    {
        cin >> arr[i];
    }

    int operationsSelection = selectionSort(arr, n);
    cout << "SelectionSort results:";
    for (int i = 0; i < n; ++i)
    {
        cout << " " << arr[i];
    }
    cout << "\nRequired number of operations: " << operationsSelection << endl;

    int operationsInsertion = insertionSort(arr, n);
    cout << "InsertionSort results:";
    for (int i = 0; i < n; ++i)
    {
        cout << " " << arr[i];
    }
    cout << "\nRequired number of operations: " << operationsInsertion << endl;

    int operationsMerge = mergeSort(arr, n);
    cout << "MergeSort results:";
    for (int i = 0; i < n; ++i)
    {
        cout << " " << arr[i];
    }
    cout << "\nRequired number of operations: " << operationsMerge << endl;

    return 0;
}`
5
  • What is the definition of "operation"? Commented Apr 6, 2024 at 19:52
  • it is working as a counter by incrementing the "operations" variable whenever a comparison or a swap operation is performed in each sorting algorithm. Commented Apr 6, 2024 at 21:43
  • If that is what you were told, then note that you don't count swaps. But taking a step back, there are many variants of these sorting algorithms and these numbers would be impacted by which version you choose. For instance, merge sort could split odd-sized arrays by taking a shorter left or shorter right partition. Without clear, unambiguous specifications of the exact flavour of the involved sorting algorithms, this exercise is useless. Commented Apr 6, 2024 at 21:46
  • One major flaw in your set up is that after the first sort, you pass a sorted array to the second and third sorting algorithms. Commented Apr 6, 2024 at 21:47
  • Also, mergesort doesn't perform swaps, so what are you supposed to count then besides comparisons? Commented Apr 7, 2024 at 8:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.