惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Martin Fowler
Martin Fowler
I
InfoQ
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
博客园 - 聂微东
Microsoft Azure Blog
Microsoft Azure Blog
D
Docker
博客园 - 三生石上(FineUI控件)
Y
Y Combinator Blog
博客园 - Franky
Engineering at Meta
Engineering at Meta
B
Blog
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
V
Visual Studio Blog

Posts on WKLKEN THINKING

apisix 中的 lrucache apisix 中的服务发现机制 apisix 中的负载均衡 apisix etcd机制 聊聊框架 关于 k8s 的 zero downtime deployment 一些建议 apisix 遇到的一些问题 关于在除夕前一天换了一个洗衣机的故事 Django DRF 性能优化 DRF 的一些实践 Part1: Serializer DRF继承关系图 Better Code: 关于接口的灵活性 新的仓库: wklken/naming 缓存使用的一些经验 Better Code: 抽象: 可扩展性与可维护性的抉择 Better Code: 异常时, 该提示用户哪些信息? Better Code: 更好的异常日志打印 Go: some libs Go: go-redis/cache升级的坑 Go: logrus性能提升 Go: gin validation 远程办公的一点总结 Go: 开发过程中的一些bug 项目管理实践: 风险驱动开发 Go: 一种error wrap调用链处理方式 漫谈技术选型 Go: 基于 apitest 做handler层单元测试 Go: go-sql-driver interpolateparams参数优化 [分享]深度工作 你需要更多的思考时间
数据结构&算法实践—【排序|选择排序】选择排序
2012-05-27 · via Posts on WKLKEN THINKING

排序»选择排序»选择排序

List:

0.概念+伪代码+示例分析
1.选择排序实现
2.Question
  1. start

基本概念:

维基百科http://zh.wikipedia.org/wiki/%E9%81%B8%E6%93%87%E6%8E%92%E5%BA%8F

伪代码:

function selectSort(A : list[1..n]) {
    index = n
    while (index > 1): #共有n-1次选择
    {
        max_index = index
        for i from index  downto 1 {  #每次从剩余序列选出最大的
        if(A[i] > A[max_index)
        {
            max_index = i
            }
        }
        swap(A[index], A[max_index ])  #将最大的换到后面
        index = index -1
    }
}

示例:

[49, 38, 65, 97, 76, 13, 27]

Current index 6 value= 27 Max index: 3 value= 97
exchange -> [49, 38, 65, 27, 76, 13, 97]
Current index 5 value= 13 Max index: 4 value= 76
exchange -> [49, 38, 65, 27, 13, 76, 97]
Current index 4 value= 13 Max index: 2 value= 65
exchange -> [49, 38, 13, 27, 65, 76, 97]
Current index 3 value= 27 Max index: 0 value= 49
exchange -> [27, 38, 13, 49, 65, 76, 97]
Current index 2 value= 13 Max index: 1 value= 38
exchange -> [27, 13, 38, 49, 65, 76, 97]
Current index 1 value= 13 Max index: 0 value= 27
exchange -> [13, 27, 38, 49, 65, 76, 97]
Done
  1. start

实现代码

:::python
def select_sort(l):
    index = len(l) -1
    while index:
        max_index = index
        for i in range(index):
            if l[i] > l[max_index]:
                max_index = i
        if l[max_index] > l[index]:
            l[index],l[max_index] = l[max_index], l[index]
        index -= 1
  1. start

A.概念,过程描述?

B.交换次数,比较次数,赋值次数?

C. 时间复杂度?空间复杂度?是否是稳定排序?

D.适用场景,何种情况下表现最优