Write a C program that implements the Selection sort method to sort a given array of integers in ascending order.

Algorithm:

Step 1: Start
Step 2: Read n, a[i] values as integers
Step 3: for i: 1 to n do increment i by 1
        begin
         min = i;
         for j: i + 1 to n increment j by 1
         begin
          if(a[j] < a[min])
          min = j;
         end
         t = a[i];
         a[i] = a[min];
         a[min] = t;
        end
Step 4: for i: 0 to n
        print a[i]
Step 5: Stop

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
 int n, a[20], min, temp, i, j;
 clrscr();
 printf("Enter the size of the array\n");
 scanf("%d", &n);
 printf("Enter the  array elements\n");
 for(i = 0; i < n; i++)
 {
  scanf("%d", &a[i]);
 }
 for(i = 0; i < n - 1; i++)
 {
  min = i;
  for(j = i + 1; j < n; j++)
  {
   if(a[j] < a[min])
   min = j;
  }
  temp = a[i];
  a[i] = a[min];
  a[min] = temp;
 }
 printf("The sorted array is\n");
 for(i = 0; i < n; i++)
  printf("%d\n", a[i]);
 getch();
}

Input & Output:

Enter the size of the array: 7
Enter the array elements: 7  6  5  4  3  2  1  
The Sorted array is: 1  2  3  4  5  6  7