C# Program for Selection Sort Algorithm
for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) minIndex = j; } int temp = arr[minIndex]; arr[minIndex] = arr[i]; arr[i] = temp; }.Examples
How to Think About It
Algorithm
Code
using System; class Program { static void SelectionSort(int[] arr) { int n = arr.Length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } int temp = arr[minIndex]; arr[minIndex] = arr[i]; arr[i] = temp; } } static void Main() { int[] arr = {5, 3, 8, 4, 2}; SelectionSort(arr); foreach (int num in arr) { Console.Write(num + " "); } } }
Dry Run
Let's trace the array [5, 3, 8, 4, 2] through the selection sort code.
First outer loop (i=0)
Find smallest from index 0 to end: smallest is 2 at index 4. Swap arr[0] and arr[4]. Array becomes [2, 3, 8, 4, 5].
Second outer loop (i=1)
Find smallest from index 1 to end: smallest is 3 at index 1. Swap arr[1] and arr[1] (no change). Array stays [2, 3, 8, 4, 5].
Third outer loop (i=2)
Find smallest from index 2 to end: smallest is 4 at index 3. Swap arr[2] and arr[3]. Array becomes [2, 3, 4, 8, 5].
Fourth outer loop (i=3)
Find smallest from index 3 to end: smallest is 5 at index 4. Swap arr[3] and arr[4]. Array becomes [2, 3, 4, 5, 8].
| Iteration | Array State |
|---|---|
| i=0 | [2, 3, 8, 4, 5] |
| i=1 | [2, 3, 8, 4, 5] |
| i=2 | [2, 3, 4, 8, 5] |
| i=3 | [2, 3, 4, 5, 8] |
Why This Works
Step 1: Find the smallest element
The inner loop uses if (arr[j] < arr[minIndex]) to find the smallest element in the unsorted part.
Step 2: Swap elements
After finding the smallest element, the code swaps it with the element at the current position to place it correctly.
Step 3: Repeat for all elements
The outer loop moves the boundary of the sorted part forward until the entire array is sorted.
Alternative Approaches
using System; using System.Linq; class Program { static void SelectionSort(int[] arr) { int n = arr.Length; for (int i = 0; i < n - 1; i++) { int minValue = arr.Skip(i).Min(); int minIndex = Array.IndexOf(arr, minValue, i); int temp = arr[minIndex]; arr[minIndex] = arr[i]; arr[i] = temp; } } static void Main() { int[] arr = {5, 3, 8, 4, 2}; SelectionSort(arr); foreach (int num in arr) Console.Write(num + " "); } }
using System; class Program { static void SelectionSort(int[] arr, int start = 0) { if (start >= arr.Length - 1) return; int minIndex = start; for (int i = start + 1; i < arr.Length; i++) { if (arr[i] < arr[minIndex]) minIndex = i; } int temp = arr[minIndex]; arr[minIndex] = arr[start]; arr[start] = temp; SelectionSort(arr, start + 1); } static void Main() { int[] arr = {5, 3, 8, 4, 2}; SelectionSort(arr); foreach (int num in arr) Console.Write(num + " "); } }
Complexity: O(n^2) time, O(1) space
Time Complexity
Selection sort uses two nested loops each running up to n times, resulting in O(n^2) time complexity.
Space Complexity
It sorts the array in place, so it uses only a constant amount of extra space, O(1).
Which Approach is Fastest?
The basic selection sort is simple but slow for large arrays; alternatives like quicksort or mergesort are faster but more complex.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Selection Sort | O(n^2) | O(1) | Small or nearly sorted arrays |
| LINQ Minimum | O(n^2) | O(n) | Readable code but less efficient |
| Recursive Selection Sort | O(n^2) | O(n) | Learning recursion, not for large data |