Selection Sort Program in C

Write a selection sort program in C. Given an unsorted array, write a c code to sort an array using selection sort.

For example :

Input :     {9, 8, 19, 2, 3 }
Output :  { 2, 3, 8, 9, 19}

After sorting, array elements are arranged in a sorted order.

Selection Sort

Selection sort is an in-place comparison technique. In selection sort, we pick minimum element and move an element to it’s correct position. We repeat this process until all the elements are sorted.

Let’s understand this process in detail. In Selection sort, we divide an array into two parts sorted (left end) and unsorted (right end). Initially the sorted part is empty and we consider complete array as an unsorted.

We perform following steps to sort an array –

i) At each iteration, we pick the minimum element from the unsorted subarray.

ii) Then Swap it with the leftmost element of the unsorted subarray.

iii) As we put the element to it’s correct position, the size of the sorted array is keep increasing and unsorted is decreasing.  At the end we get the sorted array.

Selection Sort Explanation with Example - Step by Step

Selection Sort Explanation with Example – Step by Step

The time complexity of selection sort is O(n^2). It is not suitable for large data sets.

Bubble Sort Program in Java

Programming Video Tutorials

How Selection Sort Works?

Selection sort is an in-place comparison sort, in which both searching and sorting take place. In each iteration, we pick the smallest or largest element and move to its proper place .

I have added video tutorial at the end of this post for better understanding.

In the above example, We have 5 elements in an array so it requires 4 steps to sort an array. Here is the Pseudo code for selection sort algorithm.

Selection Sort Program in C

Selection Sort Program in C

Selection Sort Program in C

We have discussed the logic. Let’s write a code of selection sort in C.

In this video tutorial, I have explained how selection sort works and their implementation using java and c code.

Tagged , , , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.