第三周作业——冒泡排序和归并排序

来源:互联网 发布:网络推广招聘东莞 编辑:程序博客网 时间:2024/05/18 03:48
package d;    import java.io.File;  import java.io.FileNotFoundException;  import java.io.PrintWriter;  import java.util.Scanner;    public class sort {      public static void main(String[] args) {          readFile    rf = new readFile();          bubbleSort  bs = new bubbleSort();          writFile    wf = new writFile();          mergeSort   ms = new mergeSort();            double starttime1 = System.currentTimeMillis();        wf.writFile(bs.bubbleSort(rf.readFile()),"largeW_bubble.txt");        double endtime1 = System.currentTimeMillis();         System.out.println("bubble sort time: " + (endtime1-starttime1) + "ms");                   double starttime2 = System.currentTimeMillis();         wf.writFile(ms.mergeSort(rf.readFile(), 0, rf.readFile().length-1),"largeW_merge.txt");          double endtime2 = System.currentTimeMillis();          System.out.println("merge sort time: " + (endtime2-starttime2) + "ms");     }  }    /////////////////////////////////////////////////  class writFile {      public void writFile(int[] wArray,String srcfile){          int num = 0;          PrintWriter pw;          try {              pw = new PrintWriter(new File(srcfile));              while(wArray.length>num){                  pw.printf("%d: %d\n", num+1,wArray[num]);                 num++;              }              pw.close();          } catch (FileNotFoundException e) {              e.printStackTrace();          }                    System.out.println("运行结束,一共有"+num+"个数据。");      }  }  /////////////////////////////////////////////////  class readFile{      public int[] readFile(){          int tempArray[] = new int[1000000],i=0;          Scanner myfile;          try {              myfile = new Scanner(new File("largeW.txt"));              while(myfile.hasNext()){                     tempArray[i]=myfile.nextInt();                      i++;              }               myfile.close();          } catch (FileNotFoundException e) {              e.printStackTrace();          }          return tempArray;      }  }  ///////////////////////////////////////////////  class bubbleSort{      public int[] bubbleSort(int[] bubbleArray){          int temp;          //double start = System.currentTimeMillis();          for(int i=bubbleArray.length-1;i>0;i--){            for(int j=0;j<i;j++){                  if(bubbleArray[j]>bubbleArray[j+1]){                      temp=bubbleArray[j];                      bubbleArray[j]=bubbleArray[j+1];                      bubbleArray[j+1]=temp;                  }              }          }          //double end = System.currentTimeMillis();        //System.out.println("time: " + (end-start) + "ms");          return bubbleArray;      }  }  //////////////////////////////////////////////////////////////  class  mergeSort{      public int[] mergeSort(int[] arrays,int start,int end){          if(start<end){              int m=(start+end)/2;              mergeSort(arrays,start,m);              mergeSort(arrays,m+1,end);              return combin_arrays(arrays,start,m,end);            }          return new int[0];      }            public int[] combin_arrays(int[] arrays,int start,int m,int end){          int length=end-start+1;          int temp[]=new int[length];          int i=start;          int j=m+1;          int c=0;          while(i<=m &&j<=end){              if(arrays[i]<arrays[j]){                  temp[c++]=arrays[i++];              }else{                  temp[c++]=arrays[j++];              }          }          while(i<=m){              temp[c++]=arrays[i++];          }          while(j<=end){              temp[c++]=arrays[j++];          }          c=0;          for(int t=start;t<=end;t++,c++){              arrays[t]=temp[c];          }          return arrays;      }  } 


0 0
原创粉丝点击