### 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)
- Frequency Domain Filtering(8)
- Color Image Processing(8)
- C# Data Structures And Algorithms(6)
- Image Noise(6)
- Grayscale Morphology(5)
- Thresholding(4)
- Sorting Algorithms(4)
- Mean Filters(4)
- Order-Statistic Filters(4)
- Morphological Reconstruction(3)
- Edge Detection(3)
- Simple Lists(2)
- Adaptive Filters(2)
- RGB to HSI Color Model(2)
- Tone and Color Corrections(2)
- Landing Pages(1)
- Point Detection(1)
- Line Detection(1)
- Social Games(1)
- Region Growing Segmentation(1)
- Region Splitting And Merging(1)
- Region Segmentation With K Means Clustering(1)
- Region Segmentation Using Superpixels(1)
- C# Arrays(1)
- C# Basics(1)
- Bandreject Filters(1)
- Bandpass filters(1)
- Notch Filters(1)
- Intensity Slicing and Color Coding(1)
- Color Slicing(1)
- Histogram Processing Color Images(1)
- Color Image Smoothing And Sharpening(1)
- Using Color In Image Segmentation(1)
- Digital Image Watermarking(1)

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

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

###
How To Make SLIC Superpixel Algorithm With C#

###
How To Make K Means Clustering Algorithm With C#

###
How To Make Region Splitting And Merging Algorithm – C#

###
How To Make Region Growing Algorithm With C#

###
How To Make Adaptive Thresholding Algorithm With C#

###
How To Make Multilevel Thresholding Algorithm With C#

###
How To Make Otsu Thresholding Algorithm With C#

###
How To Make A Basic Global Thresholding Algorithm – C#

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.

Region Segmentation Using Superpixels

SLIC superpixel segmentation is a modern operation for reducing irrelevant detail for shortening computational time in further processing.

Region Segmentation With K Means Clustering

K means clustering is a optimization method of partitioning an image by measuring Euclidean distances between pixels and cluster means.

Region Splitting And Merging

Region splitting and merging is a texture segmentation operation, where we use descriptors such as local mean intensity and standard deviation

Region Growing Segmentation

Region growing segmentation is a process, with which we can extract regions from image based on the properties of pixels inside them.

Thresholding

Adaptive thresholding operation computes thresholds for each pixel locally and therefore segments images more accurately.

Thresholding

Multilevel thresholding is an extension of Otsu's method of thresholding, which basically works for an arbitrary number of thresholds.

Thresholding

Otsu thresholding is a global thresholding method, with which we find optimal threshold intensity to ensure the maximum separability.

Thresholding

This tutorial demonstrates how to get optimal threshold value for basic global thresholding operation for segmentation in image processing.

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.

## Comments