Summary of the top ten ranking algorithms for Go Language (next)

Source: Internet
Author: User
Tags sorts
This is a creation in Article, where the information may have evolved or changed.

Hill sort

Hill sort basic idea: First take an integer less than n D1 as the first increment, dividing all the records of the file into D1 groups. All records that are multiples of the DL are placed in the same group. First, the direct insertion sort is performed within each group, and then the second increment D2 the D1 repeats the above groupings and sorts until the increment dt=1 (dt dt-l ... d2 d1), where all records are placed in the same group for direct insert sorting. This method is essentially a grouping insertion method.
Personal Summary:

public class Shellsorter
{
public void Sort (int[] arr)
{
INT Inc;
for (inc = 1; Inc <= arr. LENGTH/9; inc = 3 * inc + 1);
for (; Inc > 0; inc/= 3)
{
for (int i = inc + 1; I <= arr. Length; i + = Inc)
{
int t = arr[i-1];
int j = i;
while ((J > Inc) && (Arr[j-inc-1] > t))
{
ARR[J-1] = arr[j-inc-1];//Exchange data
J-= Inc;
}
Arr[j-1] = t;
}
}
}
}

Merge sort

With two sequential (ascending) sequences stored in adjacent positions in the same array, it may be set to a[l. M],a[m+1..h], merge them into an ordered sequence, and store them in A[l. H].

