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

推荐订阅源

J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
L
LangChain Blog
C
Check Point Blog
F
Fortinet All Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Blog — PlanetScale
Blog — PlanetScale
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
Recent Announcements
Recent Announcements
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
Netflix TechBlog - Medium
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Microsoft Azure Blog
Microsoft Azure Blog
U
Unit 42
The Cloudflare Blog
月光博客
月光博客
有赞技术团队
有赞技术团队
G
Google Developers Blog
Vercel News
Vercel News

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-06-02 · via Posts on WKLKEN THINKING

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

List:

0.概念+示例分析
1.插入排序实现
  1. start

基本概念:

维基百科http://zh.wikipedia.org/wiki/%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F

插入排序,简单来说就是每次拿一个新的数,将其插入到有序序列中.

示例:

[8, 4, 3, 1, 6, 9, 2, 7]

index- 1 #从第二个数开始

move 1 , change-> [4, 8, 3, 1, 6, 9, 2, 7]  #移动一次,插入

index- 2

move 2 , change-> [3, 4, 8, 1, 6, 9, 2, 7] #移动两次,插入

index- 3

move 3 , change-> [1, 3, 4, 8, 6, 9, 2, 7]

index- 4

move 1 , change-> [1, 3, 4,6, 8, 9, 2, 7]

index- 5

move 0 ,  nochange -> [1, 3, 4, 6, 8, 9, 2, 7]

index- 6

move 5 , change-> [1,2, 3, 4, 6, 8, 9, 7]

index- 7

move 2 , change-> [1, 2, 3, 4, 6,7, 8, 9]

[1, 2, 3, 4, 6, 7, 8, 9]
  1. start

插入排序python实现

#!/usr/bin/python
# -*- coding:utf-8 -*-
#插入排序
#@author: wklken@yeah.net

def insert_sort(l):
    print l
    for i in range(1,len(l)): #从第二个元素开始
        value = l[i]
        while i >= 1 and l[i-1] > value:
            l[i] = l[i-1]
            i -= 1
        l[i] = value
    return l
l = [8, 4, 3, 1, 6, 9, 2, 7]
print insert_sort(l)

改进及优化:

1.加入监控,已排序完成直接退出

2.使用二分插入排序,即,处理某个节点往前插入的时候,使用二分查找插入