This is a creation in Article, where the information may have evolved or changed.
Direct insertion Sorting algorithm Golang implementation version:
Insert Algorithm Summary:
Create an empty list, first in the array to sort out a random data, placed at the beginning of the new list, and then kept from the original array
Get the data and compare it with the data in the list, put it on the right end of the list, and put it on the left side of the list, loop until the end,
Sorting is complete.
Package Mainimport ("Container/list", "FMT") var old []int = []int{ 432,432432,4234,333,333,21,22,3,30,8,20,2,7,9,50,80,1,4}func Main () {FMT. Println (' old ' array: ', old ') Res,_: = Insertionsort (old) I: = 0for e: = Res. Front (); Nil! = e; E = E.next () {//fmt. Println ("[", I, "]:", E.value. ( int)) FMT. Printf ("[%d]:%d\n", I, E.value. ( int)) i + = 1}}func insertionsort (old []int) (Sorteddata *list. List, err error) {sorteddata = list. New () Sorteddata.pushback (old[0]) Size: = Len (old) for i: = 1; i < size; I++{v: = old[i]e: = Sorteddata.front () for nil! = E{if E.value. ( int) >= V{sorteddata.insertbefore (V, e) break}e = E.next ()}//the biggest,put @v on the back of the listif nil = = E{sorte Ddata.pushback (v)}}return}