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
{
temp = a[i]
for(j=i-1
{
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秒
************************************************************************/
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
{
temp = a[i]
for(j=i-1
{
a[j+1] =a[j]
j--
}
a[j+1]=temp
}
}
void printArray(int* a, int n)
{
int i
for(i=0
{
printf("%d\t",a[i])
}
printf("\n")
}