A ideia básica da classificação por bolha : compare os códigos de classificação de elementos adjacentes em sequência de trás para frente na sequência de classificação e troque se a ordem inversa for encontrada, de modo que os elementos com o código de classificação menor gradualmente se movam de trás para a frente, como bolhas na parte inferior Mesmo para cima
Demonstração de caso:
Suponha que uma matriz seja: 24,69,80,57,13
Rodada 1 :
Após a primeira classificação: (compare 24 com 69)
24,69,80,57,13
Após a segunda classificação: (compare 69 com 80)
24,69,80,57,13
Após a terceira classificação: (80 e 57 são comparados, trocados)
24,69,57,80,13
Após a quarta classificação: (80 e 13 são comparados, trocados)
24,69,57,13,80
2 ª rodada:
Após a primeira classificação: (compare 24 com 69)
24,69,57,13,80
Após a segunda classificação: (compare 69 com 57, troque)
24,57,69,13,80
Após a terceira classificação: (comparação 69 e 13, troca)
24,57,13,69,80
Rodada 3:
Após a primeira classificação: (compare 24 com 57)
24,57,13,69,80
Após a primeira classificação: (57 e 13 são comparados, trocados)
24,13,57,69,80
Rodada 4:
Após a primeira classificação: (24 e 13 são comparados e trocados)
13,24,57,69,80
Cada rodada realiza:
Rodada 1 :
package main
import "fmt"
// 冒泡排序
func bubbleSort(arr *[5]int) {
for j := 0; j < 4; j++ {
temp := 0
if (*arr)[j] > (*arr)[j+1] {
temp = (*arr)[j]
(*arr)[j] = (*arr)[j+1]
(*arr)[j+1] = temp
}
}
fmt.Println("第1轮排序后:", (*arr))
}
func main() {
arr := [5]int{24,69,80,57,13}
bubbleSort(&arr)
}
Rodada 2 :
package main
import "fmt"
// 冒泡排序
func bubbleSort(arr *[5]int) {
for j := 0; j < 4; j++ {
temp := 0
if (*arr)[j] > (*arr)[j+1] {
temp = (*arr)[j]
(*arr)[j] = (*arr)[j+1]
(*arr)[j+1] = temp
}
}
fmt.Println("第1轮排序后:", (*arr))
for j := 0; j < 3; j++ {
temp := 0
if (*arr)[j] > (*arr)[j+1] {
temp = (*arr)[j]
(*arr)[j] = (*arr)[j+1]
(*arr)[j+1] = temp
}
}
fmt.Println("第2轮排序后:", (*arr))
}
func main() {
arr := [5]int{24,69,80,57,13}
bubbleSort(&arr)
}
Rodada 3 :
package main
import "fmt"
// 冒泡排序
func bubbleSort(arr *[5]int) {
for j := 0; j < 4; j++ {
temp := 0
if (*arr)[j] > (*arr)[j+1] {
temp = (*arr)[j]
(*arr)[j] = (*arr)[j+1]
(*arr)[j+1] = temp
}
}
fmt.Println("第1轮排序后:", (*arr))
for j := 0; j < 3; j++ {
temp := 0
if (*arr)[j] > (*arr)[j+1] {
temp = (*arr)[j]
(*arr)[j] = (*arr)[j+1]
(*arr)[j+1] = temp
}
}
fmt.Println("第2轮排序后:", (*arr))
for j := 0; j < 2; j++ {
temp := 0
if (*arr)[j] > (*arr)[j+1] {
temp = (*arr)[j]
(*arr)[j] = (*arr)[j+1]
(*arr)[j+1] = temp
}
}
fmt.Println("第3轮排序后:", (*arr))
}
func main() {
arr := [5]int{24,69,80,57,13}
bubbleSort(&arr)
}
Rodada 4 :
package main
import "fmt"
// 冒泡排序
func bubbleSort(arr *[5]int) {
for j := 0; j < 4; j++ {
temp := 0
if (*arr)[j] > (*arr)[j+1] {
temp = (*arr)[j]
(*arr)[j] = (*arr)[j+1]
(*arr)[j+1] = temp
}
}
fmt.Println("第1轮排序后:", (*arr))
for j := 0; j < 3; j++ {
temp := 0
if (*arr)[j] > (*arr)[j+1] {
temp = (*arr)[j]
(*arr)[j] = (*arr)[j+1]
(*arr)[j+1] = temp
}
}
fmt.Println("第2轮排序后:", (*arr))
for j := 0; j < 2; j++ {
temp := 0
if (*arr)[j] > (*arr)[j+1] {
temp = (*arr)[j]
(*arr)[j] = (*arr)[j+1]
(*arr)[j+1] = temp
}
}
fmt.Println("第3轮排序后:", (*arr))
for j := 0; j < 1; j++ {
temp := 0
if (*arr)[j] > (*arr)[j+1] {
temp = (*arr)[j]
(*arr)[j] = (*arr)[j+1]
(*arr)[j+1] = temp
}
}
fmt.Println("第4轮排序后:", (*arr))
}
func main() {
arr := [5]int{24,69,80,57,13}
bubbleSort(&arr)
}
Resuma as regras para obter o código final:
package main
import "fmt"
// 冒泡排序
func bubbleSort(arr *[5]int) {
fmt.Println("排序前:", (*arr))
// 总结规律:先内层(每一轮)再外层,内层n-1-i次,外层n-1
for i := 0; i < len(*arr)-1; i++ {
for j := 0; j < len(*arr)-1-i; j++ {
temp := 0
if (*arr)[j] > (*arr)[j+1] {
temp = (*arr)[j]
(*arr)[j] = (*arr)[j+1]
(*arr)[j+1] = temp
}
}
}
fmt.Println("排序后:", (*arr))
}
func main() {
arr := [5]int{24, 69, 80, 57, 13}
bubbleSort(&arr)
}