# C # Data structures and algorithms-sorting

Source: Internet
Author: User
Tags sorts

`7.7the comparison and discussion of various sorting methods are very important in computer program design, the various sorting methods described above have their advantages and disadvantages, and the applicable occasions are different. The factors to consider when selecting a sorting method are: (1The size of the number N of the record to be sorted;2) record the size of other information except the key code;3) key codes;4) The requirements for sequencing stability;5The conditions of the language tool, the size of the secondary space, and so on. Considering the above factors comprehensively, we can draw the following conclusions: (1) If the number of sorted records is less than n (e.g. n≤ -), you can use either a direct insert sort or a simple select sort.    Since the record move operation required for direct insertion of a sort is more simple to choose, it is better to sort by simple selection when the information of the record itself is large. (2if the initial state of the record has been basically ordered according to the key code, you can use direct insert sort or bubble sort. (3if the number of sorted records is greater than N, then the time complexity of O (nlog2n) sorting method (such as fast sorting, heap sorting or merge sorting, etc.) can be adopted. The average performance of fast sorting is best, and the fast sort is best when the sequence is randomly distributed by key code. The time complexity of a quick sort in the worst case is O (N2), while heap sorting does not change in the worst case time complexity, and the required secondary space is less than the fast sort.    However, these two sorts of sorting methods are unstable, and if a stable sorting method is required, the merge sort can be used. (4The cardinality sort can be ordered in O (DXN) (d is the number of key codes, when n is much greater than D), but the cardinality sort is only suitable for a key code with distinct structural characteristics such as strings and integers.    When n is large and d is small, it can be sorted by cardinality. (5The sorting algorithm discussed earlier, in addition to the Cardinal sort, the other sorting algorithm is the sequential storage structure. In order to avoid a significant amount of time to record movement, a chain storage structure can be used in order to sort the records. Direct Insert sort and merge sort are very easy to implement on a linked list, but quick sorting and heap sorting are difficult to implement on a linked list. At this point, you can extract the key code to create an index table and then sort the index table. You can also introduce an array of t[n] as a secondary table, sort the pre-order T[i]=i,1≤i≤n. If the sorting algorithm requires Exchange records R[I] and R[j], then only exchange T[i] and T[j] can be. When the sort is finished, the array T[n] stores the order relationship between records. 7.8The sorting method in C # Many classes in the C # language provide a sorted member method. For example, the array class in the fourth chapter provides the sorting method sort. The sorting method in the array class takes a fast sort algorithm and requires that the data elements in the array must implement the IComparable interface so that the data elements can be compared. In fact, any type of data is sorted using a comparer, so the type must implement the IComparable interface to implement the Sort method. Generic classes implement a generic IComparable interface, and non-generic classes implement a non-generic IComparable interface.    The comparer class is available in C # to implement various comparators. Another example is that the generic List class implements the Sort method. The Sort method uses the comparer class's comparer to determine the ListThe order of the data elements in the <T> class. The comparator first checks the list<t>whether the data element in the class implements the generic IComparable interface, and if the comparer is implemented using that implementation, the comparer then checks whether the data element implements the non-generic IComparable interface and uses the implementation if it is implemented.    If neither of these interfaces is implemented, the comparer throws a InvalidOperationException exception method using the sort method of the array class arrays. Similarly, the ASP.2.0the GridView control in is also implemented with the Sort method. The GridView control is similar to a DataGrid control, but more appealing than a DataGrid control, easier to use, and less code for programmers to write.    The sort method sorts the Gridvie control using sort expressions and sort directions. A sort expression is used to determine which columns to sort on, and it can sort multiple columns.    The sort direction determines whether to sort in ascending or descending order. 7.7the comparison and discussion of various sorting methods are very important in computer program design, the various sorting methods described above have their advantages and disadvantages, and the applicable occasions are different. The factors to consider when selecting a sorting method are: (1The size of the number N of the record to be sorted;2) record the size of other information except the key code;3) key codes;4) The requirements for sequencing stability;5The conditions of the language tool, the size of the secondary space, and so on. Considering the above factors comprehensively, we can draw the following conclusions: (1) If the number of sorted records is less than n (e.g. n≤ -), you can use either a direct insert sort or a simple select sort.    Since the record move operation required for direct insertion of a sort is more simple to choose, it is better to sort by simple selection when the information of the record itself is large. (2if the initial state of the record has been basically ordered according to the key code, you can use direct insert sort or bubble sort. (3if the number of sorted records is greater than N, then the time complexity of O (nlog2n) sorting method (such as fast sorting, heap sorting or merge sorting, etc.) can be adopted. The average performance of fast sorting is best, and the fast sort is best when the sequence is randomly distributed by key code. The time complexity of a quick sort in the worst case is O (N2), while heap sorting does not change in the worst case time complexity, and the required secondary space is less than the fast sort.    However, these two sorts of sorting methods are unstable, and if a stable sorting method is required, the merge sort can be used. (4The cardinality sort can be ordered in O (DXN) (d is the number of key codes, when n is much greater than D), but the cardinality sort is only suitable for a key code with distinct structural characteristics such as strings and integers.    When n is large and d is small, it can be sorted by cardinality. (5The sorting algorithm discussed earlier, in addition to the Cardinal sort, the other sorting algorithm is the sequential storage structure. In order to avoid a significant amount of time to record movement, a chain storage structure can be used in order to sort the records. Direct Insert sort and merge sort are very easy to implement on a linked list, but quick sorting and heap sorting are difficult to implement on a linked list. At this point, you can extract the key code to create an index table and then sort the index table. You can also introduce an array of t[n] as a secondary table, sort the pre-order T[i]=i,1≤i≤n. If the sorting algorithm requires Exchange records R[I] and R[j], then only exchange T[i] and T[j] can be. When the sort is finished, the array T[n] stores the order relationship between records. `

C # Data structures and algorithms-sorting

Related Keywords:
Related Article

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.