Selection Sort Program with Explanation and Code
2025-11-19
selection sort
sorting algorithm
simple sorting
beginner sorting program
array sorting
logical programs
c selection sort
python selection sort
javascript selection sort
php sorting program
csharp programs
data structures
Selection Sort is a simple sorting algorithm that repeatedly selects the smallest element and swaps it into position. This page includes a clear explanation, step-by-step logic, and full code examples in Python, C, JavaScript, PHP, and C#. Ideal for beginners and interview preparation.
Program Explanation
What is Selection Sort?
Selection Sort is a simple comparison-based sorting algorithm. It repeatedly selects the smallest (or largest) element from the unsorted portion of the array and swaps it with the first unsorted element. This process continues until the entire array becomes sorted.
Steps to Solve
- Start with the first element of the array.
- Find the minimum element in the remaining unsorted portion.
- Swap the found minimum value with the current element.
- Move the boundary of the sorted section one step forward.
- Repeat the process for all array elements.
- Stop when the entire array is sorted.
Key Points
- Selection Sort performs well on small lists.
- Time complexity: O(n²) in all cases.
- Space complexity: O(1) (in-place sorting).
- Not a stable sorting algorithm.
- Easy to understand and implement.
Program Code
arr = list(map(int, input("Enter numbers: ").split()))
for i in range(len(arr)):
min_index = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[min_index]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]
print("Sorted array:", arr)
#include <stdio.h>
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
for (int i = 0; i < n - 1; i++) {
int min_index = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_index])
min_index = j;
}
int temp = arr[min_index];
arr[min_index] = arr[i];
arr[i] = temp;
}
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
let arr = prompt("Enter numbers:").split(" ").map(Number);
for (let i = 0; i < arr.length; i++) {
let min_index = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[min_index]) {
min_index = j;
}
}
[arr[i], arr[min_index]] = [arr[min_index], arr[i]];
}
console.log("Sorted array:", arr);
using System;
class Program {
static void Main() {
Console.Write("Enter numbers: ");
int[] arr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
for (int i = 0; i < arr.Length - 1; i++) {
int min_index = i;
for (int j = i + 1; j < arr.Length; j++) {
if (arr[j] < arr[min_index])
min_index = j;
}
int temp = arr[min_index];
arr[min_index] = arr[i];
arr[i] = temp;
}
Console.Write("Sorted array: ");
foreach (int num in arr)
Console.Write(num + " ");
}
}
<?php
$arr = explode(" ", readline("Enter numbers: "));
$n = count($arr);
for ($i = 0; $i < $n - 1; $i++) {
$min_index = $i;
for ($j = $i + 1; $j < $n; $j++) {
if ($arr[$j] < $arr[$min_index]) {
$min_index = $j;
}
}
$temp = $arr[$min_index];
$arr[$min_index] = $arr[$i];
$arr[$i] = $temp;
}
echo "Sorted array: " . implode(" ", $arr);
?>
