Go bubble sorting (the most detailed on the entire network)

The basic idea of ​​bubble sorting : compare the sorting codes of adjacent elements in sequence from back to front in the sorting sequence, and exchange if the reverse order is found, so that the elements with the smaller sorting code will gradually move from back to front, just like bubbles at the bottom of the water Same up

Case demonstration:

Suppose an array is: 24,69,80,57,13

Round 1 :

After the first sort: (Compare 24 with 69)

24,69,80,57,13

After the second sort: (Compare 69 with 80)

24,69,80,57,13

After the third sort: (80 and 57 are compared, exchanged)

24,69,57,80,13

After the fourth sort: (80 and 13 are compared, exchanged)

24,69,57,13,80

Round 2:

After the first sort: (Compare 24 with 69)

24,69,57,13,80

After the second sort: (Compare 69 with 57, exchange)

24,57,69,13,80

After the third sorting: (69 and 13 comparison, exchange)

24,57,13,69,80

Round 3:

After the first sort: (Compare 24 with 57)

24,57,13,69,80

After the first sort: (57 and 13 are compared, exchanged)

24,13,57,69,80

Round 4:

After the first sort: (24 and 13 are compared and exchanged)

13,24,57,69,80

Each round realizes:

Round 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)
}

Round 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)
}

Round 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)
}

Round 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)
}

Summarize the rules to get the final code:

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)
}

 

Guess you like

Origin blog.csdn.net/qq2942713658/article/details/113029588