SELECTION SORT

//there is a simple program for sorting an array in increasing order by selection sort

language=C++

#include<iostream>


#include<stdio.h>


using namespace std;

int main()
{
    int n;
    cin >> n;
    int a[n],j;
    for( int i= 0; i < n; i++ )
    {

        scanf( "%d",&a[i] );

    }

    for( int i = 0; i < n-1; i++ )
    {
        int small;

        small = i;

        for( j = i +1; j <= n-1; j++)
        {

            if( a[j] < a[small] )
                small = j;

        }

        int temp = a[i];
        a[i] = a[small];
        a[small] = temp;
    }

    cout<<"\nSorted array\n";

    for( int I=0;I<n;I++ )

        cout << a[I] << " ";

    cout<<endl;

    return 0;
}

No comments:

Post a Comment

Kruskal Algorithm

Minimum Cost Spanning Tree By Kruskal Algorithm using priority_queue in c++ #include<bits/stdc++.h> using namespace std; typedef p...