Programmingempire
This article explains Selection Sort in C#. Like Bubble Sort and Insertion Sort, Selection Sort is also a simple sorting technique. Furthermore, selection sort also works by dividing the list into sorted and unsorted part. Unlike, insertion sort, selection sort finds minimum element from the unsorted list and puts it at the beginning. Further, the element at beginning goes to the position of the minimum element. Therefore, now the first element also becomes the part of sorted list. Initially, the whole list is unsorted. So, this algorithm finds the minimum from the whole list and swaps it with the element at first position. After that, the sorted list contains one element and unsorted list contains n-1 elements, where n is the total number of elements. The same process is repeated for rest of the ellements. The following example demonstrates the working of selection sort.
Working of Selection Sort
Let, our list is [9, 1, 22, 5, 18, 12]. At first, unsorted part= [9, 1, 22, 5, 18, 12] , and sorted part=[]. So, we find minimum from the whole list = 1. It results in swapping the elements 1 and 9. Now the list becomes [1, 9, 22, 5, 18, 12]. Further, find the minimum from the unsorted part. So, minimum = 5. After that swap it with the element 9. So, now we get the following list = [1, 5, 22, 9, 18, 12] . Next we get following list [1, 5, 9, 22, 18, 12]. Similarly, next iteration results in this list: [1, 5, 9, 12, 18, 22]. Although, our list is sorted now. But the last iteration also takes place.
Implementation of Selection Sort in C#
The following code shows Implementation of Selection Sort in C#. While, the inner loop finds minimum element. Also, the swap function swaps the current element with the minimum one.
using System;
namespace SelectionSort
{
class Program
{
static void Main(string[] args)
{
int[] myarray = { 4, 1, 9, -13, 90, 56, 81, 34, -2, -15, 60, 88 };
Console.WriteLine("Array before sorting...");
foreach (int x in myarray)
Console.Write(x + " ");
Sort.SelectionSort(myarray);
Console.WriteLine("\nArray after sorting...");
foreach (int x in myarray)
Console.Write(x + " ");
Console.WriteLine();
}
}
class Sort
{
public static void SelectionSort(int[] myarray)
{
int min_index;
for (int i = 0; i < myarray.Length - 1; i++)
{
min_index = i;
for (int j = i + 1; j < myarray.Length; j++)
{
if (myarray[j] < myarray[min_index])
{
min_index = j;
}
}
swap(ref myarray[i], ref myarray[min_index]);
}
}
static void swap(ref int m, ref int n)
{
int t = m;
m = n;
n = t;
}
}
}
Output
Further Reading
How to Create Instance Variables and Class Variables in Python
Comparing Rows of Two Tables with ADO.NET
Example of Label and Textbox Control in ASP.NET
One Dimensional and Two Dimensuonal Indexers in C#
Private and Static Constructors in C#
Programs to Find Armstrong Numbers in C#
One Dimensional and Two Dimensional Indexers in C#
Generic IList Interface and its Implementation in C#
Creating Navigation Window Application Using WPF in C#
Find Intersection Using Arrays
An array of Objects and Object Initializer
Performing Set Operations in LINQ
Data Binding Using BulletedList Control
Understanding the Quantifiers in LINQ
Deferred Query Execution and Immediate Query Execution in LINQ
Examples of Query Operations using LINQ in C#
An array of Objects and Object Initializer
Language-Integrated Query (LINQ) in C#
Examples of Connected and Disconnected Approach in ADO.NET
IEnumerable and IEnumerator Interfaces
KeyValuePair and its Applications
Learning All Class Members in C#
Examples of Extension Methods in C#
How to Setup a Connection with SQL Server Database in Visual Studio
Understanding the Concept of Nested Classes in C#
A Beginner’s Tutorial on WPF in C#
Explaining C# Records with Examples
Everything about Tuples in C# and When to Use?
Linear Search and Binary search in C#
Examples of Static Constructors in C#