Selection sort is a simple algorithm that works by repeatedly finding the minimum element from the unsorted part of an array and swapping it with the first element.
Here is the code for selection sort in C#:-
using System;
public class Program
{
public static void Main()
{
int[] arr = {64, 25, 12, 22, 11};
SelectionSort(arr);
Console.WriteLine("Sorted array:");
foreach (int num in arr)
{
Console.Write(num + " ");
}
}
public static void SelectionSort(int[] arr)
{
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[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
}
}
}
This selection sort is a fairly intuitive sorting algorithm, though not necessarily efficient. Into this process, the smallest element is first located and switched with the element at subscript zero, thereby placing the smallest element in the first position.
The smallest element remaining in the subarray is then located next to subscripts 1 through n-1 and switched with the element at subscript 1, thereby placing the second smallest element in the second position. The steps are repeated in the same manner till the last element.
OR
The Selection sort is conceptually the simplest sorting algorithm. These algorithms will find first the smallest element in the array and swap it with the element in the first position, then it will find the second smallest element and exchange it with second position's element in that series, and It will keep doing this until the entire table is sorted. This is called selection sort because it repeatedly selects the next-smallest element and swaps it into the right place.
Algorithm for selection sort
selectionSort(array, size)
repeat (size - 1) times
set the first unsorted element as the minimum
for each of the unsorted elements
if element < currentMinimum
set element as new minimum
swap minimum with first unsorted position
end selectionSort
Example (1).
#include <stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void selectionSort(int array[], int size)
{
for (int step = 0; step < size - 1; step++)
{
int min_idx = step;
for (int i = step + 1; i < size; i++)
{
if (array[i] < array[min_idx])
min_idx = i;
}
swap(&array[min_idx], &array[step]);
}
}
void printArray(int array[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("%d ", array[i]);
}
printf("\n");
}
int main()
{
int data[] = {20, 12, 10, 15, 2};
int size = sizeof(data) / sizeof(data[0]);
selectionSort(data, size);
printf("Sorted array in Acsending Order:\n");
printArray(data, size);
}
Liked By
Write Answer
How does a selection sort work for an array?
Join MindStick Community
You have need login or register for voting of answers or question.
Aryan Kumar
06-Apr-2023Selection sort is a simple algorithm that works by repeatedly finding the minimum element from the unsorted part of an array and swapping it with the first element.
Here is the code for selection sort in C#:-
Output:-
Sorted array: 11 12 22 25 64
Anonymous User
11-Sep-2019This selection sort is a fairly intuitive sorting algorithm, though not necessarily efficient. Into this process, the smallest element is first located and switched with the element at subscript zero, thereby placing the smallest element in the first position.
The smallest element remaining in the subarray is then located next to subscripts 1 through n-1 and switched with the element at subscript 1, thereby placing the second smallest element in the second position. The steps are repeated in the same manner till the last element.
OR
The Selection sort is conceptually the simplest sorting algorithm. These algorithms will find first the smallest element in the array and swap it with the element in the first position, then it will find the second smallest element and exchange it with second position's element in that series, and It will keep doing this until the entire table is sorted. This is called selection sort because it repeatedly selects the next-smallest element and swaps it into the right place.
Algorithm for selection sort selectionSort(array, size)
repeat (size - 1) times
set the first unsorted element as the minimum
for each of the unsorted elements
if element < currentMinimum
set element as new minimum
swap minimum with first unsorted position
end selectionSort
Example (1).