×

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

Given a Perfect Binary Tree, Reverse Alternate Levels

Implementation //writing a program in C++ language to see how to approach it. #include <bits/stdc++.h> using namespace std; // creating a tree node. struct Nod { char ky; struct Nod *Lft, *Rt; }; // creating a new utility function...

9 minutes read.

Recaman’s Sequence

Recamán's succession repeat connection in arithmetic and software engineering. Since its components are obviously connected with the past components, they are as often as possible characterized utilizing recursion. It takes its...

4 minutes read.

Given a Binary Tree, Print the Pre-order Traversal in Recursive

Implementation #include <stdio.h> #include <stdlib.h>   /* Creating a binary tree node that consists of some data along with the pointer to the left and right child.  */ struct __nod {     int record;     struct...

4 minutes read.

LCA of binary tree

Implementation //Writing a program to find the lowest common factor in a given binary search tree. #include <iostream> #include <vector> using namespace std; // the very first step is to create a binary tree. struct __nod { int...

8 minutes read.

FIFO approach

FIFO is first in first out approach. It is done for the list of elements in data structures where first element will be deleted after another element ia added to it Here,...

6 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.

Insertion Sort in Data Structures

Insertion Sort in C++ Insertion sort is a sorting algorithm that, in each iteration, installs an unsorted element in its proper position Insertion sort operates in a similar way to how we...

3 minutes read.

All About Minimum Cost Spanning Trees in Data Structure

Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system that stores, manages, and optimizes...

7 minutes read.

2-3 Trees and Basic Operations on them

2-3 Trees, like any other AVL trees or B-trees, are just a type of Height Balanced Tree. 2-3 Trees are the B-trees of order 3. Like every other B-tree, the...

4 minutes read.

Linked List Representation of Binary Tree

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. The word binary itself represents its meaning; we know that...

4 minutes read.

Structure and Union Data Structure

The array is used for the same type of data, but if we want to store a mixed type of data in a group, then the array cannot be used. The Structure...

4 minutes read.

Operations of B++ tree

Insertion When we discuss the insertion operation in the B++ tree, this operation helps us in pushing a new element in the tree at any given place. In this case, the...

17 minutes read.

B Tree in Data Structure

Data management is called database management. A data model is a system that stores, manages, and optimizes computer resources. Data processing is not just about data storage. Almost every app...

9 minutes read.

Difference between complete and full binary tree

As we all know that the  binary tree is a tree it contains one or two children at each other node. It contains two children's nodes in the Binary tree. The...

6 minutes read.

Pairwise swap elements of a given linked list

Pairwise swap elements of a given linked list In this problem, we have given a linked list, and we need to pairwise swap elements of the given linked list. Example:                                     Input:1 ->3...

4 minutes read.

Stack vs Queue: Data Structure

 Difference Between Stack and Queue What is Stack? The LIFO principle applies on insertion and deletion operations of the stack which means last inserted element to the stack will remove first....

3 minutes read.

Circular Linked List

Circular Linked List A circular linked list where all nodes are connected to their next node and last node is connected to the starting node or we can say all nodes...

5 minutes read.

Stack vs Array

Difference between Array and Stack In this article, we are going to discuss the major differences between the stack and array data structures: Array – In the data structure, the array is...

3 minutes read.

Data structure: Infix to Prefix Conversion

Infix to Prefix Conversion In present time, we use the infix expression in our daily life but the computers are not able to understand this format because they need to keep...

4 minutes read.

Insertion Sort vs Selection Sort

In this article, we will discuss insertion sort, Selection sort and the basic differences between these two sorting techniques in detail: What is Insertion Sort? Insertion Sort – The insertion sort is...

5 minutes read.