Archive for the ‘C#’ Category

C# Introsort

Wednesday, February 27th, 2008

Here is a C# implementation of the Introsort algorithm. Notice that the Introsort method is nearly identical to the Quicksort method. The only difference is the addition of checking for the depth limit (number of recursions -- lines 51-59); when the depth limit has been reached, the Heapsort method is called to ...

C# Heapsort

Friday, February 15th, 2008

This is a simple implementation of the Heapsort algorithm in C#. It is a max-heap implementation, meaning that it builds the heap from the bottom up by sifting down the maximum element; this is opposed to the min-heap implementation that builds the heap top-down by sifting upward. Also, this method is ...

.NET Platform Invoke (P/Invoke) Sample

Tuesday, February 12th, 2008

Recently someone asked me to help them call an unmanaged C++ DLL from a C# application, whereupon I promptly pointed them to the MSDN documentation on P/Invoke. Unfortunately (as I later learned) the documentation is very descriptive if you're trying to use a Windows DLL, but it doesn't help you ...

C# Quicksort

Monday, December 10th, 2007

Here is a simple implementation of the Quicksort sorting algorithm in C#. This class uses generics so any type that implements IComparable can be sorted. This code is neither intended to be the most efficient Quicksort implementation, nor does it implement Median-of-Three and Insertion sort enhancements; it does, however, provide ...

Quicksort, Heapsort, and Introsort

Wednesday, December 5th, 2007

Ever heard of the introspective sort, also known as the introsort algorithm? The Wikipedia page is lacking, but this paper by David Musser describing the algorithm describes it in all its beauty. Basically it is an algorithm that sorts a list of elements by using a combination of Quicksort (with ...

Unbiased Shuffle Algorithm

Thursday, November 29th, 2007

I have recently been fascinated by the mathematics of shuffling, or randomly reassigning values to different indices within an array of elements. Wikipedia has a good article about the Fisher-Yates shuffle, which, "properly implemented, ... is unbiased, so that every permutation is equally likely." Here is a C# implementation of ...