This is a creation in Article, where the information may have evolved or changed.
One: Insert sort
1: Algorithm Description
Starting with the first element, the element can be thought to have been sorted
Takes the next element and scans the sequence of elements that have been sorted from back to forward
If the element (sorted) is greater than the new element, move the element to the next position
Repeat step 3 until you find the sorted element is less than or equal to the position of the new element
After inserting a new element into the position
Repeat step 2~5
2: Code implementation
Func Insertsort (p []int) {
For i: = 1; i < Len (p); i++ {
For j: = I-1; J >= 0 && p[j+1] < p[j]; j--{
P[J+1], p[j] = P[j], p[j+1]
}
}
}
Two: Bubble sort
1: Algorithm Description
Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. When this is done, the final element will be the maximum number.
Repeat the above steps for all elements, except for the last one.
Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.
2: Program Implementation
Func Bubblesort (p []int) {
For i: = 1; i < Len (p)-1; i++ {
For j: = 0; J < Len (p)-I.; J + + {
If P[J] > p[j+1] {
P[J+1], p[j] = P[j], p[j+1]
}
}
}
}