Code for SELECTION Sort in C-Programming

1                           Selection Sort

Ø     

#include<stdio.h>
#include<conio.h>

void SelectionSort(int[] , int);

void main()
{

   int array[10], n, i;
   clrscr();
   printf("\n\n--------------------- SELECTION SORT --------------------");

   printf("\n\n ENTER NUMBER OF ELEMENTS : ");
   scanf("%d", &n);

   printf("\n");

   for (i = 0 ; i < n ; i++ )
   { printf("ENTER ELEMENT %d : ",i+1);

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

   SelectionSort(array,n);

   getch();
}

void SelectionSort(int array[], int n)
{
   int i,c,
      j,temp;
   for ( i = 0 ; i < ( n-1 ) ; i++ )
   {
      for ( j = i + 1 ; j < n ; j++ )
      {
          if ( array[i] > array[j])
            {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
             }
      }

      printf("\nPASS %d :",i+1);

   for ( c = 0 ; c < n ; c++ )
   {   printf(" %d ", array[c]);  }
   }

   printf("\n\n FINAL SORTED ELEMENTS : ");
   for ( i = 0 ; i < n ; i++ )
  {    printf(" %d ", array[i]); }
  getch(); }

OUTPUT:



Comments

Popular posts from this blog

Code for BUBBLE Sort

Code for Circular Linked List c-programing