Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【算法】经典排序算法 #21

Open
yankewei opened this issue Mar 19, 2020 · 0 comments
Open

【算法】经典排序算法 #21

yankewei opened this issue Mar 19, 2020 · 0 comments

Comments

@yankewei
Copy link
Owner

yankewei commented Mar 19, 2020

找工作的时候就过来温习一遍

选择排序

找最小值,然后放到前边
func bubbleSortFront(s []int) {
    // 外层循环,每次循环把最小的值放到第i的位置
    for i := 0; i < len(s); i++ {
	for j := i + 1; j < len(s); j++ {
	    if s[i] > s[j] {
		s[i], s[j] = s[j], s[i]
	    } else {
		continue
	    }
	}
    }
}
找最大值,然后放到末尾
func bubbleSortEnd(s []int) {
    e := len(s) - 1
    // 每次排序,把最大值放到第e个的位置
    for e >= 0 {
	f := 0
	for f <= e {
	    if s[f] > s[e] {
		s[e], s[f] = s[f], s[e]
	    }
	    f++
	}
       e--
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant