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).
Personal Summary:
Select Sort, is to have another cursor, each time at the time of comparison, record the maximum, or the location of the minimum value, after traversing through, with the outer layer of each record position, do position exchange. Why is it called the selection sort, which is estimated to be the reason, each time traversal, choose the largest or the smallest out. The algorithm is therefore named.
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)
Personal Summary:
Prevent data from being out of bounds on the boundary of the second cycle. To ensure that the data that has bubbled up to the top of the page is not compared by a second query, it is necessary to limit the inner and outer loops separately. The outer loop is used to control the number of comparisons, and the inner loop is used to iterate through the array elements and compare them, so the outer layer needs to be limited by the result of the inner layers, which part of the inner layer needs to be compared, and the outer record should be changed dynamically.
PackageMainImport("FMT")typeSortinterfaceInterface{sort ()}typeSortorstruct{Namestring}funcMain () {arry: = []int{6,1,3,5,8,4,2,0,9,7} learnsort: = Sortor{name:"bubble sort-small to large-stable--n*n---"} learnsort.sort (Arry) fmt. Println (Learnsort.name, Arry)}func(Sorter sortor) sort (Arry []int) {Done: =trueArrylength: =Len(Arry) forI: =0; I < arrylength && done; i++ {done =false forJ: =0; J < Arrylength-i-1; J + + {done =true ifARRY[J] > Arry[j+1] {t: = arry[j] arry[j] = arry[j+1] Arry[j+1] = t}}}
Results:
/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]Bubble Sort--from small to large--Stable--N*n--- [0 1 2 3 4 5 6 7 8 9]Success: Process Exit code 0.
Quick Sort
Quick Sort by C. A. R. Hoare was introduced in 1962. Its basic idea is: by a trip to sort the data to be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.
Algorithm steps:
Set to sort the array is a[0] ... A[n-1], the first arbitrary selection of data (usually the first number of the array) as the key data, and then all the smaller than the number of it in front of it, all the larger than its number is placed behind it, this process is called a fast sort of a trip. It is important to note that fast sorting is not a stable sorting algorithm, that is, the relative position of multiple identical values may change at the end of the algorithm.
A quick sort of algorithm is:
1) Set two variables I, J, at the beginning of the order: i=0,j=n-1;
2) with the first array element as the key data, assign the value to key, i.e. key=a[0];
3) Forward search from J, that is, after starting the Forward search (J –), find the first value less than key A[j], will a[j] and A[i] interchange;
4) Backward search from I, that is, start backward search (i++), find the first a[i] greater than key], interchange a[i] and A[J];
5) Repeat 3rd, 4, until i=j, (3,4 step, did not find the qualifying value, that is, 3 a[j] is not less than the key,4 A[i] is not larger than the time to change the value of J, I, so j=j-1,i=i+1, until found. Locate the value that matches the condition, and the J pointer position does not change when I exchange it. In addition, I==J this process must be exactly when the i+ or J completes, at which time the loop ends).
Fast sequencing is not stable. The optimal condition algorithm time complexity O (nlog2n), the worst O (n ^2).
Personal Summary:
The fast sort is actually the first number of the array as the middle value, start sorting, when this value is not the best intermediate value, there will be the worst case, when a sort is completed, ready to enter the recursion, recursion is slice, recursive exit condition is, when the slice has only compared with their own, That is, it becomes the middle value, slice is 1.
PackageMainImport("FMT")typeSortinterfaceInterface{sort ()}typeSortorstruct{Namestring}funcMain () {arry: = []int{6,1,3,5,8,4,2,0,9,7} learnsort: = Sortor{name:"Fast sequencing-small to large-unstable--nlog2n worst n*n---"} learnsort.sort (Arry) fmt. Println (Learnsort.name, Arry)}func(Sorter sortor) sort (Arry []int) {if Len(Arry) <=1{return //recursive termination condition, slice becomes 0. } Mid: = Arry[0] I: =1 //arry[0] is the mid-value mid, so you want to compare from 1 onwardsHead, Tail: =0,Len(Arry)-1 forHead < Tail {ifArry[i] > Mid {arry[i], arry[tail] = Arry[tail], arry[i]Fast exchange of variable values in//go, no intermediate variable temp requiredtail--}Else{Arry[i], arry[head] = Arry[head], arry[i] head++ i++}} Arry[head] = Mid sorter.sort (Arry[:head])//The head here is the middle value. The left side is smaller than it, the right side is larger than it, and the recursion begins. Sorter.sort (Arry[head+1:])}
Operation Result:
/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]Quick Sort--from small to large--not stable--nlog2n Worst N*n--- [0 1 2 3 4 5 6 7 8 9]Success: Process Exit code 0.
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).
Personal Summary:
The starting point of the algorithm is a value that needs to be sorted, followed by the previously sorted data execution, non-stop move house operation. This should be sorted by a value of 1, because the 0 position of the data must be in good order AH. Now, do you understand? Ha ha. Small experience, we learn together together.
PackageMainImport("FMT")typeSortinterfaceInterface{sort ()}typeSortorstruct{Namestring}funcMain () {arry: = []int{6,1,3,5,8,4,2,0,9,7} learnsort: = Sortor{name:"Insertion sort-small to large-stable--n*n---"} learnsort.sort (Arry) fmt. Println (Learnsort.name, Arry)}func(Sorter sortor) sort (Arry []int) {arrylength: =Len(Arry) forI, J: =1,0; i < arrylength; i++ {//i starts at 1 and is the value to be inserted. Starting from 1, the fact is to prepare for the move house. Because next, I want to order the data in front, in turn move house. Temp: = Arry[i]//temp is the position to begin sorting, starting from 1. is also forj = i; J >0&& Arry[j-1] > temp; j--{//each time from the outer loop of the counter start, compared with a temp variable, big words, move forward a nest. //Because the front is all sorted, so it is move house in turn. There is no data loss.ARRY[J] = Arry[j-1]} Arry[j] = Temp///the appropriate position to be moved out to this temp variable at the end. That is, each time you want to insert a value. }}
Operation Result:
/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]Insert Sort--from small to large--Stable--N*n--- [0 1 2 3 4 5 6 7 8 9]Success: Process Exit code 0.
Summary of Go language ten sort algorithm