Its time complexity is O (nlog2n) in both the best and worst-case scenarios.
///
Merge sort: Merge sort entry
Unordered array///ordered array
Lihua (www.zivsoft.com)
Int[] Sort (int[] data)
{
To take the middle subscript of an array
int middle = data. LENGTH/2;
Initializes a temporary array of let,right and defines result as the final ordered array
Int[] left = new Int[middle], right = new Int[middle], result = new Int[data. Length];
if (data. Length% 2 = 0)//If array elements are odd, reinitialize right temporary array
{
right = new Int[middle + 1];
}
if (data. Length <= 1)//only 1 or 0 tuples left, returned, not sorted
{
return data;
}
int i = 0, j = 0;
foreach (int x in data)//start sorting
{
if (I < middle)//fill left array
{
Left[i] = x;
i++;
}
else//padding Right Array
{
RIGHT[J] = x;
j + +;
}
}
left = Sort (Ieft);//recursive lvalue array
right = Sort, or//recursive rvalue array
result = Merge (left, right);//Start sort
This. Write (result);//output sort, test (Lihua Debug)
return result;
}
///
Merge sort by: sort in this step
Left array///right array///merge left-side array to sort after return
Int[] Merge (int[] A, int[] b)
{
Defines an array of results to store the final result
Int[] result = new Int[a.length + b.length];
int i = 0, j = 0, k = 0;
while (I < a.length && J < B.length)
{
if (A[i] < B[J])//The element in the left array is less than the element in the right array
{
result[k++] = a[i++];//put the small one in the result array
}
else//elements in the left array that are greater than the right array
{
result[k++] = b[j++];//put the small one in the result array
}
}
while (I < a.length)//Here is actually the left element, but there is no right element
{
result[k++] = a[i++];
}
while (J < b.length)//right-right element, no left element
{
result[k++] = b[j++];
}
Return result;//returns an array of results
}
Note: This algorithm is provided by Zhou Lihua (http://www.cnblogs.com/architect/archive/2009/05/06/1450489.html

Base sort

The Cardinal sort method, also known as the "bucket method" (buckets sort) or bin sort, as the name implies, it is through the key value of the information, the elements will be sorted into some "barrels", in order to achieve the role of sequencing, the cardinality ranking method is a sort of stability, the time complexity of O (Nlog (r) m) , where R is the base to be taken, and M is the number of heaps, at some point the cardinality ranking method is more efficient than other comparative sorting methods.
Base sort
Public int[] Radixsort (int[] arraytosort, int digit)
{
Low-to-high digit
for (int k = 1; k <= Digit; k++)
{
Temp array to store the sort result inside digit
int[] Tmparray = new Int[arraytosort.length];
Temp array for Countingsort
int[] Tmpcountingsortarray = new int[10]{0,0,0,0,0,0,0,0,0,0};
Countingsort
for (int i = 0; i < arraytosort.length; i++)
{
Split the specified digit from the element
int tmpsplitdigit = arraytosort[i]/(int) Math.pow (10,k-1)-(arraytosort[i]/(int) Math.pow (10,k)) *10;
Tmpcountingsortarray[tmpsplitdigit] + = 1;
}
for (int m = 1; m < m++)
{
Tmpcountingsortarray[m] + = tmpcountingsortarray[m-1];
}
Output the value to result
for (int n = arraytosort.length-1; n >= 0; n –)
{
int tmpsplitdigit = arraytosort[n]/(int) Math.pow (10,k–1) – (arraytosort[n]/(int) Math.pow (10,k)) * 10;
TMPARRAY[TMPCOUNTINGSORTARRAY[TMPSPLITDIGIT]-1] = Arraytosort[n];
Tmpcountingsortarray[tmpsplitdigit]-= 1;
}
Copy the Digit-inside sort result to source array
for (int p = 0; p < arraytosort.length; p++)
{
ARRAYTOSORT[P] = tmparray[p];
}
}
return arraytosort;
}

Count sort

The average time complexity is O (n), which is a non-comparative sorting algorithm, such as number sorting, radix sorting, and bucket sorting. These sorts are ordered because the elements themselves contain positional features, so they do not need to be compared to determine their position before and after, which can break the theoretical lower limit of the time complexity O (NLGN) of the comparison sorting algorithm.
Counting sorting is the simplest exception, which requires that the elements to be sorted are positive integers between 0 and K, so it is a very special case, and there is basically no particular application value; But on the other hand, it is the basis of the cardinal sort, or part, so simply describe:
Input array A: The element feature is a positive integer of 0-k and can have duplicate values;
Output array B: A non-decrement sequence of output a
The middle array C: size is k, and its I (0<= i <= k) index position stores the number of elements in the A-element collection that have a value of K.
The basic idea of the algorithm is:
A collection of the values of the elements in a, indexed by the values of the elements in a, and the number of values in the corresponding place of the intermediate array C.
The c is self-accumulating from the beginning, so that C stores the number of elements in front of it (including repeating elements) when the value in input array A is the current index.
Output c to output array B in turn.
///
Counting sort
Input array///the value arrange in input array//
Public int[] Countingsort (int[] Arraya, int arrange)
{
Array to store the sorted result,
The size is the same with input array.
int[] Arrayresult = new Int[arraya.length];
Array to store the direct value in sorting process
Include index 0;
Size is arrange+1;
int[] arraytemp = new int[arrange+1];
Clear up the temp array
for (int i = 0; I <= arrange; i++)
{
Arraytemp[i] = 0;
}
Now temp Array stores the count of value equal
for (int j = 0; J < Arraya.length; J + +)
{
ARRAYTEMP[ARRAYA[J]] + = 1;
}
Now temp Array stores the count of value lower and equal
for (int k = 1; k <= Arrange; k++)
{
Arraytemp[k] + = arraytemp[k-1];
}
Output the value to result
for (int m = arraya.length-1; M >= 0; M –)
{
ARRAYRESULT[ARRAYTEMP[ARRAYA[M]]–1] = arraya[m];
ARRAYTEMP[ARRAYA[M]]-= 1;
}
return arrayresult;
}

Gan sort

Heap sorting is a sort of tree selection, in which the a[n] is considered as the sequential storage structure of a complete binary tree, and the smallest element is selected using the intrinsic relationship between the parent node and the child node in the complete binary tree.

Heap sequencing is not stable. Algorithm time complexity O (nlog N).
///
Small Gan Sort
/// /// /// ///

private void Heapsort (ref double[] Dblarray)
{
for (int i = dblarray.length–1; I >= 0; I –)
{
if (2 * i + 1 < dblarray.length)
{
int Minchildrenindex = 2 * i + 1;
Compare Saozi right subtree, record index of minimum value
if (2 * i + 2 < dblarray.length)
{
if (dblarray[2 * i + 1] > dblarray[2 * i + 2])
Minchildrenindex = 2 * i + 2;
}
if (Dblarray[i] > Dblarray[minchildrenindex])
{

Exchagevalue (ref dblarray[i], ref dblarray[minchildrenindex]);
Nodesort (ref dblarray, Minchildrenindex);
}
}
}
}
///
Node ordering
//private void Nodesort (ref double[] Dblarray, int StartIndex)
{
while (2 * StartIndex + 1 < dblarray.length)
{
int minchildrenindex = 2 * StartIndex + 1;
if (2 * StartIndex + 2 < dblarray.length)
{
if (dblarray[2 * StartIndex + 1] > dblarray[2 * StartIndex + 2])
{
Minchildrenindex = 2 * StartIndex + 2;
}
}
if (Dblarray[startindex] > Dblarray[minchildrenindex])
{
Exchagevalue (ref Dblarray[startindex], ref dblarray[minchildrenindex]);
StartIndex = Minchildrenindex;
}
}
}

///
Exchange value
//private void Exchagevalue (ref double A, ref double B)
{
Double Temp = A;
A = B;
B = Temp;
}

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.