Selection sort program in C#
Selection sort is a simple sorting algorithm that repeatedly finds the minimum element from the unsorted part of the array and places it at the beginning of the sorted part. It works by dividing the array into two parts: the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty, and the entire array is unsorted.
Here's how the selection sort algorithm works step by step:
-
Find the minimum element in the unsorted part of the array.
-
Swap the minimum element with the leftmost element of the unsorted part (i.e., the first element of the unsorted part becomes the minimum element).
-
Expand the sorted part by one element (i.e., move the boundary between the sorted and unsorted parts one element to the right).
-
Repeat steps 1-3 until the entire array is sorted.
Here's an example implementation of selection sort in C#:
public class SelectionSort
{
public static void Main()
{
int[] arr = { 64, 25, 12, 22, 11 };
Console.WriteLine("Original array:");
PrintArray(arr);
Sort(arr);
Console.WriteLine("Sorted array:");
PrintArray(arr);
}
public static void Sort(int[] arr)
{
int n = arr.Length;
// One by one move the boundary of the unsorted subarray
for (int i = 0; i < n - 1; i++)
{
// Find the minimum element in the unsorted part of the array
int minIndex = i;
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[minIndex])
{
minIndex = j;
}
}
// Swap the minimum element with the leftmost element of the unsorted part
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}
Now, let's consider an example to see how selection sort works. Suppose we have the following array of integers:
int[] arr = { 64, 25, 12, 22, 11 };
The step-by-step execution of the selection sort algorithm on this array would be as follows:
-
Initially, the array is: [64, 25, 12, 22, 11]. The first element (64) is considered as the minimum.
-
The minimum element (11) is found at index 4. Swap it with the first element. The array becomes: [11, 25, 12, 22, 64].
-
The sorted part is expanded, and the second element (25) is considered as the minimum.
-
The minimum element (12) is found at index 2. Swap it with the second element. The array becomes: [11, 12, 25, 22, 64].
-
The sorted part is expanded, and the third element (25) is considered as the minimum.
-
The minimum element (22) is found at index 3. Swap it with the third element. The array becomes: [11, 12, 22, 25, 64].
-
The sorted part is expanded, and the fourth element (25) is considered as the minimum.
-
The fourth element (25) is already the minimum. No swap is required.
-
The sorted part is expanded, and the fifth element (64) is considered as the minimum.
-
The fifth element (64) is already the minimum. No swap is required.
In this program, the Sort method implements the selection sort algorithm. The Main method initializes an array, calls the Sort method to sort the array, and then prints both the original and sorted arrays using the PrintArray method.
When you run the program, it will output:
Original array:
64 25 12 22 11
Sorted array:
11 12 22 25 64
Selection sort has a time complexity of O(n^2) in all cases, where n is the number of elements in the array. Although it is an inefficient algorithm for large amount of data.