# Selection Sort with Algorithm and Example

This article was written and distributed in order to provide a description of the "selection sort," a method or strategy that is utilized to sort the elements of an array. This article provides an explanation of the "selection sort" algorithm as well as an example of its application. First, let's take a look at what its meaning is.

## What is Selection Sort?

The elements of an array can be sorted into ascending order with the assistance of a method or algorithm known as selection sort. The element with the smallest size will be chosen using this algorithm, and it will be moved to the first position. After that, the element with the second smallest size will be chosen, and it will be moved to the second position, and so on.

## Selection Sort Algorithm

The following is the algorithm that is used in selection sort:

• Find the smallest element in a given unsorted array.
• Put it at the first index of the array (that is, the 0th index, because indexing starts at 0).
• That is, if an array's name is arr[] and its size is 5, then find the smallest element from all 5 elements present at arr[0], arr[1], arr[2],  arr[3], and arr[4], or find the smallest element in arr[0...4].
• And put it at the beginning, at arr[0].
• Find the second-smallest element.
• and put it at the second index of the array. That is at the beginning of arr[1...4] (that will be arr[1]).
• and so on.

## Selection Sort Example

For example, if the user has provided an array with its elements as 30, 0, -78, 67, and 21, then here is a step-by-step sorting of the array elements:

• -78, 0, 30, 67, 21
• -78, 0, 30, 67, 21
• -78, 0, 21, 67, 30
• -78, 0, 21, 30, 67

If the user has entered 5 as the array size and 54, 21, 8, 18, and 3 as the array elements, then the following is a step-by-step swapping of the elements to sort it according to the selection sort technique:

• 3 21 8 18 54
• 3 8 21 18 54
• 3 8 18 21 54
• 3 8 18 21 54

#### Programs Created on Selection Sort

If you want to put this sorting mechanism into action and observe how it performs in practice when sorting the elements of an array, I believe it will be most beneficial for you to go through one of the programs that have been provided below.

Computer Fundamentals Quiz

« Previous Tutorial Next Tutorial »