# Selection Sort Program in C

Views: 216

Posted On: 18-Nov-2017 06:29

Share:
reena
Moderator
32 Points
10 Posts

Introduction

Selection sort is a simple sorting and in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end.

Algorithm

First check minimum value in array list and place it at first position (position 0) of array, next find second smallest element in array list and place this value at second position (position 1) and so on. Same process is repeated until sort all element of an array.

1. Find the minimum element in the list.
2. Swap it with the element in the first position of the list.
3. Repeat the steps above for all remaining elements of the list starting from the second position.

Program

``#include<stdio.h>#include<conio.h>void main(){ int array[100], n, c, d, position, swap; clrscr(); printf("Enter number of elements (n): "); scanf("%d", &n); printf("Enter any %d elements: \n", n); for (c = 0; c < n; c++) {   scanf("%d", &array[c]); } for (c = 0; c < (n - 1); c++) {   position=c;   for (d = c + 1; d < n; d++)   {     if (array[position] > array[d])     {       position = d;     }   }   if (position != c)   {     swap = array[c];     array[c] = array[position];     array[position] = swap;   } } printf("Sorted list in ascending order:\n"); for (c = 0; c < n; c++) {   printf("%d  ", array[c]); } getch();}``

Output