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

Using Titanic dataset, do the following: a. Find total number of passengers with age less than 30 b. Find total fare paid by passengers of first class c. Compare number of survivors of each passenger class

Differentiate between Unimodal, Bimodal and Multimodal with graphic representation.

Import iris data using sklearn library . Compute mean, mode, median, standard deviation, confidence interval and standard error for each feature ii. Compute correlation coefficients between each pair of features and plot heatmap iii. Find covariance between length of sepal and petal iv. Build contingency table for class feature