Tuesday, March 22, 2011

Selection sort algorithm

# include "stdio.h"
# define MAX 100
int main()
{
int szArray[MAX],nLoop,nLoop2,nCount;
//To read the array input
printf("Enter the array element count \n");
scanf("%d",&nCount);
printf("Enter the Array Element\n");
for(nLoop=0;nLoop <nCount;nLoop++ )
scanf("%d",&szArray[nLoop]);

// Selection sort algorithm
for(nLoop=0;nLoop<nCount;nLoop++)
{
int nIndex = nLoop;

for(nLoop2=nIndex;nLoop2<nCount;nLoop2++)
{
if(szArray[nIndex] < szArray[nLoop2])
{
nIndex = nLoop2;

}
}
if(nLoop != nIndex)
{
int temp;
temp = szArray[nIndex];
szArray[nIndex] = szArray[nLoop];
szArray[nLoop] = temp;
}
}

// To display the details

for(nLoop = 0; nLoop < nCount;nLoop++)
{
printf("%d\t",szArray[nLoop]);
}

printf("\n");



return 0;
}


OUTPUT:

Enter the array element count
5
Enter the Array Element
1
2
3
4
5
5 4 3 2 1
Press any key to continue . . .

No comments:

Post a Comment