Write a C# Program to Perform Selection Sort ?
Here, we will learn how to perform Selection sort in C#
Selection sort is an algorithm of sorting an array where it loop from the start of the loop, and check through other elements to find the minimum value. After the end of the first iteration, the minimum value is swapped with the current element. The iteration then continues from the 2nd element and so on.
Program:
/*
* C# Program to Perform a Selection Sort
*/
using System;
class Program
{
static void Main(string[] args)
{
int array_size = 10;
int[] array = new int[10] { 100, 50, 20, 40, 10, 60, 80, 70, 90, 30 };
Console.WriteLine("The Array Before Selection Sort is: ");
for (int i = 0; i < array_size; i++)
{
Console.WriteLine(array[i]);
}
int tmp, min_key;
for (int j = 0; j < array_size - 1; j++)
{
min_key = j;
for (int k = j + 1; k < array_size; k++)
{
if (array[k] < array[min_key])
{
min_key = k;
}
}
tmp = array[min_key];
array[min_key] = array[j];
array[j] = tmp;
}
Console.WriteLine("The Array After Selection Sort is: ");
for (int i = 0; i < 10; i++)
{
Console.WriteLine(array[i]);
}
Console.ReadLine();
}
}
Output:
The Array Before Selection Sort is :
100
50
20
40
10
60
80
70
90
30
The Array After Selection Sort is :
10
20
30
40
50
60
70
80
90
100
Here, we will learn how to perform Selection sort in C#
Selection sort is an algorithm of sorting an array where it loop from the start of the loop, and check through other elements to find the minimum value. After the end of the first iteration, the minimum value is swapped with the current element. The iteration then continues from the 2nd element and so on.
Program:
/*
* C# Program to Perform a Selection Sort
*/
using System;
class Program
{
static void Main(string[] args)
{
int array_size = 10;
int[] array = new int[10] { 100, 50, 20, 40, 10, 60, 80, 70, 90, 30 };
Console.WriteLine("The Array Before Selection Sort is: ");
for (int i = 0; i < array_size; i++)
{
Console.WriteLine(array[i]);
}
int tmp, min_key;
for (int j = 0; j < array_size - 1; j++)
{
min_key = j;
for (int k = j + 1; k < array_size; k++)
{
if (array[k] < array[min_key])
{
min_key = k;
}
}
tmp = array[min_key];
array[min_key] = array[j];
array[j] = tmp;
}
Console.WriteLine("The Array After Selection Sort is: ");
for (int i = 0; i < 10; i++)
{
Console.WriteLine(array[i]);
}
Console.ReadLine();
}
}
Output:
The Array Before Selection Sort is :
100
50
20
40
10
60
80
70
90
30
The Array After Selection Sort is :
10
20
30
40
50
60
70
80
90
100
using System;
class SelectionSort {
static void Main(string[] args) {
int[] arr = { 64, 25, 12, 22, 11 };
Console.WriteLine("Original array:");
PrintArray(arr);
SelectionSortFunc(arr);
Console.WriteLine("\nSorted array:");
PrintArray(arr);
}
static void SelectionSortFunc(int[] arr) {
int n = arr.Length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
int minIndex = i;
for (int j = i + 1; j < n; j++)
if (arr[j] < arr[minIndex])
minIndex = j;
// Swap the found minimum element with the first element
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
static void PrintArray(int[] arr) {
foreach (int value in arr)
Console.Write(value + " ");
Console.WriteLine();
}
}