**Introduction **

Most programming languages have a built-in sort function, but we need to understand the sorting algorithms to understand the code effectively. The algorithm which we are going to explore in this blog is Selection Sort in C/C++/Python/Java.

A selection sort algorithm sorts the elements by iterating over the entire array. It selects the smallest element from the unsorted array and swaps it with the element present at the first index.

It again finds the next smallest element from the unsorted array and swaps it with the element at the second index. This keeps going on until we achieve our resultant sorted array

Let’s understand the concept in different programming languages.

Selection Sort is an easy way for computers to sort a list of numbers. Suppose you have a deck of cards and want to arrange them from smallest to largest. With Selection Sort, the computer analyzes the cards individually, finding the smallest and putting them first. Then, it finds the next smallest card from the remaining ones and puts it in the second position. It keeps doing this until all the cards are in the correct order. It is like finding the smallest card each time and putting it in its proper place.

So to know how computers figure the smallest and the most significant number, let’s look at the pseudo-code.

```
function selectionSort(array, size)
// Iterating over the entire array from 0 to size - 2(0 - Based Indexing)
for i = 0 to size - 2
smallest = array[i]
for j = i+1 to size - 1
if array[j] < smallest
smallest = array[j]
smallest_index = j
swap(array[i],array[smallest_index])
return array
```

The pseudocode mentioned above conveys the working of how a code will run in the selection sort:

- It sets the smallest number to be the first element in the unsorted section of the array. Initially, the whole array is unsorted i.e the first element of the array.
- It looks through the entire unsorted section of the array and then finds the smallest number.
- It will swap the value with the item at the beginning index i.e first element of the unsorted section, which increases the size of the sorted section by 1 and at the same time decreases the size of the unsorted section by 1.

## Algorithm for Selection Sort in C

- First, start with an unsorted array of elements.
- Find the smallest element in the unsorted portion of the array.
- Change the smallest element with the first element of the unsorted portion. It effectively moves it to the sorted part of the array.
- Expand the sorted portion of the array by one element and reduce the unsorted portion by one element.
- Repeat steps 2-4 until the entire array is sorted.
- The array is now sorted in ascending order.