![](/style/images/good.png)
![](/style/images/bad.png)
Generic bubble sort in C# .NET
source link: https://swimburger.net/blog/dotnet/generic-bubble-sort-in-csharp-dotnet
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Generic Bubble Sort in C# .NET
Early this year, I decided to brush up on my algorithms and data structure knowledge. I took these great two courses (1, 2) on PluralSight by Robert Horvick.
To practice what I learned in this course, I decided to create generic versions of the different algorithms and data structures.
What do I mean by generic versions? These types of courses always use integers or strings to demonstrate the algorithms. Instead of using those primitive data types, I'm reimplementing the algorithms and data structures using C#'s generic type parameters.
Here's a console application with a generic method BubbleSort
to perform a bubble sort on an enumerable:
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { var randomNumbers = new int[] { 5, 4, 5, 7, 6, 9, 4, 1, 1, 3, 4, 50, 56, 41 }; var sortedNumbers = BubbleSort(randomNumbers); PrintList(sortedNumbers); Console.ReadKey(); } private static IEnumerable<T> BubbleSort<T>(IEnumerable<T> list) where T : IComparable { T[] sortedList = list.ToArray(); int listLength = sortedList.Length; while (true) { bool performedSwap = false; for (int currentItemIndex = 1; currentItemIndex < listLength; currentItemIndex++) { int previousItemIndex = currentItemIndex - 1; T previousItem = sortedList[previousItemIndex]; T currentItem = sortedList[currentItemIndex]; var comparison = previousItem.CompareTo(currentItem); if (comparison > 0) { sortedList[previousItemIndex] = currentItem; sortedList[currentItemIndex] = previousItem; performedSwap = true; } } if (!performedSwap) { break; } } return sortedList; } private static void PrintList<T>(IEnumerable<T> list) { foreach (var item in list) { Console.WriteLine(item); } } }
By using a generic type parameter with the constraint that the type has to implement the IComparable
interface, you can perform the bubble sort algorithm without knowing the exact type you are working with.
If you want to understand the logic behind the bubble sort algorithm, I recommend checking out the courses mentioned earlier. There's also a lot of other great resources out there online!
Disclaimer: This code works, but is only developed for the sake of practice. Use at your own risk or just use a sorting library. If you see some room for improvement, there most likely is, I'm all ears~
![Niels Swimberghe](https://swimburger.net/content/img/avatar.jpg)
Niels Swimberghe
Niels Swimberghe is a Belgian Full Stack Developer solving problems and delivering value to customers using .NET technologies for back-end systems, and modern JavaScript technologies for the front-end.
Found this article useful? Follow me on Twitter, buy me a coffee, add this blog to your feed reader!
Recommend
-
6
Understanding .NET Generic Host Model 2020-08-13asp.net core 6 min readIn this article, we will concentrate on how the Generic Host model hosts ASP.NET Core 3.x Web app...
-
4
NServiceBus NServiceBus and .NET Core Generic Host Jimmy Bogard
-
3
Preview Features in .NET 6 – Generic Math Tanner
-
11
Generic Insertion Sort in C# .NET Niels Swimberghe - 8/4/2021 - .NET Follow me on
-
4
1. Bubble SortBubble sort repeatedly compares and swaps(if needed) adjacent elements in every pass. In
-
7
Bubble sort slower with -O3 than -O2 with GCC Asked 9 days ago Active
-
6
You're a programmer, right? But do you rally think like programmers (or computer scientists) do? When going for job interviews, you're likely to meet data structure and algorithm questions. This helps companies make sure they hire a s...
-
5
Bubble Sort - Videos | GeeksforGeeksSkip to content
-
2
Time and Space Complexity Analysis of Bubble SortSkip to content
-
4
What is the Bubble Sort Algorithm for Numbers?What is the Bubble Sort Algorithm for Numbers?June 27th 2023 New Story5min...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK