×

Time Complexity of Selection Sort in Data Structure

What is Time Complexity?

The term “Time complexity” can be defined as the number of times executions made of a particular sequence of instructions and not the total amount of time taken. This is because the total time required also depends on external factors such as the compiler used, processor speed, etc.

There are mainly 3 types of Time Complexities:

Best Time complexity

The best time complexity displays the minimum time taken to compute the lower bound of the algorithm.

Example: In linear search, the best case occurs when the search data is in the first place of the big data. 

Average Time Complexity

In the average time complexity case, we take all random inputs and compute the computation time for all inputs and divide it by the total number of inputs. 

Worst Time Complexity

The worst time complexity defines the input that takes the longest time to execute the algorithm. It calculates the upper bound of the worst-case algorithm.

For example: linear search has the worst case when the search data is in the last place of the big data.

Time Complexity of Selection Sort Algorithm

Selection sort algorithm is used for sorting an array of elements. It works by repeatedly selecting the smallest elements from the unsorted portion of the array and swapping it with the first element of the unsorted portion.

The time complexity of selection sort is O(n2). This implies that the time taken by the algorithm grows quadratic rapidly with the size of the input. This makes selection sort’s efficiency less than other sorting algorithms, such as mergesort and quicksort, which have a time complexity of O(n log n).

Selection sort works by repeatedly selecting the minimum element from the unsorted portion of the array and swapping it with the element at the current position. This process repeats until the whole array gets sorted.

The time complexity of selection sort can be analyzed by considering the number of steps required to sort an array of size n. In the best case complexity, the array is already sorted and the selection sort algorithm takes O(n) time to complete. In the worst case complexity, the array is arranged in reverse order and the selection sort algorithm takes O(n2) time to complete.

In the average case, time complexity of selection sort is also O(n2). This means that, on average, the selection sort algorithm will take O(n2) time to sort an array of size n.

Selection sort is not a very efficient sorting algorithm, especially for large inputs. There are many other sorting algorithms that have a better time complexity, such as quicksort and mergesort, which have a time complexity of O(n log n).This is because the algorithm requires two nested loops to sort the elements in the list. The inner loop is used to find the minimum element in the unsorted part of the list, and the outer loop is used to iterate through the entire list. Since both the loops run in O(n) time, the overall time complexity turns out is O(n2).

Selection sort is not a very efficient sorting algorithm, especially when compared to more advanced algorithms such as quick sort and merge sort. However, selection sort is relatively simple to implement and can be useful in certain situations, such as when the input list is very small or when the list is almost sorted and only requires a few swaps to be completely sorted.

Selection sort has a quadratic time complexity because it uses nested loops to sort the elements in the list. The inner loop iterates over the unsorted portion of the list, while the outer loop iterates over the entire list. This as a result gets a running time of O(n × n) = O(n2).

  • Best case Time Complexity of Selection Sort: O(n)
  • Average case Time Complexity of Selection Sort: O(n2)
  • Worst case Time Complexity of Selection Sort: O(n2)

Algorithm for Selection Sort:

Selection_Sort(arr, size)
start loop and repeat (size - 1) times
set the first unsorted element as the minimum
for each of the unsorted elements
if element< minimum
set element as new_minimum
swap minimum with first unsorted position
endselection_Sort

Pseudo-code for the Selection Sort Algorithm:

procedure selectionSort(A: list of sortable items)
    n = length(A)
fori = 1 to n - 1
minIndex = i
for j = i + 1 to n
if A[j] < A[minIndex]
minIndex = j
swap A[i] and A[minIndex]

Related Topics

Invert binary tree

Invert binary tree is a mirror image of a tree. It is pretty much the same compared to the only difference: its left and right children are swapped with the...

4 minutes read.

Count pairs from two linked lists whose sum is equal to a given value

Count pairs from two linked lists whose sum is equal to a given value In this problem, we have given two linked lists of size n1 and n2 with distinct elements...

4 minutes read.

Why is Binary Heap Preferred over BST for Priority Queue

A priority queue is a linear and ordered collection of elements in which each element has an attribute named priority and the priority attribute decides the order in which elements...

2 minutes read.

Primitive Data Structure in C

The data structure is a logical or mathematical model for organizing and structuring the main memory or elements. We can classify the data structures in two ways one is primitive, and...

10 minutes read.

Dynamic memory allocation of structure 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...

5 minutes read.

Inorder Successor in Binary Trees

The next node in the Inorder traversal of a binary tree is known as Inorder successor of that particular node. In a Binary Search Tree, the definition of Inorder successor can...

9 minutes read.

Remove duplicates from an unsorted Linked List

Remove duplicates from an unsorted Linked List This article will explain how we can remove duplicates from unsorted linked lists. Here we have given an unsorted singly linked list and will...

3 minutes read.

What is Skewed Binary Tree

To understand the skewed binary tree, we must first understand the concept of a binary tree. A binary is generally the one in which every single node has two further...

3 minutes read.

Digital Search Tree in Data Structures

What is a digital search Tree in Data Structures? The Digital search tree is known for its application and diversity in the way it has impacted our world in the field...

3 minutes read.

Interval Tree

Interval Tree Interval Tree: The concept is to increase a Binary Search Tree self-balancing such as Red Black Tree, and AVL Tree, so that every feature can be completed in time O(Logn). Each Interval...

4 minutes read.

Tree in Data Structure

Tree A tree is a non-linear data structure by which hierarchical data is displayed. As we know that there are many trees in the forest, similarly the data structure also contains...

3 minutes read.

Data Structures Tutorial

The data structure is a way of storing and organizing data in a computer system. So that we can use the data quickly, which means the information is stored and...

7 minutes read.

Binary search tree traversal in-order pre-order post-order examples

A binary search tree is a type of non-linear tree in which the tree contains at least two nods. It is called binary because of its nature that states bi...

8 minutes read.

Given a Binary Tree Print the Shortest Path

Implementation // Writing a program in C++ to find the shortest between the nodes i and j.  #include <bits/stdc++.h> using namespace std; // the given function will print the path between nodes i and...

7 minutes read.

Find Bridges in a Graph

You have been given a graph. You have to find out the bridges in that graph. Graph may be connected or disconnected. You have to print vertices of particular edge...

4 minutes read.

Properties of Binary Tree

Trees are maybe of the most significant datum structures. They are used to store and figure out data. A binarytree is a tree data structure made from nodes, all of which has...

3 minutes read.

Find the nth node from the end of a Linked List

Find the nth node from the end of a Linked List In this problem, we have given a singly linked list and a number 'n,' and we need to find the...

3 minutes read.

Program to calculate the area of the circumcircle of an equilateral triangle

You have given one value which represents the side of the equilateral triangle. You have to find out the area of the circumcircle. Let’s take an example - For the above...

3 minutes read.

Linear vs Circular Queue: Data Structure

Difference Between Linear and Circular Queue What is Linear Queue? A linear queue is linear data structure which works on first in first out principle. We can say a linear queue is...

3 minutes read.

Data Structure Prefix to Postfix Conversion

Prefix to Postfix Conversion Prefix: As the name suggests if the operator placed before the operands called the prefix expression.  The form of prefix expression is (operator, operand1, operand2). Example:  *+EF-GH (Infix:...

2 minutes read.