DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Tools
  3. Algorithm Visualizer
  4. Selection Sort

Selection Sort

Sortingeasy#2

Find minimum element and place it at the beginning repeatedly

Ready

Step 1 of 0

Speed

Input Data

Comma-separated numbers (0-999). Min 2 values.

Algorithm Code

1function selectionSort(arr) {
2 const n = arr.length;
3 for (let i = 0; i < n - 1; i++) {
4 let minIdx = i;
5 for (let j = i + 1; j < n; j++) {
6 if (arr[j] < arr[minIdx]) {
7 minIdx = j;
8 }
9 }
10 [arr[i], arr[minIdx]] = [arr[minIdx], arr[i]];
11 }
12 return arr;
13}
Best

O(n²)

Average

O(n²)

Worst

O(n²)

Space

O(1)

About Selection Sort

Selection Sort divides the input list into a sorted and an unsorted region. It repeatedly selects the smallest element from the unsorted region and moves it to the end of the sorted region. Simple but inefficient for large datasets.

Time Complexity: Best: O(n²), Average: O(n²), Worst: O(n²). Space: O(1).

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.