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
Filter by Author
Selection sort algorithm is one of the simplest sorting algorithms out there. You can use it to sort different data type arrays.
Posted by Andraz Krzisnik
C# arrays are data structures for storing multiple items of the same type. This guide shows you how to use different varieties of arrays.
Posted by Andraz Krzisnik
SLIC superpixel segmentation is a modern operation for reducing irrelevant detail for shortening computational time in further processing.
Posted by Andraz Krzisnik
K means clustering is a optimization method of partitioning an image by measuring Euclidean distances between pixels and cluster means.
Posted by Andraz Krzisnik
Region splitting and merging is a texture segmentation operation, where we use descriptors such as local mean intensity and standard deviation
Posted by Andraz Krzisnik
Region growing segmentation is a process, with which we can extract regions from image based on the properties of pixels inside them.
Posted by Andraz Krzisnik
Adaptive thresholding operation computes thresholds for each pixel locally and therefore segments images more accurately.
Posted by Andraz Krzisnik
Multilevel thresholding is an extension of Otsu's method of thresholding, which basically works for an arbitrary number of thresholds.
Posted by Andraz Krzisnik
Otsu thresholding is a global thresholding method, with which we find optimal threshold intensity to ensure the maximum separability.
Posted by Andraz Krzisnik
This tutorial demonstrates how to get optimal threshold value for basic global thresholding operation for segmentation in image processing.
Posted by Andraz Krzisnik
Selection sort algorithm is one of the simplest sorting algorithms out there. You can use it to sort different data type arrays.
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.
Gaussian highpass filter processes images in frequency domain. It attenuates low frequencies without creating ringing artifacts.
Rayleigh noise is one of the noise models with which we can simulate data corruption. Guide to making noise from PDF and image histogram.