General fast Sorting Algorithm

Source: Internet
Author: User

Note:

This is something that you usually write based on your needs.CodeIn order to avoid data loss again, putting it online may not be useful to various readers. In addition, this is written based on my personal thoughts, and may not be rigorous and compliant with the design principles. If there is anything wrong with it, please do not give us any further advice.

Description:

For sorting or searchingAlgorithmThe difficulty of reusing these codes is that the algorithm logic is coupled with the code for dataset operations. As a result, even though the algorithm logic is the same, the entire algorithm has to be re-implemented due to the difference in dataset types. The reuse of algorithms we want is actually the reuse of algorithm logic. Therefore, the key to reusing algorithms is to extract the algorithm logic. Generally, the structure of an algorithm is that dataset operations are embedded in the logic process of the algorithm. Therefore, if a unified interface can be abstracted for dataset operations, the algorithm logic can be universal.
How to abstract an interface for dataset operations? Should a base class be defined for the dataset? Obviously, this is not feasible. Although. Net has some basic set-type interfaces, these interfaces basically do not provide the operation methods required for sorting. And not all types have the same base type. Therefore, to abstract the data operation interface, you must change the angle.
Most sorting algorithms involve operations on Datasets: 1. Compare two values in a dataset; 2. Exchange two values in a dataset. You only need to design the interfaces for these two operations. Obviously, how to compare data depends on the Data Type and business requirements, and how to exchange data values is related to the dataset structure and data type. On the other hand, the algorithm only determines the location of the data to be compared or exchanged, and the algorithm only cares about the result of the comparison operation (that is, the return value ).
In summary, a data operation interface can be abstracted: 1. Comparison. Two indexes are accepted and comparison results are returned. 2. Exchange: two indexes are accepted and no return value is returned. The algorithm calls these two interfaces and passes in the index. The caller is responsible for comparing the values of the two indexes and exchanging the values of the two indexes.
For C #, this interface can be implemented using delegation, while for Java, it can only be implemented using interfaces. Relatively speaking, the delegate implementation is concise, the user needs to do less work, and the efficiency should be higher.

In addition, for fast sorting, you only need a two-way iterative accesser for the data source, but this implementation should be a little more complicated than the above method.

 

Example:

Class quicksorttest {static byte [] array; static random = new random (); static quicksorter = new quicksorter (); public static void swap (INT idxa, int idxb) {byte temp = array [idxa]; array [idxa] = array [idxb]; array [idxb] = temp;} public static int compare (INT idxa, int idxb) {return array [idxa]-array [idxb];} public static void test () {int testtimes = 1000; while (-- testtimes> 0) {array = new byte [random. next (50)]; random. nextbytes (array); console. write ("\ r \ nbefore sort:"); foreach (byte data in array) {console. write (data. tostring () + '\ t');} quicksorter. quicksort (0, array. length-1, compare, swap); console. write ("\ r \ nafter sort:"); foreach (byte data in array) {console. write (data. tostring () + '\ t');} If (array. length <= 1) continue; For (INT idx = 0; idx <array. length-1; idx ++) {If (array [idx]> array [idx + 1]) console. write ("\ r \ nsort error ");}}}}

source code:

Public class quicksorter {Public Delegate int comparedelegate (INT idxa, int idxb); Public Delegate void swapdelegate (INT idxa, int idxb); Private comparedelegate comparer; private swapdelegate swaper; public int quicksort (INT lbound, int ubound, comparedelegate comparer, swapdelegate swaper) {This. comparer = comparer; this. swaper = swaper; quicksort (lbound, ubound); return 1;} private int quicksort (INT lbound, int ubound) {If (ubound-lbound <1) return 0; int forwardpointr = lbound, backwardpointer = ubound + 1; // note: the first element is used as the reference while (forwardpointr <backwardpointer) {// find the first one that is greater than the reference value. While (++ forwardpointr <backwardpointer & comparer (lbound, forwardpointr)> = 0 ); // find the first one that is less than or equal to the reference number from the back. While (-- backwardpointer> forwardpointr & comparer (lbound, backwardpointer) <0); // if it is found, then the exchange value if (forwardpointr <backwardpointer) swaper (backwardpointer, forwardpointr);} // change the reference value to the intermediate swaper (lbound, -- iterator); quicksort ); quicksort (forwardpointr + 1, ubound); return 1 ;}}
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.