selectionSort_withMinimum code example

Example: selectionSort_withMinimum

/* sorting array using selection sort method */

#include <stdio.h>
#define SIZE 5

int main ()
{
   int data[ SIZE ] = { 3, 12, 7, 10, 5};
   int pass, i, temp;

   printf("Original order of data items:\n");

   //printing data items
   for ( i = 0; i < SIZE; ++i)
   {
      printf("%4d", data[ i ]);
   }

   //selection sort algorithm
   for (pass = 0; pass < SIZE; ++pass)
   {
      for ( i = pass + 1; i < SIZE; ++i)
      {
         if ( data[ pass ] > data[ i ])
         {
            //steps for swapping
            temp = data[ pass ];
            data[ pass ] = data [ i ];
            data[ i ] = temp;
         }
      }
   }
   printf("\n\nData items in ascending order:\n");
   for (i= 0; i < SIZE; ++i)
   {
      printf("%4d", data[ i ]);
   }
   return 0;
}

Tags:

Misc Example