Click here to Skip to main content
15,891,976 members
Articles / Programming Languages / C#

Back to Basics - Generic Data Structures and Algorithms In .NET 2.0

Rate me:
Please Sign up or sign in to vote.
4.96/5 (93 votes)
23 Apr 2007Ms-PL15 min read 279.3K   2.6K   300  
Implementations of generic data structures and algorithms in .NET 2.0.
using System;
using System.Collections.Generic;
using System.Text;
using DataStructuresDotNet.ComparerImplementations;

namespace DataStructuresDotNet.Sorting
{
	public abstract class Sorter<T> : ISorter<T>
	{
		#region Construction

		/// <summary>
		/// Initializes a new instance of the <see cref="Sorter&lt;T&gt;"/> class.
		/// </summary>
		protected Sorter()
		{
			
		}		

		#endregion
								
		#region ISorter<T> Members

		/// <summary>
		/// Sorts the specified list.
		/// </summary>
		/// <param name="list">The list.</param>
		/// <param name="comparer">The comparer to use in comparing items.</param>
		public abstract void Sort(IList<T> list, IComparer<T> comparer);

		/// <summary>
		/// Sorts the specified list.
		/// </summary>
		/// <param name="list">The list.</param>
		public void Sort(IList<T> list)
		{
			if (list == null)
			{
				throw new ArgumentNullException("list");
			}

			Sort(list, Comparer<T>.Default);
		}

		/// <summary>
		/// Sorts the specified list.
		/// </summary>
		/// <param name="list">The list.</param>
		public void Sort(IList<T> list, SortOrder order)
		{
			if (list == null)
			{
				throw new ArgumentNullException("list");
			}

			if (order == SortOrder.Ascending)
			{
				Sort(list, Comparer<T>.Default);
			}
			else if (order == SortOrder.Descending)
			{
				Sort(list, new ReverseComparer<T>(Comparer<T>.Default));
			}
		}

		#endregion

		#region Protected Members

		/// <summary>
		/// Swaps items in the specified list.
		/// </summary>
		/// <param name="list">The list.</param>
		/// <param name="pos1">The position of the first item.</param>
		/// <param name="pos2">The position of the last item.</param>
		protected void Swap(IList<T> list, int pos1, int pos2)
		{
			T tmp = list[pos1];
			list[pos1] = list[pos2];
			list[pos2] = tmp;
		}
		
		#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.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Web Developer
South Africa South Africa
The author is a software consultant in South Africa, specializing in bespoke software solutions.

Comments and Discussions