C C++ program for sorting in array-Selection sort

Coding in C

#include<stdio.h>
int main()
{
    int a[5],i,j,temp;
    printf("Enter values of array: \n");
    for(i=0;i<5;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=4;i>0;i--)
    {
        for(j=0;j<i;j++)
        {
            if(a[j+1]<a[j])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }
    printf("Sorted Array: \n");
    for(i=0;i<5;i++)
    {
        printf("%d ",a[i]);
    }
    return 0;
}

Coding in C++

#include<iostream>
using namespace std;
int main()
{
    int a[5],i,j,temp;
    cout<<"Enter values of array: \n";
    for(i=0;i<5;i++)
    {
        cin>>a[i];
    }
    for(i=4;i>0;i--)
    {
        for(j=0;j<i;j++)
        {
            if(a[j+1]<a[j])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }
    cout<<"Sorted Array: \n";
    for(i=0;i<5;i++)
    {
        cout<<a[i]<<" ";
    }
    return 0;
}

Output

C C++ program for sorting in array-Selection sort

Published by

admin

I am a computer teacher, programmer and web developer