Write a sample C programming Codes to implement selection sort.


// C program to implement selection sort.
#include<stdio.h>
int main(){
  int s,i,j,temp,a[20];
  printf("Enter total elements: ");
  scanf("%d",&s);
  printf("Enter %d elements: ",s);
  for(i=0;i<s;i++)
      scanf("%d",&a[i]);
  for(i=0;i<s;i++){
      for(j=i+1;j<s;j++){
           if(a[i]>a[j]){
               temp=a[i];
              a[i]=a[j];
              a[j]=temp;
           }
      }
  }
  printf("After sorting is: ");
  for(i=0;i<s;i++)
      printf(" %d",a[i]);
  return 0;
}

Sample Output:
Enter total elements: 8
Enter 5 elements: 4 5 0 21 7 1 2 9
The array after sorting is:  0 1 2 4 5 7 9 21

0 comments:

Feel free to contact the admin for any suggestions and help.