Thursday, January 8, 2009

C code for selection Sort

#include <iostream.h>



void printArray(int a[])

{

 int k;

 for(k=0;k<10;k++)

         cout << a[k] << " ";

 cout << endl;

}



int main()

{

        int a[10] = {1,3,2,4,8,7,5,10,9,6};

        int i,j;

        printArray(a);

        int min,pos;

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

        {

                min = a[i];

                pos = i;

                for(j=i;j<9;j++)

                {

                  if(a[j] < min)

                  {

                     min = a[j];

                     pos = j;

                  }      

                }

                int t = a[i];

                a[i] = a[pos];

                a[pos] = t;

        }





printArray(a);



}

No comments:

Blog Archive