This is a creation in Article, where the information may have evolved or changed.
Objective
Finally feel the importance of the algorithm. So I intend to continue to study.
In fact, the algorithm is not related to language, with what language can be achieved key is the idea, recently just learning Golang. The algorithm is intended to be written using Golang completion.
The IDE is not used, the plug-in for sublime2+ Golang is used, and then compiled using the command line.
The development efficiency is also not low, also supports the language automatic completion.
Build the Environment "http://blog.csdn.net/freewebsys/article/details/17955255"
Learn the parameter book, using the "Introduction to Algorithms," that is, knocking on the books on the code to learn.
1, inserting sort
Package MainFunc Insertsort (A []int) {for J: = 1; j < Len (a); J + + {var key = A[j]var i = j-1for; I >= 0 && ; A[i] > key; i--{//a[i] > key is sorted in ascending order, A[i] < key is sorted in descending order. A[I+1] = A[i]}a[i+1] = Keyfor _, V: = Range a {print (V, ",")}println ()}}func main () {a: = []int{5, 2, 4, 6, 1, 3}for _, V : = Range A {print (V, ",")}println () Insertsort (a)}
Operation Result:
5,2,4,6,1,3,2,5,4,6,1,3,2,4,5,6,1,3,2,4,5,6,1,3,1,2,4,5,6,3,1,2,3,4,5,6,
Summary: The insertion algorithm, starting from the second element, loops through the entire array, finds I that is smaller than the current element, and then swaps the data. Until the entire data is cycled. The example in the book is very graphic, like a sort of poker.
2, bubble sort
Code:
Package MainFunc Bubblesort (A []int) {for I: = 0, I <= Len (a)-2; i++ {for J: = Len (a)-1; J >= i+1; j--{if a[j] &l T A[j-1] {a[j], a[j-1] = a[j-1], a[j]//data exchange}}}}func Main () {a: = []int{5, 2, 4, 6, 1, 3}for _, V: = Range a {print (V, ",")} println () Bubblesort (a) for _, V: = Range a {print (V, ",")}println ()}
Operation Result:
5,2,4,6,1,3,1,2,3,4,5,6,
Summary: Bubble sort, compare from the last data forward, and exchange data if it is smaller than the previous data.