Home » Selection Sort

# Selection Sort Algorithm

In this article, we will discuss the Selection sort Algorithm. The working procedure of selection sort is also simple. This article will be very helpful and interesting to students as they might face selection sort as a question in their examinations. So, it is important to discuss the topic.

In selection sort, the smallest value among the unsorted elements of the array is selected in every pass and inserted to its appropriate position into the array. It is also the simplest algorithm. It is an in-place comparison sorting algorithm. In this algorithm, the array is divided into two parts, first is sorted part, and another one is the unsorted part. Initially, the sorted part of the array is empty, and unsorted part is the given array. Sorted part is placed at the left, while the unsorted part is placed at the right.

In selection sort, the first smallest element is selected from the unsorted array and placed at the first position. After that second smallest element is selected and placed in the second position. The process continues until the array is entirely sorted.

The average and worst-case complexity of selection sort is O(n2), where n is the number of items. Due to this, it is not suitable for large data sets.

Selection sort is generally used when –

• A small array is to be sorted
• Swapping cost doesn’t matter
• It is compulsory to check all elements

Now, let’s see the algorithm of selection sort.

## Working of Selection sort Algorithm

Now, let’s see the working of the Selection sort Algorithm.

To understand the working of the Selection sort algorithm, let’s take an unsorted array. It will be easier to understand the Selection sort via an example.

Let the elements of array are – Now, for the first position in the sorted array, the entire array is to be scanned sequentially.

At present, 12 is stored at the first position, after searching the entire array, it is found that 8 is the smallest value. So, swap 12 with 8. After the first iteration, 8 will appear at the first position in the sorted array. For the second position, where 29 is stored presently, we again sequentially scan the rest of the items of unsorted array. After scanning, we find that 12 is the second lowest element in the array that should be appeared at second position. Now, swap 29 with 12. After the second iteration, 12 will appear at the second position in the sorted array. So, after two iterations, the two smallest values are placed at the beginning in a sorted way. The same process is applied to the rest of the array elements. Now, we are showing a pictorial representation of the entire sorting process. Now, the array is completely sorted.

## Selection sort complexity

Now, let’s see the time complexity of selection sort in best case, average case, and in worst case. We will also see the space complexity of the selection sort.

### 1. Time Complexity

CaseTime Complexity
Best CaseO(n2)
Average CaseO(n2)
Worst CaseO(n2)
• Best Case Complexity – It occurs when there is no sorting required, i.e. the array is already sorted. The best-case time complexity of selection sort is O(n2).
• Average Case Complexity – It occurs when the array elements are in jumbled order that is not properly ascending and not properly descending. The average case time complexity of selection sort is O(n2).
• Worst Case Complexity – It occurs when the array elements are required to be sorted in reverse order. That means suppose you have to sort the array elements in ascending order, but its elements are in descending order. The worst-case time complexity of selection sort is O(n2).

### 2. Space Complexity

 Space Complexity O(1) Stable YES
• The space complexity of selection sort is O(1). It is because, in selection sort, an extra variable is required for swapping.

## Implementation of selection sort

Now, let’s see the programs of selection sort in different programming languages.

Program: Write a program to implement selection sort in C language.

Output:

After the execution of above code, the output will be – Program: Write a program to implement selection sort in C++ language.

Output:

After the execution of above code, the output will be – Program: Write a program to implement selection sort in C# language.

Output: Program: Write a program to implement selection sort in python.

Output: Program: Write a program to implement selection sort in Java.

Output: Program: Write a program to implement selection sort in PHP.

Output:

After the execution of above code, the output will be – So, that’s all about the article. Hope the article will be helpful and informative to you.

This article was not only limited to the algorithm. We have also discussed the Selection sort complexity, working, and implementation in different programming languages.

Next TopicShell Sort