Sort Algorithm in C# - Bubble Sort Explained
I did not like any of the examples of sorting algorithms in C# online. So, I decided to write it by myself with some good code, and not something which looks like it was written by people who have never done commercial software development before.
Good code is readable and maintainable. You can quickly understand what the code does.
This is the easiest sort algorithm.
There are three variations of this algorithm:
(1) The first one uses a while loop to go through the entire list as many times as even a single instance of unsorted items were found:
public static void Type1(List<int> list)
{
Console.WriteLine("Bubble Sorting");
var unsortedFound = false;
do
{
unsortedFound = false;
for (int i = 0; i < list.Count - 1; i++)
{
if (list[i] > list[i + 1])
{
var temp = list[i + 1];
list[i + 1] = list[i]; // Move greater element forward.
list[i] = temp;
unsortedFound = true;
}
}
//An astonishing number of passes (pretty bad efficiency).
}
while (unsortedFound);
}
(2) The second type is slightly more complex. Here, we have a pass variable, which makes sure that each time we go through the list, we don't go through the entire list.
public static void Type2(List<int> list)
{
Console.WriteLine("Bubble Sorting v2.0");
var mismatchFound = false;
//One pass through entire list.
for (int pass = 1; pass < list.Count - 1; pass++)
{
mismatchFound = false;
//Passes with each pass smaller than the older one.
for (int i = 0; i < list.Count - pass; i++)
{
if (list[i] > list[i + 1])
{
var temp = list[i + 1];
list[i + 1] = list[i]; // Move greater element forward.
list[i] = temp;
mismatchFound = true;
}
}
if (!mismatchFound)
{
break;
}
}
}
(3) In cocktail sort, we sort in both directions with each pass, and the iterations in each pass reduces over time.
public static void Cocktail(List<int> list)
{
Console.WriteLine("Cocktail Sort");
var mismatchFound = false;
//Will reduce with each pass
for (int iterationLimit = list.Count - 1; iterationLimit > 0; iterationLimit--)
{
mismatchFound = false;
//From back to front
for (int i = iterationLimit; i > 0; i--)
{
if (list[i - 1] > list[i])
{
var temp = list[i];
list[i] = list[i - 1]; //Bubble forward
list[i - 1] = temp;
mismatchFound = true;
}
}
//From front to back
for (int i = 0; i < iterationLimit; i++)
{
if (list[i] > list[i + 1])
{
var temp = list[i + 1];
list[i + 1] = list[i]; //Bubble forward
list[i] = temp;
mismatchFound = true;
}
}
if (!mismatchFound)
{
break;
}
}
}
Good code is readable and maintainable. You can quickly understand what the code does.
This is the easiest sort algorithm.
There are three variations of this algorithm:
(1) The first one uses a while loop to go through the entire list as many times as even a single instance of unsorted items were found:
public static void Type1(List<int> list)
{
Console.WriteLine("Bubble Sorting");
var unsortedFound = false;
do
{
unsortedFound = false;
for (int i = 0; i < list.Count - 1; i++)
{
if (list[i] > list[i + 1])
{
var temp = list[i + 1];
list[i + 1] = list[i]; // Move greater element forward.
list[i] = temp;
unsortedFound = true;
}
}
//An astonishing number of passes (pretty bad efficiency).
}
while (unsortedFound);
}
(2) The second type is slightly more complex. Here, we have a pass variable, which makes sure that each time we go through the list, we don't go through the entire list.
public static void Type2(List<int> list)
{
Console.WriteLine("Bubble Sorting v2.0");
var mismatchFound = false;
//One pass through entire list.
for (int pass = 1; pass < list.Count - 1; pass++)
{
mismatchFound = false;
//Passes with each pass smaller than the older one.
for (int i = 0; i < list.Count - pass; i++)
{
if (list[i] > list[i + 1])
{
var temp = list[i + 1];
list[i + 1] = list[i]; // Move greater element forward.
list[i] = temp;
mismatchFound = true;
}
}
if (!mismatchFound)
{
break;
}
}
}
(3) In cocktail sort, we sort in both directions with each pass, and the iterations in each pass reduces over time.
public static void Cocktail(List<int> list)
{
Console.WriteLine("Cocktail Sort");
var mismatchFound = false;
//Will reduce with each pass
for (int iterationLimit = list.Count - 1; iterationLimit > 0; iterationLimit--)
{
mismatchFound = false;
//From back to front
for (int i = iterationLimit; i > 0; i--)
{
if (list[i - 1] > list[i])
{
var temp = list[i];
list[i] = list[i - 1]; //Bubble forward
list[i - 1] = temp;
mismatchFound = true;
}
}
//From front to back
for (int i = 0; i < iterationLimit; i++)
{
if (list[i] > list[i + 1])
{
var temp = list[i + 1];
list[i + 1] = list[i]; //Bubble forward
list[i] = temp;
mismatchFound = true;
}
}
if (!mismatchFound)
{
break;
}
}
}
Comments
Post a Comment