C Program:Selection Sort



Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.

PROGRAM:

//Selection sort by Vikas konaparthi

#include <stdio.h>

void select(int a[10],int size);

void ReadArray(int a[10],int size);

void WriteArray(int a[10],int size);



void ReadArray(int a[10],int size)

{

    int i;

    printf("\n Enter %d elements",size);

    for(i=0;i<size;i++)

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

}

void WriteArray(int a[10],int size)

{

    int i;

    printf("\n Sorted Array\n");

    for(i=0;i<size;i++)

    {

        printf("%d\t",a[i]);

    }

}

void select(int a[10],int size)

{

  int i,j,min,temp;

  for(i=0;i<size;i++)

  {

      min=i;

      for(j=i+1;j<size;j++)

      {

          if(a[j]<a[min])

          min=j;

      }

if(min!=i){



      temp = a[i];

      a[i]=a[min];

      a[min]=temp;

  }

    }

}

 

void main()

{

    int a[10],size;

    printf("\nEnter the size of array");

    scanf("%d",&size);

    ReadArray(a,size);

    select(a,size);

    WriteArray(a,size);

}

  
Previous Post Next Post