Home Arrays Selection Sort

# Selection Sort

0 comment

As the name suggests SELECTION SORT involves selecting an element. Now the question arises, as to how should we select this element. What is the criteria? Where should we put it? All these answers are given in the algorithm for SELECTION SORT.

In selection sort, what we do is:-

1. Start from the first position in the array.
2. Traverse the remaining array to find the smallest number
3. Swap this smallest number with the number we selected in the first place.
4. Repeat steps 2 and 3 with the next position.

Let us try to understand this algorithm with the help of this diagram.

As we can see we have an unsorted array in the first step. The red blocks represent sorted elements and the blue elements represent the sorted ones.

• We start with the element 7 and then scan the remaining array for the minimum element.
• 1 is the minimum element and thus we swap 1 and 7.
• Now we start with the second element that is 4, and scan the remaining array for the minimum element.
• 2 is the minimum element and thus we swap 2 and 4.
• Similarly we swap 5 and 4
• This process continues and finally our complete array is sorted.

Here is an implementation of the above algorithm.

#### Code:

.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
.hljs > mark.shcb-loc { background-color: #ddf6ff; }#include<stdio.h>

// function to swap two integers
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}

//defining a function to perform selection sort on array arr[] of given size
void selectionSort(int arr[], int size)
{
int i,j;

for(i = 0; i < size; i++)
{
//a variable to store the position with minimum element
int min_pos = i;

//we need to check the remaining elements
for(j = i + 1; j < size; j++)
{
//compare each element and get the minimum element's position
if(arr[j] < arr[min_pos])
{
min_pos = j;
}
}

//now 'min_pos' contains the position with minimum element
//so we swap the elements
swap(&arr[i], &arr[min_pos]);
}
}

// driver function to test the above function
int main(void)
{
int i;
int arr = {3, 4, 7, 1, 10, 8, 2, 22, 99, 50};

selectionSort(arr,10);

printf("SORTED array:- ");
for(i = 0; i < 10; i++)
printf("%d ",arr[i]);

return 0;
}
Code language: C++ (cpp)
Time Complexity: O(n^2)
Space Complexity: O(1)

The run-time of this algorithm is always O(n2) in any case. But upon analysis we can also say that this algorithm always performs swaps in the order of O(n).

Thus this type of algorithm can be helpful when we need to sort few number of elements and the cost of swapping elements is very high.

#### You may also like

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More