Summary of Go language ten sort algorithm

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

Select sort

The basic idea of choosing a sort is to treat a sequence of records to be processed n-1 times, and the first I-pass processing is to be l[i. N] The smallest person in the l[i] exchange position. Thus, after I pass the processing, the position of the first I record is already correct.

Choosing a sort is not stable. The complexity of the algorithm is O (n ^2).

 PackageMainImport("FMT")typeSortinterfaceInterface{sort ()}typeSortorstruct{Namestring}funcMain () {arry: = []int{6,1,3,5,8,4,2,0,9,7} learnsort: = Sortor{name:"Select sort-Small to large-unstable--n*n---"} learnsort.sort (Arry) fmt. Println (Learnsort.name, Arry)}func(Sorter sortor) sort (Arry []int) {arrylength: =Len(Arry) forI: =0; i < arrylength; i++ {min: = i forJ: = i +1; J < Arrylength; J + + {ifARRY[J] < Arry[min] {min = j}} t: = Arry[i] arry[i] = arry[min] Arry[min] = t}}

The output is:

/usr/local/go/bin/go Build -I [/users/liuhanlin/go/src/go_learn]Success: Process Exit code 0./users/liuhanlin/go/src/go_learn/go_learn  [/users/liuhanlin/go/src/go_learn]Select Sort--from small to large--not stable--N*n--- [0 1 2 3 4 5 6 7 8 9]Success: Process Exit code 0.

Bubble sort
The bubble sort method is the simplest sort method. The basic idea of this approach is to think of the elements to be sorted as "bubbles" that are vertically arranged, smaller elements lighter and thus upward. In the bubble sorting algorithm we have to deal with this "bubble" sequence several times. The so-called process, is to check the sequence from the bottom up, and always pay attention to the sequence of two adjacent elements is correct. If the order of two adjacent elements is found to be incorrect, that is, the "light" elements are below, exchanging their positions. Obviously, after processing, the "lightest" element floats to its highest position, and after two times, the "light" element floats to the next high position. In the second pass, you do not have to check because the element at the highest position is already the lightest element. In general, when I pass the processing, I do not have to check the higher position above the elements, because after the previous i-1 the processing, they have been correctly sequenced.
The bubbling sort is stable. Algorithm time complexity is O (n ^2)
Class Ebullitionsorter
{
public void Sort (int[] arr)
{
int I, j, temp;
bool done = false;
j = 1;
while (J < arr. Length) && (!done))//Judging Lengths
{
Done = true;
for (i = 0; i < arr. Length-j; i++)
{
if (Arr[i] > arr[i + 1])
{
Done = false;
temp = Arr[i];
Arr[i] = arr[i + 1];//exchanging data
Arr[i + 1] = temp;
}
}
j + +;
}
}
}

Quick Sort
Quick sort is an essential improvement to the bubbling sort. Its basic idea is that the length of the sequencing sequence can be drastically reduced after a scan. In a bubbling sort, a scan can only ensure that the number of maximum values is moved to the correct position, while the length of the sequence to be sorted may be reduced by only 1. Quick sort by a scan, you can make sure that the number of the left is smaller and the number on the right is larger than it. It then uses the same method to manipulate the left and right sides of the number until there is only one element to the left of the datum point.

The

Quick sort is not stable. The optimal condition algorithm time complexity O (nlog2n), the worst O (n ^2).
Class Quicksorter
{
private void swap (ref int l, ref int r)
{
int temp;
temp = l;
L = r;
R = temp;
}
public void Sort (int[] list, int. low, int high)
{
int pivot;//storage pivot point
int L, R;
int mid;
if (high <=)
return;
Else if (high = = low + 1)
{
if (List[low] > List[high])
Swap (ref List[low], ref list[high]);
return;
}
Mid = (low + high) >> 1;
pivot = List[mid];
Swap (ref List[low], ref list[mid]);
L = low + 1;
R = high;
do
{
while (l <= R && List[l] < pivot)
l++;
while (List[r] >= pivot)
R –;
if (L < R)
Swap (ref list[l], ref list[r]);
} while (L < R);
List[low] = List[r];
List[r] = pivot;
if (low + 1 < R)
Sort (list, low, r-1);
if (r + 1 < High)
Sort (list, R + 1, high);
}
}

Insert Sort
The basic idea of inserting a sort is that, after i-1-through, l[1..i-1] is in the right order. I-pass processing only l[i] into the appropriate position of l[1..i-1], so that l[1..i] is a sequence of orderly. To achieve this, we can use a sequential comparison method. First compare L[i] and l[i-1], if l[i-1]≤l[i], then L[1..I] has been ordered, the first time the processing is finished, otherwise exchange l[i] and l[i-1] position, continue to compare l[i-1] and l[i-2] until a certain position J (1≤j≤i-1) is found, Make l[j]≤l[j+1]. Figure 1 illustrates the process of inserting a sequence of 4 elements, which requires (a), (b), (c) three insertions.
The direct insert sort is stable. The time complexity of the algorithm is O (n ^2).

public class Insertionsorter
{
public void Sort (int[] arr)
{
for (int i = 1; i < arr. Length; i++)
{
int t = arr[i];
int j = i;
while ((J > 0) && (arr[j-1] > t))
{
ARR[J] = arr[j-1];//Exchange Order
–j
}
ARR[J] = t;
}
}
}

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 insert sort is done within each group, then the second increment D2

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.