插入排序算法&二路归并排序算法java实现

来源:互联网 发布:韩国网络作家 编辑:程序博客网 时间:2024/05/16 10:11
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package sortalgos;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Collections;import java.util.List;/** * * @author hitachi *///实现插入排序算法class insertSort{           public static void Sort(ArrayList<Double> myArr){        if(myArr.size()==1||myArr==null)            return;        else{            int i,j;            for(j=1;j<myArr.size();j++){                Double temp=myArr.get(j);                i=j-1;                while(temp.compareTo(myArr.get(i))<0){                        myArr.set(i+1, myArr.get(i));                        --i;                        if(i<0)                            break;                }                myArr.set(i+1, temp);            }        }    }}class mergeSort{    public static void merge(ArrayList<Double> sub0, ArrayList<Double> sub1, ArrayList<Double> dest){        int i,j,k;        if(sub0.size()+sub1.size()!=dest.size()){            System.out.println("Size doesn't match");            return;        }        i=0;        j=0;        k=0;        while(true){           if(i<sub0.size()&&j<sub1.size()){  //两边都还有值的情况               if(sub0.get(i).compareTo(sub1.get(j))<0){                  dest.set(k, sub0.get(i));                  i++;                  k++;               }               else{                   dest.set(k, sub1.get(j));                   j++;                   k++;               }           }           else if(i>=sub0.size()&&j<sub1.size()){  //仅单边有值情况1               dest.set(k, sub1.get(j));               j++;               k++;           }           else if(i<sub0.size()&&j>=sub1.size()){  //仅单边有值情况2               dest.set(k, sub0.get(i));               i++;               k++;           }           else               break;        }    }    public static void sort(ArrayList<Double> myArr){        if(myArr.size()==1)            return;        else{            List sub0=myArr.subList(0, myArr.size()/2);            List sub1=myArr.subList(myArr.size()/2, myArr.size());            ArrayList<Double> subAL0=new ArrayList<Double>(sub0);            ArrayList<Double> subAL1=new ArrayList<Double>(sub1);            sort(subAL0);            sort(subAL1);            merge(subAL0,subAL1,myArr);        }    }}class arrayPrinter{    public static void print(ArrayList myArr){        for(Object single : myArr){//            System.out.println("The array by order now is:");            System.out.print(single);            System.out.print("  ");        }            System.out.println("  ");    }}public class SortAlgos {     /**     * @param args the command line arguments     */    private static ArrayList<Double> myArr = new ArrayList<Double>();    public static void main(String[] args) throws IOException {        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));        while(true){        myArr.clear();        System.out.println("Enter some double values one by one,end with a '.' or empty line:");        String readin=input.readLine();        while(!"".equals(readin) &&!".".equals(readin)){            Double tempD=new Double(readin);            myArr.add(tempD);            readin=input.readLine();        }        System.out.println("Your input order is:");        arrayPrinter.print(myArr);        //**************************************************   要用哪种排序算法自己取消注释,并注释掉其他选项     //        insertSort.Sort(myArr);              //插入排序        mergeSort.sort(myArr);                  //二路归并排序//**************************************************                        System.out.println("After sorting, the order is:");        arrayPrinter.print(myArr);        System.out.println("Would you like another round?(Y/N)");        readin=input.readLine();        if(!("Y".equals(readin)||"y".equals(readin)))            break;        }             }    }