Wednesday, October 20, 2021

Program 2- Selection Sort in C Language

Program 1: Selection Sort
****************************************************

#include <stdio.h>

int main()

{   int i, j, n, temp, arr[25];

   printf("How many numbers u are going to enter?: ");

   scanf("%d",&n);

   printf("Enter %d elements: ", n);

   // Loop to get the elements stored in array

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

      scanf("%d",&arr[i]);

 

   // Logic of selection sort algorithm

   for(i=0;i<n;i++){

      for(j=i+1;j<n;j++){

         if(arr[i]>arr[j]){

            temp=arr[i];

            arr[i]=arr[j];

            arr[j]=temp;

         }

      }

   }

   printf("Sorted elements: ");

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

      printf(" %d",arr[i]);

    return 0;

}

Output