直接插入排序
2016-04-27 20:42
281 查看
java版本
/************************************************************************* > File Name: DirectInsertSort.java > Author: lxlm > Created Time: 2016年04月27日 星期三 20时21分35秒 ************************************************************************/ public class DirectInsertSort { public static void main(String[] args) { int[] a={6,1,2,7,9,3,4,5,10,8}; directInsertSort(a); printArray(a); } public static void directInsertSort(int[] a) { int temp = 0; int n = a.length; int j; if(n==1 || n==0) { return; } for(int i=1;i<n;i++) { temp = a[i]; for(j=i-1;j>=0 && temp<a[j];)//注意:必须将j>=0放在temp<a[j]前面 { a[j+1] = a[j]; --j; } a[j+1] = temp; } } public static void printArray(int[] a) { for(int item:a) { System.out.printf("%d\t",item); } System.out.println(); } }
C版本
/************************************************************************* > File Name: directInsertSort.c > Author: lxm > Created Time: 2016年04月27日 星期三 20时10分14秒 ************************************************************************/ #include<stdio.h> #define N 10 void directInsertSort(int* a, int n); void printArray(int* a, int n); int main(void) { int a[] = {6,1,2,7,9,3,4,5,10,8}; directInsertSort(a,N); printArray(a,N); return 0; } void directInsertSort(int* a, int n) { int i,j; int temp; for(i=1;i<n;i++) { temp = a[i] ; for(j=i-1;j>=0 && temp<a[j];) { a[j+1] =a[j]; j--; } a[j+1]=temp ; } } void printArray(int* a, int n) { int i; for(i=0;i<n;i++) { printf("%d\t",a[i]); } printf("\n"); }
相关文章推荐
- C++用new来创建对象和非new来创建对象的区别
- 数据结构 - 线性表的顺序实现
- servlet--百度百科
- 漫画城之搜索
- Exception
- FileChannel
- python格式化dict输出
- 1015. Reversible Primes (20)
- Best Coder Matrix(数学)
- Houdini中全景摄像机shader立体左右眼成像方法
- 主成分分析(Principal Component Analysis)
- 最短路问题(各种方法整理)
- Nginx--后端节点健康检查
- zabbix3.0 从坚持到用起 开始配置管理界面
- mysql中变量character_set_connection的具体作用
- Retrofit——Cookie
- 容斥定理
- 动易CMS2006安装与配置
- HDU 2401 Baskets of Gold Coins(数学题)
- 汇编调用C程序