赞
踩
插入排序:也称直接插入排序。基本思路:每一步将一个数插入一个已经排好的序列中,并使之保持有序。直到插完所有的数为止。
- //插入排序(从小到大)
- #include<stdio.h>
- #include<stdlib.h>
- int number[100000000]; //定义数组
- void insertion_sort(int *number,int n) //定义一个插入函数"insertion_sort"
- {
- int i,t,temp;
- for(i=1;i<n;i++) //外层循环遍历 (需要插入n个数)
- {
- temp=number[i]; //取未排序列的元素,有n个,从第一个开始取
- for(t=i;t>0&&number[t-1]>temp;t--);
- {
- number[t]=number[t-1];//依次比较并右移
- number[t]=temp;//放进合适位置
- }
- }
- }
- int main()
- {
- int i=0,n,j=0;
- printf("输入数字个数:\n");
- scanf("%d",&n); //输入要排序的数字的个数
- printf("输入%d个数:\n",n);
- for(j=0;j<n;j++) //将所有数全放入number数组中
- scanf("%d",&number[j]) ;
- insertion_sort(number,n); //引用插入函数
- for(i=0;i<n-1;i++) //循环输出
- printf("%d ",number[i]); //格式需要
- printf("%d\n",number[i]);
- system("pause");
- return 0;
- }
-

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。