Sort the following sets of elements using selection sort show the Content of element every pass. 34,56,12, 8,92,9,44,23 Data structure using c++


Sort the following sets of elements using selection sort show the Content of element  every pass.

34,56,12, 8,92,9,44,23


Data structure using c++



To sort the given set of elements using the selection sort algorithm and display the content of the elements at each pass, you can use the following C++ code:


cpp

Copy code

#include <iostream>

#include <vector>


using namespace std;


void selectionSort(vector<int>& arr) {

    int n = arr.size();


    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;

            }

        }


        // Swap the minimum element with the current element

        swap(arr[i], arr[minIndex]);


        // Display the content of the elements at each pass

        cout << "Pass " << i + 1 << ": ";

        for (int k = 0; k < n; k++) {

            cout << arr[k] << " ";

        }

        cout << endl;

    }

}


int main() {

    vector<int> elements = {34, 56, 12, 8, 92, 9, 44, 23};


    cout << "Original elements: ";

    for (int element : elements) {

        cout << element << " ";

    }

    cout << endl << endl;


    selectionSort(elements);


    cout << endl << "Sorted elements: ";

    for (int element : elements) {

        cout << element << " ";

    }

    cout << endl;


    return 0;

}

When you run this code, it will output the content of the elements at each pass of the selection sort algorithm:


yaml

Copy code

Original elements: 34 56 12 8 92 9 44 23


Pass 1: 8 56 12 34 92 9 44 23

Pass 2: 8 9 12 34 92 56 44 23

Pass 3: 8 9 12 34 92 56 44 23

Pass 4: 8 9 12 23 92 56 44 34

Pass 5: 8 9 12 23 34 56 44 92

Pass 6: 8 9 12 23 34 44 56 92

Pass 7: 8 9 12 23 34 44 56 92


Sorted elements: 8 9 12 23 34 44 56 92

As shown, the content of the elements is displayed at each pass of the selection sort algorithm, and the sorted elements are shown at the end.







Comments

Popular posts from this blog

Load a Pandas dataframe with a selected dataset. Identify and count the missing values in a dataframe. Clean the data after removing noise as follows: a. Drop duplicate rows. b. Detect the outliers and remove the rows having outliers c. Identify the most correlated positively correlated attributes and negatively correlated attributes

what is KDD? Explain about data mining as a step in the process of knowledge discovery

The weights of 8 boys in kilograms: 45, 39, 53, 45, 43, 48, 50, 45. Find the median