Selection Sort
- Shreyas Naphad
- May 3, 2024
- 1 min read
Updated: Jun 5, 2024
Sorting is one of the most important techniques in Computer Science and in this article, we will be learning one of the most common sorting techniques which is the Selection Sort algorithm.
Selection sort can be considered like picking cards from a deck one by one and putting them in order. You repeatedly find the smallest card and place it in the correct position until all cards are sorted.
The Problem Statement is as follows:
Given an array of N integers, sort the elements of the array using the Selection Sorting algorithm.
Example:
Input: N = 5, array[] = {5,7,0,12,45}
Output: 0,5,7,12,45
Selection Sort Algorithm:
Start at the beginning of the array.
Find the smallest element from the unsorted part of the array.
Swap this smallest element with the first unsorted element.
Move to the next position in the array and repeat steps 2 and 3.
This process continues until all elements are in sorted order.
Selection Sort Code in C++
Time complexity: O(N^2)
Space Complexity: O(1)
Comments