• 直接插入排序


    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	",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	",a[i]);
        }
        printf("
    ");
    }
    
    
  • 相关阅读:
    Springboot整合activeMq
    linux下安装activeMq
    linux下配置jdk
    thinkphp5 集成 redis
    linux下安装redis
    tp5集成swagger
    lombok
    idea下springboot环境搭建
    Mac环境下maven安装配置
    idea环境下搭建swagger2接口文档
  • 原文地址:https://www.cnblogs.com/yldf/p/6249907.html
Copyright © 2020-2023  润新知