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

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

C Language में Array और इसके विभिन्न प्रकारो के बारे में और अधिक जानने के लिए देखें— Arrays in C

Pointer to array क्या होता है जानने के लिए देखें—Pointer to array in C

Unconditional branching statements के बारे में अधिक जानकारी के लिए देखें— Break, Continue and Goto in C

C program for sorting in array-Selection sort

#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;
}

C++ program for sorting in array-Selection sort

#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 of C C++ program for sorting in array-Selection sort

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

More programs on array in C C++

  1. Declaration and initialization of array
  2. Input-output in array
  3. Sum and average of array elements
  4. Largest Element in Array
  5. Smallest element in Array
  6. Reverse Elements of Array
  7. Merging two arrays
  8. Searching in array (Linear search)
  9. Pointer to array
Share it to:

Published by

admin

I am a computer teacher, programmer and web developer