//WAP to implement Selection Sort.
#include <stdio.h>
void main()
{
int array[10], i, j, N, temp;
int findmax(int b[10], int k); // function prototype (declaration)
void exchang(int b[10], int k); // function prototype (declaration)
clrscr(); // clearing the console screen
printf("Enter the size of array\n");
scanf("%d",&N); // inputting array size
printf("Enter the elements one by one\n");
for(i=0; i<N ; i++)
{
scanf("%d",&array[i]); // inputting array elements
}
exchang(array, N); // function calling
/* Selection sorting begins */
printf("Sorted array is...\n");
for(i=0; i< N ; i++)
{
printf("%d\n",array[i]);
}
} // End of main()
int findmax(int b[10], int k)/* function to find the maximum array element*/
{
int max=0,j;
for(j = 1; j <= k; j++)
{
if ( b[j] > b[max])
{
max = j;
}
}
return(max);
}
void exchang(int b[10],int k) // function to sort and swap elements of array
{
int temp, big, j;
for ( j=k-1; j>=1; j--)
{
big = findmax(b,j);
temp = b[big];
b[big] = b[j];
b[j] = temp;
}
return;
}
No comments:
Post a Comment