# Selection Sort in C Programming Language

In the last article, we have described the implementation of Bubble Sort in C. This article will guide you on how to implement Selection Sort in C. Following are the headings this article will focus on,

**Selection Sort in C****Selection Sort Algorithm**

Let us have a look at these terms in detail:

Table of Contents

## Selection Sort in C

Selection sort is the other algorithm that is used for arranging or sorting. This sorting or arranging algorithm iterates through the array and identifies the tiniest number in the array, and compares it with the first term if it is smaller than the first term. Further, it continues and goes on to the next term and so on until all terms are arranged or sorted accordingly.

## Example of Selection Sort

Consider the array:

**[10, 5, 2, 1]**

The first term is ten. In the next part, we must identify the smallest term from the remaining array. The smallest term from **5**, **2**, and **1** is **1**. So, we replace the term** 10** with **1**.

The new array is **[1, 5, 2, and 10]** Again, this process would be repeated.

Finally, we get arranged array as **[1, 2, 5, and 10]**.

Let us move on with this article to Selection Sort in C and see how the selection sort algorithm works,

## Algorithm for Selection Sort:

Step 1 − Place min to the first point

Step 2 − Identify the minimum number in the array

Step 3 – compare the first point with the smallest value in the array

Step 4 – Give the second term as min.

Step 5 − Repeat the whole process until we get an arranged array.

Now, let us have a closure look at the code for the programmatic implementation,

## Selection sort Program in c:

#include <stdio.h> int main() { int a[100], n, i, j, position, swap; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d Numbersn", n); for (i = 0; i < n; i++) scanf("%d", &a[i]); for(i = 0; i < n - 1; i++) { position=i; for(j = i + 1; j < n; j++) { if(a[position] > a[j]) position=j; } if(position != i) { swap=a[i]; a[i]=a[position]; a[position=swap; } } printf("Sorted Array:n"); for(i = 0; i < n; i++) printf("%dn", a[i]); return 0; }

**Output:**

- C:\WINDOWS\SYSTEM32\cmd.exe

Enter the number of elements

** Enter 4 Numbers**

4

2

7

1

**Sorted Array:**

1

2

4

7

(Program exited with code: 0)

In the above-mentioned code, we first take the number of elements from the user and store it in n. Then the user needs to put the array. The array is accepted and stored in a [].

The first ‘for loop’ takes care of the term to be matched exactly. It gives i to the placed variable. The inner ‘for loop’ is used to iterate through the remaining terms and identify the minimum term. Once the minimum term is identified the j is assigned to the placed variable. j takes care of the index of the minimum term. Then, it is checked again if the placed variable is not equal to i. If it is not equal, then comparing happens by using a swap variable.

**Conclusion:**

That’s all about selection sort in c, If you still got any questions you can ask in the comments box.