Algorithms - Insertion Sort - 插入排序
Insertion Sort - 插入排序
插入排序算法的 '时间复杂度' 是输入规模的二次函数, 深度抽象后表示为, n 的二次方. import time, random
F = 0
alist = []
while F < 13:
F += 1
alist.append(random.randrange(0,100))
j =1
print("List-O",alist)
startT =time.time()
while j < alist.__len__():
factor = alist[j]
j += 1
print("-",factor)
i = j -1
while i > 0:
i -= 1
if alist[i] > factor:
alist[i],alist[i+1] = alist[i+1],alist[i]
print("Steps", alist)
endT = time.time()
print("List-S", alist)
print("Time elapsed :", endT - startT) Output,
List-O [67, 22, 94, 33, 81, 92, 65, 94, 56, 10, 55, 5, 64]
- 22
Steps [22, 67, 94, 33, 81, 92, 65, 94, 56, 10, 55, 5, 64]
- 94
Steps [22, 67, 94, 33, 81, 92, 65, 94, 56, 10, 55, 5, 64]
Steps [22, 67, 94, 33, 81, 92, 65, 94, 56, 10, 55, 5, 64]
- 33
Steps [22, 67, 33, 94, 81, 92, 65, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 67, 94, 81, 92, 65, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 67, 94, 81, 92, 65, 94, 56, 10, 55, 5, 64]
- 81
Steps [22, 33, 67, 81, 94, 92, 65, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 67, 81, 94, 92, 65, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 67, 81, 94, 92, 65, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 67, 81, 94, 92, 65, 94, 56, 10, 55, 5, 64]
- 92
Steps [22, 33, 67, 81, 92, 94, 65, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 67, 81, 92, 94, 65, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 67, 81, 92, 94, 65, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 67, 81, 92, 94, 65, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 67, 81, 92, 94, 65, 94, 56, 10, 55, 5, 64]
- 65
Steps [22, 33, 67, 81, 92, 65, 94, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 67, 81, 65, 92, 94, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 67, 65, 81, 92, 94, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 65, 67, 81, 92, 94, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 65, 67, 81, 92, 94, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 65, 67, 81, 92, 94, 94, 56, 10, 55, 5, 64]
- 94
Steps [22, 33, 65, 67, 81, 92, 94, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 65, 67, 81, 92, 94, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 65, 67, 81, 92, 94, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 65, 67, 81, 92, 94, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 65, 67, 81, 92, 94, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 65, 67, 81, 92, 94, 94, 56, 10, 55, 5, 64]
Steps [22, 33, 65, 67, 81, 92, 94, 94, 56, 10, 55, 5, 64]
- 56
Steps [22, 33, 65, 67, 81, 92, 94, 56, 94, 10, 55, 5, 64]
Steps [22, 33, 65, 67, 81, 92, 56, 94, 94, 10, 55, 5, 64]
Steps [22, 33, 65, 67, 81, 56, 92, 94, 94, 10, 55, 5, 64]
Steps [22, 33, 65, 67, 56, 81, 92, 94, 94, 10, 55, 5, 64]
Steps [22, 33, 65, 56, 67, 81, 92, 94, 94, 10, 55, 5, 64]
Steps [22, 33, 56, 65, 67, 81, 92, 94, 94, 10, 55, 5, 64]
Steps [22, 33, 56, 65, 67, 81, 92, 94, 94, 10, 55, 5, 64]
Steps [22, 33, 56, 65, 67, 81, 92, 94, 94, 10, 55, 5, 64]
- 10
Steps [22, 33, 56, 65, 67, 81, 92, 94, 10, 94, 55, 5, 64]
Steps [22, 33, 56, 65, 67, 81, 92, 10, 94, 94, 55, 5, 64]
Steps [22, 33, 56, 65, 67, 81, 10, 92, 94, 94, 55, 5, 64]
Steps [22, 33, 56, 65, 67, 10, 81, 92, 94, 94, 55, 5, 64]
Steps [22, 33, 56, 65, 10, 67, 81, 92, 94, 94, 55, 5, 64]
Steps [22, 33, 56, 10, 65, 67, 81, 92, 94, 94, 55, 5, 64]
Steps [22, 33, 10, 56, 65, 67, 81, 92, 94, 94, 55, 5, 64]
Steps [22, 10, 33, 56, 65, 67, 81, 92, 94, 94, 55, 5, 64]
Steps [10, 22, 33, 56, 65, 67, 81, 92, 94, 94, 55, 5, 64]
- 55
Steps [10, 22, 33, 56, 65, 67, 81, 92, 94, 55, 94, 5, 64]
Steps [10, 22, 33, 56, 65, 67, 81, 92, 55, 94, 94, 5, 64]
Steps [10, 22, 33, 56, 65, 67, 81, 55, 92, 94, 94, 5, 64]
Steps [10, 22, 33, 56, 65, 67, 55, 81, 92, 94, 94, 5, 64]
Steps [10, 22, 33, 56, 65, 55, 67, 81, 92, 94, 94, 5, 64]
Steps [10, 22, 33, 56, 55, 65, 67, 81, 92, 94, 94, 5, 64]
Steps [10, 22, 33, 55, 56, 65, 67, 81, 92, 94, 94, 5, 64]
Steps [10, 22, 33, 55, 56, 65, 67, 81, 92, 94, 94, 5, 64]
Steps [10, 22, 33, 55, 56, 65, 67, 81, 92, 94, 94, 5, 64]
Steps [10, 22, 33, 55, 56, 65, 67, 81, 92, 94, 94, 5, 64]
- 5
Steps [10, 22, 33, 55, 56, 65, 67, 81, 92, 94, 5, 94, 64]
Steps [10, 22, 33, 55, 56, 65, 67, 81, 92, 5, 94, 94, 64]
Steps [10, 22, 33, 55, 56, 65, 67, 81, 5, 92, 94, 94, 64]
Steps [10, 22, 33, 55, 56, 65, 67, 5, 81, 92, 94, 94, 64]
Steps [10, 22, 33, 55, 56, 65, 5, 67, 81, 92, 94, 94, 64]
Steps [10, 22, 33, 55, 56, 5, 65, 67, 81, 92, 94, 94, 64]
Steps [10, 22, 33, 55, 5, 56, 65, 67, 81, 92, 94, 94, 64]
Steps [10, 22, 33, 5, 55, 56, 65, 67, 81, 92, 94, 94, 64]
Steps [10, 22, 5, 33, 55, 56, 65, 67, 81, 92, 94, 94, 64]
Steps [10, 5, 22, 33, 55, 56, 65, 67, 81, 92, 94, 94, 64]
Steps [5, 10, 22, 33, 55, 56, 65, 67, 81, 92, 94, 94, 64]
- 64
Steps [5, 10, 22, 33, 55, 56, 65, 67, 81, 92, 94, 64, 94]
Steps [5, 10, 22, 33, 55, 56, 65, 67, 81, 92, 64, 94, 94]
Steps [5, 10, 22, 33, 55, 56, 65, 67, 81, 64, 92, 94, 94]
Steps [5, 10, 22, 33, 55, 56, 65, 67, 64, 81, 92, 94, 94]
Steps [5, 10, 22, 33, 55, 56, 65, 64, 67, 81, 92, 94, 94]
Steps [5, 10, 22, 33, 55, 56, 64, 65, 67, 81, 92, 94, 94]
Steps [5, 10, 22, 33, 55, 56, 64, 65, 67, 81, 92, 94, 94]
Steps [5, 10, 22, 33, 55, 56, 64, 65, 67, 81, 92, 94, 94]
Steps [5, 10, 22, 33, 55, 56, 64, 65, 67, 81, 92, 94, 94]
Steps [5, 10, 22, 33, 55, 56, 64, 65, 67, 81, 92, 94, 94]
Steps [5, 10, 22, 33, 55, 56, 64, 65, 67, 81, 92, 94, 94]
Steps [5, 10, 22, 33, 55, 56, 64, 65, 67, 81, 92, 94, 94]
List-S [5, 10, 22, 33, 55, 56, 64, 65, 67, 81, 92, 94, 94]
Time elapsed : 0.015672683715820312
Algorithms - Insertion Sort - 插入排序的更多相关文章
- [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)
Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...
- Algorithms - Insertion sort
印象 图1 插入排序过程 思想 插入排序(Insertion Sort)的主要思想是不断地将待排序的元素插入到有序序列中,是有序序列不断地扩大,直至所有元素都被插入到有序序列中. 分析 时间复杂度: ...
- [Algorithms] Insertion sort algorithm using TypeScript
Insertion sort is a very intuitive algorithm as humans use this pattern naturally when sorting cards ...
- insertion sort(插入排序)
#include<stdio.h> #include<time.h> int insertion_sort() { ; int a[max],i,j; srand((unsig ...
- 经典排序算法 – 插入排序Insertion sort
经典排序算法 – 插入排序Insertion sort 插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕. 插入排序方法分直接插入排序和折半插入排序两种, ...
- 【排序基础】5、插入排序法 - Insertion Sort
插入排序法 - Insertion Sort 文章目录 插入排序法 - Insertion Sort 插入排序设计思想 插入排序代码实现 操作:插入排序与选择排序的比较 简单记录-bobo老师的玩转算 ...
- leetcode 147. Insertion Sort List ----- java
Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...
- [LeetCode] Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...
- [算法] 插入排序 Insertion Sort
插入排序(Insertion Sort)是一种简单直观的排序算法.它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入.插入排序在实现上,通常采用in-pla ...
随机推荐
- Spring Boot2 系列教程 (十六) | 整合 WebSocket 实现广播
前言 如题,今天介绍的是 SpringBoot 整合 WebSocket 实现广播消息. 什么是 WebSocket ? WebSocket 为浏览器和服务器提供了双工异步通信的功能,即浏览器可以向服 ...
- Openstack之七:实现基于桥接的内外网络
一.在控制端进行配置网络 #启动实例文档:https://docs.openstack.org/ocata/zh_CN/install-guide-rdo/launch-instance.html# ...
- Friday the Thirteenth 黑色星期五 USACO 模拟 超级简单做法
1003: 1.1.3 Friday the Thirteenth 黑色星期五 时间限制: 1 Sec 内存限制: 128 MB提交: 8 解决: 8[提交] [状态] [讨论版] [命题人:外部 ...
- Java.前端模板.Thymleaf
1. Input 日期格式化 <input id="renewalDate" name="renewalDate" th:value="${#d ...
- 如何在匿名thread子类中保证线程安全
在做性能测试的过程中,我写了两个虚拟类ThreadLimitTimeCount和ThreadLimitTimesCount做框架,通过对线程的标记来完成超时请求的记录.旧方法如下: @Override ...
- 基于JS实现归并排序算法
/*********************************************JS归并排序************************************************ ...
- linux操作系统运行学习总结
https://www.cnblogs.com/f-ck-need-u/p/10481466.html 操作系统学习总结 1.linux上面cpu通过上下文切换达到进程的不断切换,通过动态计算切换执行 ...
- C#的WinForm窗体美化
为了帮助用户追求美观,.NET 4.0 专门为对此有需求的人提供了IrisSkin4.dll皮肤引用集,里面封装了许多对窗体重新描绘的方法,再搭配上WinForm特有的 .ssk 文件,就可以实现窗体 ...
- 从maven安装配置到idea成功创建maven项目
在文章开始之前,我还是想安抚下你躁动的心情,说实话这一套操作下来的确花了我不少时间,的确头疼. 不过对于现在在看文章的你,我还是想提倡多多尝试,耐心哈,别砸键盘......废话少说切入正题 一. ma ...
- swoole(PHP异步网络通信引擎)的结构和运行流程
swoole结构说明和运行流程 主要分为三个部分: 1.Master:swoole的主进程 处理swoole核心的事件驱动, 它包含多个线程(蓝色Reactor), 所有事件的监听都在Reactor实 ...