Using the Golang Learning Algorithm (1)-Sorting

Source: Internet
Author: User
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.



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.