×

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its appropriate place within the array.

Selection sort iterated through the array and only then finds the smallest element within the iterated array and swaps it with the first element within the list if it is smaller when compared to the first element.

In the next step, it again iterated throughout the array and got the second element and this process repeated until all the elements are sorted.

Selection sort in the C programming language is considered as the assaulting algorithm which works by finding the smallest number from the array and then replacing it to the very first position in the array.

The next array that is to be traversed will then start from the index position to the next position where the smallest of the elements is placed.

Consider an example where the array ‘arr’ consists of ‘n’ number of elements. It needs to be sorted by making use of ‘n-1’ selection passes.

  • Pass the smallest element present within the array when found along with its index ‘position’. After doing so, then swap the arr[0] and arr[position]. This will make sure that arr[0] is sorted and we will be left with ‘n-1’ number of elements that needs to be sorted.
  • After the first step, during the second pass, position of the smallest element present in the subarray arr[n - 1] will be selected. After doing so, then swap the arr[1] and arr[position]. This will make sure that arr[0] and arr[1] is sorted and we will be left with ‘n - 2’ number of elements that needs to be sorted.
  • In the n - 1th pass present, the position of the smaller element will be between the index numbers arr[n - 1] and arr[n - 2] which needs to be found. Then later swap the arr[position] and arr[n - 1].

Therefore, by following the above explained process, the elements arr[0], arr[1], arr[2],...., arr[n-1] are sorted.

Algorithm for selection sort:

  • Find the minimum element in the array and swap it with the element in the 1st position.
  • Find the minimum element again in the remaining array[2, n] and swap it with the element at 2nd position. Now we have two elements at their correct positions.
  • We have to do this n-1 times to sort the array.

E.g.:

#include<stdio.h>  
int smallest(int[],int,int);  
void main ()  
{  
int a[10] = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};  
int i,j,k,pos,temp;  
for(i=0;i<10;i++)  
{  
pos = smallest(a,10,i);  
temp = a[i];  
a[i]=a[pos];  
a[pos] = temp;  
}  
printf("\n Printing the sorted elements using selection sort \n");  
for(i=0;i<10;i++)  
{  
printf("%d\n",a[i]);  
}  
}  


int smallest(int a[], int n, int i)  
{  
int small,pos,j;  
small = a[i];  
pos = i;  
for(j=i+1;j<10;j++)  
{  
if(a[j]<small)  
{  
small = a[j];  
pos=j;  
}  
}  
return pos;  
} 

Output:

Printing the sorted elements using selection sort:
7
9
10
12
23
23
34
44
78
101

Time complexities:

  • Worst case complexity: if we want to sort in ascending order and the array is in descending order then, the worst case occurs. It equates to O(n2)
  • Best case complexity: best case always occurs when the array is already sorted. It equates to O(n2)
  • Average case complexity: It occurs when the elements of the array are in jumbled order that is either ascending order or descending order. It equates to O(n2)

The time complexity is always the same in all three cases. At each and every step, one has to find the minimum element and should put it in the correct position.

The least or the minimum element is not known until the end of the array is reached.

Space complexity:

Space complexity is always O(1) as the temporary variable is used.

Applications:

  • Selection sort is used when a small list needs to be sorted.
  • It is opted when the cost of swapping does not matter.
  • It can also be used when the checking of all the elements is necessary
  • The cost of writing the memory matters such as the flash memory.

Related Topics

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

If else Programs in C Programming

If else Programs in C programming The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in...

4 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

3 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.