Summary of common DotNet sorting algorithms and dotnet sorting algorithms

Source: Internet
Author: User
Tags dotnet

Summary of common DotNet sorting algorithms and dotnet sorting algorithms

Data structures and algorithms are crucial to a program. Here we will introduce several algorithms. The commonly used algorithms in projects include Bubble sorting, simple sorting, and direct insertion of sorting, hill sorting, heap sorting, Merge Sorting, and quick sorting among other 7 algorithms.

Now we will introduce the selection sorting algorithm, the hill sorting algorithm, and the quick sorting algorithm.

(1 ). select Sorting Algorithm: by comparing the n-I keywords, the minimum keyword record is selected from the n-I + 1 record, and exchange with records I (1 is greater than or equal to I is less than or equal to n.

(2). Hill sorting: Take an integer d1 smaller than n as the first increment, and group all records of the file. All records whose distance is multiples of d1 are placed in the same group. Insert and sort directly in each group. Then, repeat the group and sort in the second incremental d2 <d1 until the obtained increment is 1 (<... <D2 <d1), that is, all records are placed in the same group for direct insertion sorting.

(3 ). quick Sorting Algorithm: splits the records to be sorted into two separate parts by one sort. the keywords of some records are smaller than those of other records, then, the two records can be sorted separately to achieve the purpose of sorting the entire sequence.

The above is a brief description of the algorithm definition. Next we will look at the specific implementation of the algorithm:

1. Sorting Algorithm type interface:

/// <Summary> /// Sorting Algorithm type interface /// </summary> internal interface ISortAlgorithm {// <summary> /// specify. /// </Summary> /// <typeparam name = "T"> type of the elements to be sorted </typeparam> /// <param name = "toSort"> Yes sort list </param> /// <param name = "direction"> sort direction </param> /// <param name = "startIndex"> Start index </param> /// <param name = "endIndex"> end start index </param> /// <param name = "compareFunc"> comparison function. </Param> void Sort <T> (IList <T> toSort, SortDirection direction, int startIndex, int endIndex, Comparison <T> compareFunc );}

2. Sorting Algorithm Factory:

/// <Summary> /// Sorting Algorithm factory class /// </summary> internal static class SortAlgorithmFactory {/// <summary> /// create Sorting Algorithm Implementation. /// </Summary> /// <param name = "algorithm"> algorithm </param> /// <returns> </returns> internal static ISortAlgorithm Merge (SortAlgorithm algorithm) {ISortAlgorithm toReturn = null; switch (algorithm) {case SortAlgorithm. selectionSort: toReturn = new SelectionSorter (); break; case SortAlgorithm. shellSort: toReturn = new ShellSorter (); break; case SortAlgorithm. quickSort: toReturn = new QuickSorter (); break;} return toReturn ;}}

3. Quick Sorting Algorithm:

/// <Summary> /// fast Sorting Algorithm /// </summary> internal class QuickSorter: ISortAlgorithm {// <summary> /// sort the specified list in the specified direction. /// </Summary> /// <typeparam name = "T"> type of the elements to be sorted </typeparam> /// <param name = "toSort"> Yes the list of sorted items. </Param> /// <param name = "direction"> sorts the direction of elements in an infringement action. </Param> /// <param name = "startIndex"> Start the index. </Param> /// <param name = "endIndex"> end the index. </Param> /// <param name = "compareFunc"> comparison function. </Param> void ISortAlgorithm. sort <T> (IList <T> toSort, SortDirection direction, int startIndex, int endIndex, Comparison <T> compareFunc) {Func <T, T, bool> valueComparerTest; switch (direction) {case SortDirection. ascending: valueComparerTest = (a, B) => (compareFunc (a, B) <0); break; case SortDirection. descending: valueComparerTest = (a, B) => (compareFunc (a, B)> 0); break; default: throw new Ar GumentOutOfRangeException ("direction", "Invalid direction specified, can't craete value comparer func");} define msort (toSort, startIndex, endIndex, valueComparerTest );} /// <summary> /// sort the partitions in the list. This routine is recursively called. /// </Summary> /// <typeparam name = "T"> </typeparam> /// <param name = "toSort"> sort. </Param> /// <param name = "left"> left index. </Param> /// <param name = "right"> correct index. </Param> /// <param name = "valueComparerTest"> test the value comparator. </Param> private static void merge msort <T> (IList <T> toSort, int left, int right, Func <T, T, bool> valueComparerTest) {while (true) {if (right <= left) {return;} var comment tindex = Partition (toSort, left, right, left, valueComparerTest); Comment msort (toSort, left, comment tindex-1, valueComparerTest); left = tindex + 1 ;}} /// <summary> /// List specified by the partition /// </summary> /// <typeparam name = "T"> </typepa Ram> // <param name = "toSort"> sort. </Param> /// <param name = "left"> left. </Param> /// <param name = "right"> right </param> /// <param name = "pivotIndex"> pivot index. </Param> /// <param name = "valueComparerTest"> test the value comparator. </Param> // <returns> index of the new hub </returns> private static int Partition <T> (IList <T> toSort, int left, int right, int comment tindex, Func <T, T, bool> valueComparerTest) {var comment tvalue = toSort [comment tindex]; toSort. swapValues (gradient tindex, right); var storeIndex = left; for (var I = left; I <right; I ++) {if (! ValueComparerTest (toSort [I], effectvalue) {continue;} toSort. swapValues (I, storeIndex); storeIndex ++;} toSort. swapValues (storeIndex, right); return storeIndex ;}}

4. Hill Sorting Algorithm:

/// <Summary> /// Hill Sorting Algorithm /// </summary> internal class ShellSorter: ISortAlgorithm {// <summary> /// sort the specified list in the specified direction. /// </Summary> /// <typeparam name = "T"> type of the elements to be sorted </typeparam> /// <param name = "toSort"> Yes sort list </param> /// <param name = "direction"> sort direction </param> /// <param name = "startIndex"> Start index </param> /// <param name = "endIndex"> end start index </param> /// <param name = "compareFunc"> comparison function. </Param> void ISortAlgorithm. sort <T> (IList <T> toSort, SortDirection direction, int startIndex, int endIndex, Comparison <T> compareFunc) {Func <T, T, bool> valueComparerTest; switch (direction) {case SortDirection. ascending: valueComparerTest = (a, B) => (compareFunc (a, B)> 0); break; case SortDirection. descending: valueComparerTest = (a, B) => (compareFunc (a, B) <0); break; default: throw new ArgumentOutOfRangeException ("direction", "Invalid direction specified, can't craete value comparer func ");} int [] increments = {1391376,463 792, 198768,869 61, 33936,137 76, 4592,196 8, 861,336,112, 48, 21, 7, 3, 1}; for (var incrementIndex = 0; incrementIndex <increments. length; incrementIndex ++) {for (int intervalIndex = increments [incrementIndex], I = startIndex + intervalIndex; I <= endIndex; I ++) {var currentValue = toSort [I]; var j = I; while (j> = intervalIndex) & valueComparerTest (toSort [j-intervalIndex], currentValue )) {toSort [j] = toSort [j-intervalIndex]; j-= intervalIndex;} toSort [j] = currentValue ;}}}}

5. Select the Sorting Algorithm:

/// <Summary> /// select the Sorting Algorithm /// </summary> internal class SelectionSorter: ISortAlgorithm {// <summary> /// sort the specified list in the specified direction. /// </Summary> /// <typeparam name = "T"> type of the elements to be sorted </typeparam> /// <param name = "toSort"> Yes the list of sorted items. </Param> /// <param name = "direction"> sorts the direction of elements in an infringement action. </Param> /// <param name = "startIndex"> Start the index. </Param> /// <param name = "endIndex"> end the index. </Param> /// <param name = "compareFunc"> comparison function. </Param> void ISortAlgorithm. sort <T> (IList <T> toSort, SortDirection direction, int startIndex, int endIndex, Comparison <T> compareFunc) {Func <T, T, bool> valueComparerTest; switch (direction) {case SortDirection. ascending: valueComparerTest = (a, B) => (compareFunc (a, B)> 0); break; case SortDirection. descending: valueComparerTest = (a, B) => (compareFunc (a, B) <0); break; default: throw new ArgumentOutOfRangeException ("direction", "the specified direction is invalid, cannot create value comparator function ");} for (var I = startIndex; I <endIndex; I ++) {var indexValueToSwap = I; for (var j = I + 1; j <= endIndex; j ++) {if (valueComparerTest (toSort [indexValueToSwap], toSort [j]) {indexValueToSwap = j ;}} toSort. swapValues (I, indexValueToSwap );}}}

In the above algorithm implementation, a simple factory model is used to achieve loose coupling of the algorithm.

The simple factory mode is determined by a factory object to create a product instance. A dedicated class is used to create instances of other classes. The created instance usually has a common parent class. The simple factory mode contains the necessary judgment logic, which determines the specific class object to be created based on the information given by the outside world.

The UML diagram of a simple factory is as follows:

If you need to add a new algorithm, after adding the new algorithm implementation class, you can directly add the case Branch to the factory method without changing the class on the client, you only need to select the implementation class in the subclass.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.