Insertion sort
来源:互联网 发布:设置手机繁体软件 编辑:程序博客网 时间:2024/06/11 06:47
插入排序的最大复杂度为O(n^2);
代码:
#include <cstdio>#include <algorithm>#include <iostream>using namespace std;void Output(int A[],int n){for(int i=0;i<n;i++)printf("%d%c",A[i],i==n-1?'\n':' '); }void Sort(int A[],int n){int j,i,v;for(int i=1;i<n;i++){v = A[i];j = i-1; //A[i]后面的值,如图A[3]while( j>=0 && A[j]>v ) //当小于等于v的时候停止,如图A[0] { A[j+1] = A[j]; j--; //从i-1向前自减}A[j+1] = v; //在此之前,通过j--,了如图j==0的时候,所以A[i+1]=vOutput(A,n);}}int main(){int n;int A[105];scanf("%d",&n);for(int i=0;i<n;i++)scanf("%d",&A[i]); for(int i=0;i<n;i++) printf("%d%c",A[i],i==n-1?'\n':' '); Sort(A,n);return 0;} /*输入: 65 2 4 6 1 3输出:5 2 4 6 1 32 5 4 6 1 32 4 5 6 1 32 4 5 6 1 31 2 4 5 6 31 2 3 4 5 6*/
阅读全文
0 0
- Insertion Sort
- Insertion Sort
- insertion sort
- insertion sort
- Insertion Sort
- insertion sort
- insertion sort
- insertion sort
- insertion sort
- Insertion Sort
- Insertion Sort
- Insertion-sort
- Insertion sort
- Insertion sort
- insertion sort
- Insertion sort
- Insertion sort
- Insertion Sort
- 安全预警:Xshell 5.0 Build 1322官方版本被植入后门,请尽快更新至最新版本
- 一步一步制作yaffs/yaffs2根文件系统(八)---总结及怎么样使系统自动启动自己编写的程序
- Spring MVC之@RequestBody, @ResponseBody 详解
- EtherCAT protrocol
- 关于String ,StringBuffer,StringBuilder的比较
- Insertion sort
- gulp学习博客
- 深入浅出数据结构C语言版(10)——树的简介
- 不安装Oracle客户端使用PLSQL连接数据库
- 第二章--java内存区域与内存溢出
- JNI GetFieldID和GetMethodID函数解释及方法签名
- JSP页面从后台类取值
- tornado url 调整
- Android记录手机屏幕操作并进行回放(需要root权限)