Click here to Skip to main content
15,892,746 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 346.3K   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;

namespace NSort
{
	public class DoubleStorageMergeSort : SwapSorter
	{
		public DoubleStorageMergeSort() : base() {}

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

		public override void Sort(IList list) 
		{
			object[] scratch = new IComparable[list.Count];
			Sort(list, 0, list.Count-1, scratch);
		}

		private void Sort(IList list, int fromPos, int toPos, object[] scratch) 
		{
			int mid=0;
			int i;
			int t_low;
			int t_high;
		
			if (fromPos < toPos) 
			{
				mid = (fromPos + toPos) / 2;
				Sort(list, fromPos, mid, scratch);
				Sort(list, mid + 1, toPos,scratch);
				
				t_low = fromPos;
				t_high = mid + 1;
				for (i = fromPos;i<=toPos;i++) 
				{
					if (t_low <= mid) 
					{
						if (t_high > toPos) 
						{
							scratch[i] = list[t_low];
							t_low++;
						} 
						else 
						{
							if (Comparer.Compare(list[t_low], list[t_high])<0) 
							{
								scratch[i] = list[t_low];
								t_low++;
							} 
							else 
							{
								scratch[i] = list[t_high];
								t_high++;
							}
						}
					} 
					else 
					{
						scratch[i] = list[t_high];
						t_high++;
					}
				}
				for (i = fromPos;i<=toPos;i++) 
				{				
					Swapper.Set(list, i, scratch[i]);
				}
			}
		}
	}
}

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