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;
        }
    }

    单元测试的就略过了吧。

  • 相关阅读:
    html5_css 3 学习指南__转
    MySQL常见故障处理手册_转
    MYSQL出错代码列表——转
    Redhat 6环境下安装Oracle 12c的方法
    Wireshark入门:分析DHCP协议的运行
    重命名Oracle数据库的表空间(Renaming a Tablespace)
    Oracle DB 分区特性概述 Overview of Partitions
    Oracle Database Concepts:介绍模式对象(Introduction to Schema Objects)
    Supporting Connected Routes to Subnet Zero
    Secondary IP Addressing
  • 原文地址:https://www.cnblogs.com/davidwang456/p/3474750.html
Copyright © 2011-2022 走看看