< Nafees />
github
  • Assignments - DSA
  • Long Integer Operations
  • OOP
    • Introduction
    • Implementation
  • Sorting Algorithms
    • Selection Sort
    • Bubble Sort
    • Insertion Sort
    • Shell Sort
    • Shuffle
    • Merge Sort
    • Convex Hull
    • Quick sort
    • System Sort
    • Heap-Sort
  • Binary Search
  • Binary Search By Recursion
  • Two-Pointer
  • String
  • LeetCode 75
    • Array/String
    • Hash Map
    • BST
    • Binary Search
  • Top Interview 150
    • Linked-List
  • Leetcode Programming Skill
    • Math
  • Leetcode 75
  • 🤡Leet Code Extra
  • Arrays
    • 1-D Array
    • 2-D Arrays
  • 👨‍🍳Basic Math - Codechef
  • ☠️Recursion
  • 😁Public Member Functions
    • Strings Functions
  • 👾Linked List
    • What's Linked List & Implementation?
      • Singly Linked List
      • Doubly Linked List
    • Problems
  • 📚Stack
    • What's Stack & Implementation?
      • Stack Using Array
      • Stack using LL
    • Problems
  • 🏧Queue
    • What's Queue & Implementation?
      • Simple Queue
      • Queue using LL
      • Circular Queue
      • Deque using Linked List
      • STL Deque
    • Problems
  • 🏧Priority Queue
    • What's Priority Queue & Implementation
      • OrderedArrayMaxPQ.Java
      • Maximum-Oriented PQ using Binary Heap
      • Minimum-Oriented PQ using Binary Heap
    • Problems
  • 🗓️Hash Table
    • What's Hash Table & Implementation
      • ST - Seperate Chaining
      • ST - Linear Probing
    • Problems
  • 🎴Symbol Table
    • What's Symbol Table and implementation
      • ST Using Binary search (ordered array)
      • ST Using Binary Search Tree
      • ST Using Left-Leaning Red-Black Tree
      • ST Using Hash Table
    • Problems
  • 🔗Union-Find (Dynamic Connectivity problem)
    • What is a Union Find Data Structure?
    • Implementation
  • 🎋Binary Tree
    • What's Binary Tree & Implementation?
      • Traversal
      • Red-Black BST
  • 🌴Trie
    • What's Trie & Implementation?
    • Problems
  • 😎Project
    • Expression Evaluation
Powered by GitBook
On this page

Was this helpful?

  1. Sorting Algorithms

Selection Sort

This sorting algorithm works by repeatedly finding the smallest element in an array and swapping it with the first element.

PreviousSorting AlgorithmsNextBubble Sort

Last updated 10 months ago

Was this helpful?

Use Case

The selection sort algorithm is a simple and efficient sorting algorithm that can be used in various situations. Some of the use cases of selection sort in C++ include:

  • Sorting small arrays. Selection sort is a good choice for sorting small arrays because it is relatively efficient for small inputs.

  • Selection sort does not require a lot of memory, so it can be used in situations where the cost of swapping elements is high.

  • Sorting arrays where the order of identical elements does not matter. Selection sort is a stable sorting algorithm, which means that it does not change the relative order of identical elements in the array.

It is not very efficient for large arrays.

Here is an example of how to implement selection sort in C++:

Question statement::

Notes:

void SelectionSort(int arr[], int n){
    for (int i = 0; i < n-1; i++){
        int minIndex = i; 
        for (int j = i+1; j < n; j++){
            if(arr[minIndex] > arr[j]){ // 0 > j..., 1 > j..., 2 > j..., (n-1) > j
                minIndex = j; // minIndex value will be updated, when the value at arr[minIndex(i)] is greater than the value at arr[j] index
            }
        }
        swap(arr[i], arr[minIndex]); 
    }
}

The time complexity of the selection sort algorithm is O(n^2), where n is the number of elements in the array. This is because the algorithm has two nested loops, and each loop iterates through the entire array.

The space complexity of the selection sort algorithm is O(1) because it does not require any additional memory space apart from a temporary variable used for swapping.

Here is a breakdown of the time complexity of the code:

  • The outer loop iterates through the array n-1 times.

  • The inner loop iterates through the array n-i-1 times.

  • The total number of comparisons is the sum of the number of comparisons made by the outer loop and the inner loop.

  • The sum of the number of comparisons is n(n-1)/2.

  • The time complexity of the code is O(n^2).

n = 5
a | b c d e  = 4 Comparison for a,   (n-1) = 4
a b | c d e  = 3 Comparison for b,   (n-2) = 3
a b c | d e  = 2 Comparison for c,   (n-3) = 2
a b c d | e  = 1 Comparison for d,   (n-4) = 1
1 + 2 + 3 + .... + (n - 2) + (n - 1) 
= n(n-1)/2 
= (n^2 + n)/2 
= O(n^2) // nested loops
https://www.codingninjas.com/studio/problems/selection-sort_981162
https://drive.google.com/file/d/1WtwEvIvqKOUR0Ys0S_SLPjfWzcT-W4_X/view
Page cover image