Working with NSort and RSS.NET - Using NSort
(Page 2 of 4 )
The NSort Library provides 15 different sort algorithms:
- Bidirectional Bubble Sort
- Bubble Sort
- Combo Sort 11
- Double Storage Merge Sort
- Fast Quick Sorter
- Heap Sort
- In Place Merge Sort
- Insertion Sort
- Odd/Even Transport Sort
- Quick Sort
- Quick Sort with Bubble Sort
- Selection Sort
- Shaker Sort
- Shear Sort
- Shell Sort
These sorters vary in best-and worst-case sort times and in memory usage. Unfortunately, determining the optimal sorter isn’t always easy. It’s not just a matter of choosing one with the “best”worst-case sort time and the least amount of memory utilization. Performance depends on the size of your list and on how much of the data is already sorted. One sorter may perform very well under one set of conditions and poorly under a different set. For this reason, Quick Sort with Bubble Sort often makes a good default sorter. This sorter reverts to a Bubble Sort for small sets of data, which is more efficient than a Quick Sort. For large sets, it uses the Quick Sort algorithm first, then applies the Bubble Sort on the smaller fragments.
Using the sorters is straightforward. Decide which sorter you wish to use and instantiate it. Here’s an example that requests a Bubble Sort:
private ISorter sorter = new BubbleSorter();
Next, create the list of items to be sorted. TheSort()method takes anIListrepresenting an enumerable collection. For example, to sort a set of random numbers:
Random rnd = new Random();
int[] list = new int[1000];
for(int i = 0;i<list.Length;++i)
{
list[i] = rnd.Next();
}
ArrayList mylist=new ArrayList(list);
sorter.Sort(mylist);
The default constructor for each of the sorters instantiates theComparableComparer andDefaultSwapclasses. These handle all value types and any class or struct implementingIComparable.
You can also customize the comparer and swapper by implementingIComparerandISwapclasses. All the sorters have an additional constructor:
public xxx(IComparer comparer, ISwap swapper)
This constructor can be used to specify your own compare and swap algorithms. This is a very useful feature of these classes, allowing you to sort not just value types, but also classes and structs that do not implementIComparable. Conversely, you can implementIComparableon your own classes to support custom comparisons.
Many of the sorters in NSort are ported from sorting demonstration code in Java, available at http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html.
For more information on sorting, refer to John Robbins’s MSDN article “Make Your Apps Fly with the New Enterprise Performance Tool,”available at http:// msdn.microsoft.com/msdnmag/issues/04/12/EnterprisePerformance/default.aspx. You can also find an overview of sorting on Wikipedia at http://en.wikipedia.org/wiki/ Sort_algorithms.
Getting Support
NSort is supported via the tool’s Code Project article at http://www.codeproject.com/ csharp/cssorters.asp and at http://www.marcclifton.com.
Problems or questions can be posted in the comments section for the Code Project article or in the forum at Marc Clifton’s web site.
NSort Library in a Nutshell
NSort is a useful collection of sorting algorithms that enables you to select a sorter suitable
for your requirements. The algorithms are very flexible, as the core comparer supports
classes implementing IComparable and can be overridden for classes that don’t support IComparable. You can also override the default swap algorithm to handle custom
swapping of objects and values.
—Marc Clifton, cocreator of NSort
Next: 4.9 Creating RSS Feeds with RSS.NET >>
More BrainDump Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Windows Developer Power Tools, written by James Avery and Jim Holmes (O'Reilly; ISBN: 0596527543). Check it out today at your favorite bookstore. Buy this book now.
|
|