zoukankan      html  css  js  c++  java
  • java实现插入排序算法 附单元测试源码

    插入排序算法

    public class InsertSortTest {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            int[] a={3,5,9,16,29,47,78,89,94};
            int key=47;
            int[] b=insertSort(a,key);
            for(int i=0;i<b.length;i++){
                System.out.print(b[i]+"  ");    
            }
        }
        
        public static int[] insertSort(int[] unsorted,int key){
            int len=unsorted.length;
            int[] sorted=new int[len+1];
            for(int i=0;i<len;i++){
                //when key exists in edge of array
                if(key<unsorted[0]){
                    sorted[0]=key;
                    System.arraycopy(unsorted, 0, sorted, 1, len);
                }
                if(key>unsorted[len-1]){
                    sorted[sorted.length-1]=key;
                    System.arraycopy(unsorted, 0, sorted, 0, len);
                }
                //when key exists in mid of array
                if(key>unsorted[i]){
                    sorted[i]=unsorted[i];
                }else{
                    sorted[i]=key;
                    System.arraycopy(unsorted, i, sorted, i+1, len-i);
                    break;
                }
            }
            return sorted;
        }
    }

    单元测试源码:(使用junit4)

    public class InsertSortUnitTest {
    
        @Test
        public void testLT() {
            int[] a={3,5,9,16,29,47,78,89,94};
            int key=-5;
            int[] result={-5,3,5,9,16,29,47,78,89,94};
            int[] b=InsertSortTest.insertSort(a,key);
            for(int i=0;i<result.length;i++){
                assertEquals(result[i],b[i]);
            }
        }
        
        @Test
        public void testGT() {
            int[] a={3,5,9,16,29,47,78,89,94};
            int key=205;
            int[] result={3,5,9,16,29,47,78,89,94,205};
            int[] b=InsertSortTest.insertSort(a,key);
            for(int i=0;i<result.length;i++){
                assertEquals(result[i],b[i]);
            }
        }
        
        @Test
        public void testMid() {
            int[] a={3,5,9,16,29,47,78,89,94};
            int key=50;
            int[] result={3,5,9,16,29,47,50,78,89,94};
            int[] b=InsertSortTest.insertSort(a,key);
            for(int i=0;i<result.length;i++){
                assertEquals(result[i],b[i]);
            }
        }
        
        @Test
        public void testEqual(){
            int[] a={3,5,9,16,29,47,78,89,94};
            int key=47;
            int[] result={3,5,9,16,29,47,47,78,89,94};
            int[] b=InsertSortTest.insertSort(a,key);
            for(int i=0;i<result.length;i++){
                assertEquals(result[i],b[i]);
            }
        }
    }

     程序进一步优化:

    public class InsertSortTest {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            Comparable[] a={3f,5f,9f,16f,29f,47f,78f,89f,94f};
            Comparable key=47.78f;
            Comparable[] b=insertSort(a,key);
            for(int i=0;i<b.length;i++){
                System.out.print(b[i]+"  ");    
            }
        }
        
        public static Comparable[] insertSort(Comparable[] unsorted,Comparable key){
            int len=unsorted.length;
            Comparable[] sorted=new Comparable[len+1];
            for(int i=0;i<len;i++){
                //when key exists in edge of array
                if(key.compareTo(unsorted[0])<0){
                    sorted[0]=key;
                    System.arraycopy(unsorted, 0, sorted, 1, len);
                }
                if(key.compareTo(unsorted[len-1])>0){
                    sorted[sorted.length-1]=key;
                    System.arraycopy(unsorted, 0, sorted, 0, len);
                }
                //when key exists in mid of array
                if(key.compareTo(unsorted[i])>0){
                    sorted[i]=unsorted[i];
                }else{
                    sorted[i]=key;
                    System.arraycopy(unsorted, i, sorted, i+1, len-i);
                    break;
                }
            }
            return sorted;
        }
    }

    单元测试的就略过了吧。

  • 相关阅读:
    select应用于read函数 超时非阻塞方式
    取文本索引所执向的值(简单)
    linux c函数指针的应用
    解决vsftp无法启动问题(转)
    Could not chdir to home directory /home/USER: Permission denied
    sscanf和正则表达式
    存储过程重置SEQUENCE值从新开始。
    Signal ()函数详细介绍 Linux函数(转)
    linux 环境NTP配置与开机自启动(转)
    linux下iconv()函数的用法(转载并修改)
  • 原文地址:https://www.cnblogs.com/davidwang456/p/3474750.html
Copyright © 2011-2022 走看看