Selection sort

 

Selection Sort






OURSHOPKEEPER









#include <iostream>
using namespace std;
// Transverse the array find the minium element and swap
void selection_sort(int a[], int n);
void swap(int &aint &b);
int main()
{
    int ijna[50];
    cout << "Enter the number of elements: " << endl;
    cin >> n;
    cout << "Enter the elemenets: " << endl;
    for (int i = 0i < ni++)
        cin >> a[i];
    cout << "array after sorting: " << endl;
    selection_sort(an);
    for (int i = 0i < ni++)
        cout << a[i<< " ";
}
void selection_sort(int a[], int n)
{
    int min_indexij;
    for (int i = 0i < n - 1i++)
    {
        min_index = i;
        for (int j = i + 1j < nj++)
        {
            if (a[j] < a[min_index])
                min_index = j;
        }
        swap(a[min_index], a[i]);
    }
}
void swap(int &aint &b)
{
    int temp = a;
    a = b;
    b = temp;
}





    


Click on the above button to download the code.



Post a Comment

If you have furthur QUERIES please let us know

Previous Post Next Post