### How to Selection Sort Algorithm in C# Made Easy

Selection sort algorithm is one of the simplest sorting algorithms out there. You can use it to sort different data type arrays.

Filter by Category

- C# Tutorial(85)
- C# Image Processing(69)
- Morphological Processes(20)
- Image Processing(16)
- Image Restoration and Reconstruction(16)
- Image Segmentation(13)
- C# Data Structures And Algorithms(11)
- Color Image Processing(8)
- Frequency Domain Filtering(8)
- Image Noise(6)
- Grayscale Morphology(5)
- Thresholding(4)
- Order-Statistic Filters(4)
- Mean Filters(4)
- Sorting Algorithms(4)
- Morphological Reconstruction(3)
- Edge Detection(3)
- Simple Lists(2)
- RGB to HSI Color Model(2)
- Adaptive Filters(2)
- Tone and Color Corrections(2)
- Linked Lists(2)
- Stacks(1)
- Queues(1)
- Point Detection(1)
- Line Detection(1)
- C# Arrays(1)
- Region Segmentation Using Superpixels(1)
- Region Segmentation With K Means Clustering(1)
- Region Splitting And Merging(1)
- Sorted Lists(1)
- Region Growing Segmentation(1)
- Digital Image Watermarking(1)
- Using Color In Image Segmentation(1)
- Social Games(1)
- Bandreject Filters(1)
- Bandpass filters(1)
- Notch Filters(1)
- Landing Pages(1)
- Intensity Slicing and Color Coding(1)
- Color Slicing(1)
- Histogram Processing Color Images(1)
- Color Image Smoothing And Sharpening(1)
- C# Basics(1)

Back to Latest Articles
###
How to Selection Sort Algorithm in C# Made Easy

###
C# Arrays And Everything You Need To Know About Them

Sorting Algorithms

Selection sort algorithm is one of the simplest sorting algorithms out there. You can use it to sort different data type arrays.

C# Basics

C# arrays are data structures for storing multiple items of the same type. This guide shows you how to use different varieties of arrays.

How to Selection Sort Algorithm in C# Made...

Selection sort is just one of many operations out there that we can use on arrays. In general, what sorting algorithms are good for, is arranging items in a ascending or descending order. However, different sorting algorithms present us with different efficiencies.

I will dedicate this tutorial only to selection sort algorithm using C# programming, since it’s one of the simplest sorting algorithms out there.

In essence, this algorithm divides the array into 2 parts, sorted and unsorted. Furthermore, through each iteration, it takes a look at the items left in the unsorted part.

In order to sort the items, it picks either the lowest or highest item from unsorted section and exchanges it with the first item in the sorted array. Whether it picks the lowest or highest value with each iteration depends on the order we want to sort.

It’s a pretty simple concept, but you need to keep in mind that it doesn’t come without a drawback. In order to sort the entire array, it takes as many iterations through it as there are items inside. Therefore, it makes it very inefficient when we’re working with arrays that hold a large number of items.

In case that explanation above isn’t quite clear enough, I’m going to implement it by first creating static class SelectionSort.

```
public static class SelectionSort
{
public static void Sort<T>(T[] array) where T : IComparable
{
for (int i = 0; i < array.Length - 1; i++)
{
int minIndex = i;
T minValue = array[i];
for (int j = i + 1; j < array.Length; j++)
{
if (array[j].CompareTo(minValue) < 0)
{
minIndex = j;
minValue = array[j];
}
}
Swap(array, i, minIndex);
}
}
private static void Swap<T>(T[] array, int first, int second)
{
T temp = array[first];
array[first] = array[second];
array[second] = temp;
}
}
```

As you can see from the code above, we created a static class, which holds 2 methods for the whole operation.

First of all, the Sort method is a generic static method, which takes 1 parameter, the array we want to sort. Secondly, this method can only sort data types that IComparable interface accepts. In other words, we can use this same method on different data type arrays.

The second method in the class is meant to, as it’s name suggests, swap values within array when we’re sorting it. Therefore, we use it within the first method.

Thank you for visiting my blog and I hope you found this post about selection sort useful.

You can also download the demo project and try it out yourself.