Comparison of sorting algorithm and its application

Source: Internet
Author: User

First, the various data sorting

The data type of the comparable interface can be sorted as long as it is implemented.

However, to make the algorithm flexible to sort by different fields, it is a question to be considered later.

1. Ordering of pointers

In Java, pointer operations are implicit, and the sort algorithm operations are always data references, not the data itself.

2, the key is not variable

If the use case can also change the key value after sorting, then the array is probably not ordered. Similarly, priority queues can be messed up.

In Java, you can avoid this problem by using an immutable data type as a key, such as string,integer,double and file are immutable.

3. Cheap Exchange

Another benefit of using references is that the algorithm does not have to move the entire element, and it will save a lot of operational costs for arrays with large key values that are small.

Because comparisons require only a small subset of the elements to be accessed, most of them will not be accessed during the entire sorting process.

Therefore, for arbitrary-sized elements, the use of references makes the cost of exchanging and comparing in general cases almost identical.

If the key value is long, the cost of the interchange is even lower than the cost of the comparison.

4. Multiple sorting methods

In practice, users want to sort a group of objects in different ways, depending on the situation.

Java's comparator interface allows multiple sorting methods to be implemented in a class. the Comparator interface object is passed to the less method by passing it to the sort method.

Examples of comparator interface implementations are:

    class Implements Comparator<transaction> {        @Override        publicint  Compare ( Transaction V, Transaction W) {            //Add code            return 0;        }            } 

The sort example is as follows:

     Public Static voidsort (object[] A, Comparator c) {intN =a.length;  for(inti = 1; i < N; i++)             for(intj = i; J > 0 && Less (c, A[j], a[j-1]); j--) Exch (A, J, J-1); }        Private Static BooleanLess (Comparator C, Object V, Object W) {returnC.compare (V, W) < 0; }        Private Static voidExch (object[] A,intIintj) {Object T=A[i]; A[i]=A[j]; A[J]=T; }

The comparator interface allows you to define any number of sorting methods for any data. Replacing the comparable interface with Comparator is a better way to differentiate between the definition of a data type and the definition of how two objects of that data type should be compared.

For example, relative to string array A, ignoring case ordering, you can use the Case_insensitive_order comparer comparator in the string class and pass it to the sort function:

Insertion.sort (A, String.case_insensitive_order)

5, Multi-key

In real-world applications, multiple attributes of an element may be used as a sort key.

To achieve this flexibility, the Comparator interface is just right, and we can define multiple comparators (Comparator) in the data type.

For example:

 Public classTransactionImplementsComparable<transaction> {    Private FinalString who;//Customer    Private FinalDate when;//Date    Private Final DoubleAmount//Amount    /*** Compares transactions by customer name. */     Public Static classWhoorderImplementsComparator<transaction>{@Override Public intCompare (Transaction V, Transaction W) {returnV.who.compareto (w.who); }    }    /*** Compares-transactions by date. */     Public Static classWhenorderImplementsComparator<transaction>{@Override Public intCompare (Transaction V, Transaction W) {returnV.when.compareto (W.when); }    }    /*** Compares transactions by amount. */     Public Static classHowmuchorderImplementsComparator<transaction>{@Override Public intCompare (Transaction V, Transaction W) {returnDouble.compare (V.amount, W.amount); }    }}

After this definition, you want transaction to be sorted by time, you can call

New

6. Stability

If a sorting algorithm preserves the relative position of the repeating elements in the array, then this sort algorithm makes it stable.

The insertion sort and merge sort are stable, and the selection sort , the Hill sort , the quick sort , and the heap sort are not stable.

Comparison of sorting algorithms 1, insert sort

stable , in-situ sequencing

Time complexity: worst ~n2/2, best ~n, average ~N2/4

Space complexity: 1

Number of exchanges: worst ~n2/2, best 0, average ~N2/4

Note: Depending on the arrangement of the input elements

Http://www.cnblogs.com/songdechiu/p/6610515.html

2. Select sort

Unstable, in-situ sequencing

Time complexity: N2 (~N2/2)

Space complexity: 1

Number of exchanges: N

Http://www.cnblogs.com/songdechiu/p/6609896.html

3. Hill sort

Unstable, in-situ sequencing

Time complexity: up to the square level, the worst and N3/2 proportional ( unknown )

Space complexity: 1

Http://www.cnblogs.com/songdechiu/p/6611340.html

4. Quick Sort

Unstable, in-situ sequencing

Time complexity: best ~nlogn, worst ~n2, average 1.39NlogN

Spatial complexity: best Logn, worst N, average Logn

Operational efficiency guaranteed by probability

Http://www.cnblogs.com/songdechiu/p/6629539.html

5, three-direction segmentation quick Sorting

Unstable, in-situ sequencing

Time complexity: best N, worst ~n2, average ~nlogn

Space complexity: best 1, worst N, average Logn

Operational efficiency guaranteed by probability

Http://www.cnblogs.com/songdechiu/p/6629539.html

6. Merge sort

stable , non-in-situ sequencing

Time complexity: 1/2NLGN to NLGN

Complexity of space: N

Number of array accesses: 6NlgN

Http://www.cnblogs.com/songdechiu/p/6607341.html

Http://www.cnblogs.com/songdechiu/p/6607720.html

7. Heap Sequencing

Unstable, in-situ sequencing

Time complexity:2NlogN + 2N (worst of all)

Space complexity: 1

Number of array element exchanges:Nlogn + N (worst)

Http://www.cnblogs.com/songdechiu/p/6736502.html

Conclusion: fast sequencing is the fastest general-purpose sorting algorithm because of the small number of internal loops and the ability to take advantage of cache (sequential access to data) with a growth order of ~CNLOGN. Especially after using three-slice, the complexity of the express line to some special input becomes linear level.

In practice, in most cases, fast sequencing is the best choice.

But if stability is important and space is not a problem, then the merge sort is the best.

Iii. problem Specification 1, identifying duplicate elements

First, sort the array, then iterate through the ordered array, and record the repeated occurrences of the elements.

2. Arrangement

A permutation is an array of n integers, where each number from 0 to N-1 appears only once.

The Kendall tau distance between the two permutations is the inverse number pair in the two sets of permutations. For example, 0 3 1 6 2 5 4 and 1 0 3 6, 4 2 5 The Kendall tau distance is 4, because 0-1,3-1,2-4,5-4 this four is different from the relative order of the numbers.

Kendall Tau Distance is the number of inverse pairs .

Implementation: http://algs4.cs.princeton.edu/25applications/KendallTau.java.html

 Public classKendalltau {//return Kendall tau distance between, permutations     Public Static LongDistanceint[] A,int[] b) {if(A.length! =b.length) {Throw NewIllegalArgumentException ("Array Dimensions Disagree"); }        intn =a.length; int[] Ainv =New int[n];  for(inti = 0; I < n; i++) Ainv[a[i]]=i; Integer[] bnew=NewInteger[n];  for(inti = 0; I < n; i++) Bnew[i]=Ainv[b[i]]; returnInversions.count (bnew); }

}

In order to find the inverse number of two permutations A and B , it is a bit different to find an unordered permutation in the order of the previous merge sort .

In merge sort, the comparison is by default with the standard sequence (ordered), i.e., 0 1 2 3 4 5 6, where a is compared with B, we use B as the standard sequence .

So we're going to convert the inverse number pair between a sequence and a standard sequence, and then pass that sequence to the solution in the merge sort.

To facilitate the interpretation of the above code, make the following assumptions

a:0 3 1 6 2 5 4

B:1 0 3 6 4 2 5

which will a[i] called key, I is called index; B[j] is also key, J is index.

The main idea of the above code is to convert to the index sequence between the comparison , with B as the standard sequence, its index sequence is 0 1 2 3 4 5 6, and then find B[i] (0<= i <= 6) in array a corresponding The index sequence .

AINV is an inverse array of a, that is, the index that stores the key of the array A, that is, ainv[k] = i represents a[i] = k.

When key is b[0] = 1 o'clock, its index is ainv[b[0]] that is ainv[1] = 2, and so on, finally find a relative to B index sequence is 2 0 1 3 6 4 5.

Finally, the relative index sequence 2 0 1 3 6 4 5 is passed to the solution method in the merge sort, and is calculated as 4.

The following is a method for merging the number pairs of Qiu in reverse order:

Http://algs4.cs.princeton.edu/22mergesort/Inversions.java.html

 Public classInversions {//Do not instantiate    Privateinversions () {}//Merge and Count    Private Static LongMergeint[] A,int[] aux,intLointMidinthi) {        LongInversions = 0; //copy to aux[]         for(intK = lo; K <= Hi; k++) {Aux[k]=A[k]; }        //merge back to a[]        inti = lo, j = mid+1;  for(intK = lo; K <= Hi; k++) {            if(i > Mid) a[k] = aux[j++]; Else if(J > Hi) a[k] = aux[i++]; Else if(Aux[j] < aux[i]) {A[k] = aux[j++];inversions + = (mid-i + 1); }            ElseA[k] = aux[i++]; }        returninversions; }    //Return the number of inversions in the subarray B[lo. Hi]//side effect B[lo. Hi] is rearranged in ascending order    Private Static LongCountint[] A,int[] B,int[] aux,intLointhi) {        LongInversions = 0; if(Hi <= lo)return0; intMid = lo + (Hi-lo)/2; Inversions+=Count (A, B, aux, lo, mid); Inversions+ = count (A, B, aux, mid+1, HI); Inversions+=merge (b, Aux, Lo, Mid, hi); assertInversions = =Brute (A, lo, HI); returninversions; }    /*** Returns the number of inversions in the integer array.     * The argument array is not modified. * @parama The array *@returnThe number of inversions in the array. An inversion is a pair of * indicies {@codei} and {@codeJ} such that {@codei < J} * and {@codeA[i]} > {@codeA[j]}. */     Public Static LongCountint[] a) {int[] B =New int[A.length]; int[] aux =New int[A.length];  for(inti = 0; i < a.length; i++) B[i]=A[i]; LongInversions = count (A, B, aux, 0, a.length-1); returninversions; }}

3. Priority Queue Protocol

Two protocols are examples of priority queue operation issues:

TOPM, the first m largest element is found in the input stream.

MultiWay, the M-ordered input stream is merged into an ordered input stream.

4, median and sequential statistics

Find a small number of elements in a set of numbers (the median is a special case).

It is possible to regulate the segmentation method in a quick row.

     Public Static<keyextendsComparable<key>> Key Select (key[] A,intk) {if(k < 0 | | k >=a.length) {Throw NewIndexoutofboundsexception ("Selected element out of bounds");        } stdrandom.shuffle (a); intLo = 0, hi = a.length-1;  while(Hi >lo) {            inti =partition (A, lo, HI); if(i > k) Hi = i-1; Else if(I < k) Lo = i + 1; Else returnA[i]; }        returnA[lo]; }

On average, the run time of the segmentation-based selection algorithm is linear.

Iv. sequencing applications

Comparison of sorting algorithm and its application

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.