Click here to Skip to main content
15,885,435 members
Articles / Programming Languages / C#

Sorting Algorithms In C#

, ,
Rate me:
Please Sign up or sign in to vote.
4.77/5 (47 votes)
4 Feb 20044 min read 345.8K   2.6K   193  
A collection of sorting algorithms implementing customizable comparitor and swapper functions
This library provides a very nice and flexible package of sorting algorithms from which the developer can choose.
using System;
using System.Collections;
using System.IO;


namespace NSort
{
	/// <summary>
	/// http://www.codeproject.com/csharp/csquicksort.asp
	/// </summary>
	public class QuickSorter : SwapSorter
	{
		public QuickSorter()
			:base()
		{}

		public QuickSorter(IComparer comparer, ISwap swapper)
			:base(comparer,swapper)
		{}

		/// <summary>
		/// Sorts the array.
		/// </summary>
		/// <param name="array">The array to sort.</param>
		public override void Sort(IList array)
		{
			Sort(array, 0, array.Count-1);
		}

		public void Sort(IList array, int lower, int upper)
		{
			// Check for non-base case
			if (lower < upper)
			{
				// Split and sort partitions
				int split=Pivot(array, lower, upper);
				Sort(array, lower, split-1);
				Sort(array, split+1, upper);
			}
		}

		#region Internal
		internal int Pivot(IList array, int lower, int upper)
		{
			// Pivot with first element
			int left=lower+1;
			object pivot=array[lower];
			int right=upper;

			// Partition array elements
			while (left <= right)
			{
				// Find item out of place
				while ( (left <= right) && (Comparer.Compare(array[left], pivot) <= 0) )
				{
					++left;
				}

				while ( (left <= right) && (Comparer.Compare(array[right], pivot) > 0) )
				{
					--right;
				}

				// Swap values if necessary
				if (left < right)
				{
					Swapper.Swap(array, left, right);
					++left;
					--right;
				}
			}

			// Move pivot element
			Swapper.Swap(array, lower, right);
			return right;
		}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions