常用排序算法(插入排序)

插入排序使用的是增量(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   



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值