插入排序使用的是增量(incremental)方法;在排好子数组A[1..j-1]后,将A[j]插入,形成排好序的子数组A[1..j]。
更多信息请参考:http://baike.baidu.com/view/396887.htm
C语言代码:
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <time.h>
void init(int *array, int count)
{
int n = 0;
srand((unsigned int)time(NULL));
for (n=0; n<count; n++)
{
array[n] = rand()%100 + 1;
}
}
void output(int *array, int count)
{
int n = 0;
for (n=0; n<count; n++)
{
printf("%5d", array[n]);
}
printf("\n");
}
void insertsort(int* array, int count, int type)
{
int i = 0;
int j = 0;
int temp = 0;;
if (type == 0)
{
//正排序,从小排到大
//比较的轮数
for (i = 1; i<count; i++)
{
//保证前i个数排好序
temp=array[i];
j = i-1;
while(j>=0&&array[j]>temp)
{
array[j+1]=array[j];
j--;
}
array[j+1]=temp;
}
}
else
{
//倒排序,从大排到小
//比较的轮数
for (i = 1; i<count; i++)
{
//保证前i个数排好序
temp=array[i];
j = i-1;
while(j>=0&&array[j]<temp)
{
array[j+1]=array[j];
j--;
}
array[j+1]=temp;
}
}
}
int main()
{
const int count = 10;
int array[count];
memset(array, 0, sizeof(int)*count);
init(array, count);
printf("data before sort: ");
output(array, count);
insertsort(array, count, 0); // 正排序,从小排到大
printf("data after sort(asc): ");
output(array, count);
insertsort(array, count, 1); // 倒排序,从大排到小
printf("data after sort(desc): ");
output(array, count);
return 0;
}
运行结果如下:
data before sort: 71 68 58 93 32 80 100 73 10 18
data after sort(asc): 10 18 32 58 68 71 73 80 93 100
data after sort(desc): 100 93 80 73 71 68 58 32 18 10
Java代码:
import java.util.Random;
/**
* 插入排序
* 方法:将一个记录插入到已排好序的有序表(有可能是空表)中,从而得到一个新的记录数增1的有序表。
* 性能:比较次数O(n^2),n^2/4
* 比较次数是前两者的一般,而复制所需的CPU时间较交换少,所以性能上比冒泡排序提高一倍多,而比选择排序也要快。
*/
public class Sort
{
/*
* 输出数组中的数据
*/
public static void OutputArray(int[] array)
{
for (int data : array)
{
System.out.print(data + "\t");
}
System.out.println();
}
/*
* 生成需要排序的数组
*/
public static int[] createArray(int count)
{
int array[] = new int[count];
Random r = new Random();
for (int i = 0; i < count; i++)
{
array[i] = r.nextInt(100);
}
System.out.println("");
System.out.print("data before sort:\t");
OutputArray(array);
return array;
}
/**
* 插入排序
*/
public static void insertSort(int[] array, String sortType)
{
int insertValue;
if (sortType.equals("asc"))
{ //正排序,从小排到大
//比较的轮数
for (int i = 1; i <array.length; i++)
{
//保证前i个数排好序
insertValue=array[i];
int index=i-1;
while(index>=0&&array[index]>insertValue)
{
array[index+1]=array[index];
index--;
}
array[index+1]=insertValue;
}
}
else if (sortType.equals("desc"))
{ //倒排序,从大排到小
//比较的轮数
for (int i = 1; i <array.length; i++)
{
//保证前i个数排好序
insertValue=array[i];
int index=i-1;
while(index>=0&&array[index]<insertValue)
{
array[index+1]=array[index];
index--;
}
array[index+1]=insertValue;
}
}
else
{
System.out.println("您输入的排序类型错误!");
}
}
public static void main(String[] args)
{
int[] arr1=createArray(10);
System.out.print("data after sort(asc):\t");
insertSort(arr1, "asc");
OutputArray(arr1);
System.out.print("data after sort(desc):\t");
insertSort(arr1, "desc");
OutputArray(arr1);
}
}
运行结果如下:
data before sort: 33 99 96 91 85 97 85 82 58 86
data after sort(asc): 33 58 82 85 85 86 91 96 97 99
data after sort(desc): 99 97 96 91 86 85 85 82 58 33