C Program
#include<stdio.h> int main() { int a[100], n, i, j, min, t; scanf("%d", &n); for(i = 0; i < n; i++) scanf("%d", &a[i]); for(i = 0; i < n-1; i++) { min = i; for(j = i+1; j < n; j++) if(a[j] < a[min]) min = j; t = a[i]; a[i] = a[min]; a[min] = t; } for(i = 0; i < n; i++) printf("%d ", a[i]); }
C Output
Input: 5 3 1 4 5 2 Output: 1 2 3 4 5
C++ Program
#include<iostream> using namespace std; int main() { int a[100], n; cin >> n; for(int i = 0; i < n; i++) cin >> a[i]; for(int i = 0; i < n-1; i++) { int min = i; for(int j = i+1; j < n; j++) if(a[j] < a[min]) min = j; swap(a[i], a[min]); } for(int i = 0; i < n; i++) cout << a[i] << " "; }
C++ Output
Input: 6 9 3 7 1 5 2 Output: 1 2 3 5 7 9
JAVA Program
import java.util.*; class S { public static void main(String[] a) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), i, j, min, t; int[] arr = new int[n]; for(i = 0; i < n; i++) arr[i] = sc.nextInt(); for(i = 0; i < n-1; i++) { min = i; for(j = i+1; j < n; j++) if(arr[j] < arr[min]) min = j; t = arr[i]; arr[i] = arr[min]; arr[min] = t; } for(i = 0; i < n; i++) System.out.print(arr[i] + " "); } }
JAVA Output
Input: 4 5 2 9 1 Output: 1 2 5 9
Python Program
a = list(map(int, input().split())) for i in range(len(a)-1): min = i for j in range(i+1, len(a)): if a[j] < a[min]: min = j a[i], a[min] = a[min], a[i] print(*a)
Python Output
Input: 8 5 1 3 Output: 1 3 5 8
In-Depth Learning – Whole Concept in Paragraphs
What Is Selection Sort?
Selection Sort is a basic comparison-based sorting algorithm. It operates by searching repeatedly for the smallest (or largest) item in the unsorted section of the list and moving it into its proper position. Imagine it as discovering the minimum, then fixing it in place — step by step.
In contrast to Bubble or Insertion Sort, Selection Sort makes fewer swaps, which may be beneficial when swapping is expensive (such as in embedded systems).
How the Code Works
We iterate from the start of the array.
For each index i, we determine the index of the smallest element in the rest of the array (from i+1 to the end).
We swap that smallest element with a[i].
Repeat until everything's sorted.
So, one element is sorted and moved to the beginning after each pass.
Example
We will sort:
[3, 1, 4, 5, 2]
Pass 1: Get min from index 0–4 → 1 → swap with 3 → [1, 3, 4, 5, 2]
Pass 2: Get min from index 1–4 → 2 → swap with 3 → [1, 2, 4, 5, 3]
Pass 3: Locate min from 2–4 → 3 → swap with 4 → [1, 2, 3, 5, 4]
Pass 4: Locate min from 3–4 → 4 → swap with 5 → [1, 2, 3, 4, 5]
Done!
Real-Life Analogy
Think of arranging books by height:
Go through the entire row to look for the shortest book.
Put it in the first position.
Then locate the shortest from the rest, and put it in the second position.
Continue this until all books are lined up — that’s exactly how selection sort works.
Where and When Is It Used?
Selection sort is useful when:
You care more about the number of swaps than comparisons.
Data size is small and simplicity matters more than speed.
You’re implementing sorting in low-memory or embedded systems.
It’s commonly taught in:
Intro to algorithms
Coding interviews (for logic explanation)
Basic data structures classes
Time Complexity
Best, Average, Worst Case: O(n²)
Space Complexity: O(1)
Stability: Unstable by default (unless changed)
The number of comparisons is constant regardless of the initial state of the array.
Python-Specific Benefit
Python enables swapping in one line:
python
a[i], a[min] = a[min], a[i]
This makes it more readable and Selection Sort very easy to implement for students.
SEO-Optimized Natural Paragraph for Ranking
Want to become an expert at Selection Sort in computer programming? This tutorial describes how to apply Selection Sort in C, C++, Java, and Python with straightforward and simple examples. Selection Sort is a very simple sorting algorithm wherein the smallest item is picked from the unsorted region and exchanged with the current position. It's simple to comprehend, very predictable, and very useful in scenarios where swapping is expensive. Whether you're studying for a coding interview, learning core algorithm design, or simply attempting to grasp sorting in bite-sized pieces, this detailed breakdown, with real-world analogies and illustrations, will set you up to learn Selection Sort like a pro.
Social Plugin