首页 > 学院 > 开发设计 > 正文

插入排序

2019-11-08 01:20:39
字体:
来源:转载
供稿:网友

我理解的插入排序的基本思想是:对于新插入的数,通过寻找其在有序序列中的合适位置 达到排序的目的。下面是实现的代码

#include <iostream>using namespace std;void insert_sort(int a[],int n){int j;for (int i=0;i<n;i++){int temp = a[i];      //用于存放要插入的数for(j=i;j>0 && a[j-1]>temp;j--)  //将要插入的数和前面的有序序列比较,寻找合适的插入位置{a[j]=a[j-1];}a[j]=temp;}}void main(){int a[] = {4,6,12,9,3,66,3,5,9,98};insert_sort(a,10);for (int i = 0;i<10;i++){cout<<a[i]<<"->";}system("Pause");}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表