Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction of various sorting algorithms of Golang

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "the introduction of various sorting algorithms of Golang". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "introduction to various sorting algorithms of Golang".

Bubble sort: iterate through a list multiple times. Compare two adjacent items and swap items that are misordered in size. Each pair

Once the list is traversed, there is a maximum or minimum item in the correct position. In general, each data item in the list will be in the

Its corresponding position is "bubbling".

Diagram of the first sort of bubbling

Selective sorting: a little better than bubbling sorting. It only exchanges data once for each traversal of the list, that is, it does an ergodic search.

Go to the largest or smallest item, and then change it to the correct position after traversing.

Select sort diagram

Insert sort: always keep a ranked child table at the top, and then each new data item is "inserted" into the previous child table

Add an item to the arranged subtable.

Insert sort diagram

Code:

Package main

Import "fmt"

Func main () {

Var (

DataArr = [] int {3, 9, 7, 5, 61, 85, 99, 456,21,456}

)

Fmt.Println (sortMaoBao (dataArr, true))

Fmt.Println (sortChoice (dataArr, true))

Fmt.Println (sortInsert (dataArr, true))

}

/ *

Bubble sort: iterate through a list multiple times. Compare two adjacent items and swap items that are misordered in size. Each pair

Once the list is traversed, there is a maximum or minimum item in the correct position. In general, each data item in the list will be in the

Its corresponding position is "bubbling".

Selective sorting: a little better than bubbling sorting. It only exchanges data once for each traversal of the list, that is, it does an ergodic search.

Go to the largest or smallest item, and then change it to the correct position after traversing.

Insert sort: always keep a ranked child table at the top, and then each new data item is "inserted" into the previous child table

Add an item to the arranged subtable. , /

/ / sortMaoBao sortCond true is sorted from large to small, and vice versa

Func sortMaoBao (data [] int, sortCond bool) [] int {

Var (

L = len (data)-1

)

For l > 0 {

For I: = 0; I

< l; i++ { // 每一个当前位置与下一位进行比较,所有的数据都进行比较完毕,就可以得到排序后的数据 if sortCond { if data[i+1] >

Data [i] {

Data [I], data [I] = data [I], data [I]

}

} else {

If data [iTun1]

< data[i] { data[i+1], data[i] = data[i], data[i+1] } } } l-- } return data } // sortChoice sortCond true 大到小排序, 反之小到大 func sortChoice(data []int, sortCond bool) []int { var ( l = len(data) - 1 ) for l >

0 {

Var initIndex int

/ / the initial value is compared with the first one

For I: = 1; I

< l+1; i++ { if sortCond { if data[i] < data[initIndex] { initIndex = i } } else { if data[i] >

Data [initIndex] {

InitIndex = I

}

}

}

Data [l], data [initIndex] = data [initIndex], data [l]

L Murray-

}

Return data

}

/ / sortInsert sortCond true is sorted from large to small, and vice versa

Func sortInsert (data [] int, sortCond bool) [] int {

For l: = 1; l

< len(data); l++ { currentValue := data[l] position := l if !sortCond { for position >

0 & & data [position-1] > currentValue {

Data [position] = data [position-1]

Position--

Data [position] = currentValue

}

} else {

For position > 0 & & data [position-1] < currentValue {

Data [position] = data [position-1]

Position--

Data [position] = currentValue

}

}

}

Return data

}

At this point, I believe that you have a deeper understanding of the "introduction of various sorting algorithms of Golang", you might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report