第三周作业

1,冒泡排序:

  1. package index;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.InputStreamReader;  
  7.   
  8. public class index {  
  9.     public static void main(String args[]){   
  10.            System.out.println( "Hello World! \n" );    
  11.            String filepath="E:/迅雷下载/largeW.txt";  
  12.            readTxtFile(filepath);  
  13.        }  
  14.       
  15.     public static void readTxtFile(String filePath){  
  16.         try {  
  17.                 String encoding="GBK";  
  18.                 File file=new File(filePath);  
  19.                 if(file.isFile() && file.exists()){ //判断文件是否存在  
  20.                     long t1=System.currentTimeMillis();  
  21.                     InputStreamReader read = new InputStreamReader(  
  22.                     new FileInputStream(file),encoding);//考虑到编码格式  
  23.                     BufferedReader bufferedReader = new BufferedReader(read);  
  24.                     String lineTxt = null;  
  25.                     int i=0;  
  26.                     double mun=0;  
  27.                     double[] b0=new double[150000];  
  28.                     double[] b1=new double[150000];  
  29.                     double[] b2=new double[150000];  
  30.                     double[] b3=new double[150000];  
  31.                     double[] b4=new double[150000];  
  32.                     double[] b5=new double[150000];  
  33.                     double[] b6=new double[150000];  
  34.                     double[] b7=new double[150000];  
  35.                     double[] b8=new double[150000];  
  36.                     double[] b9=new double[150000];  
  37.                     int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,j=0,k=0;  
  38.                     while((lineTxt = bufferedReader.readLine()) != null){  
  39.                         mun=Double.parseDouble(lineTxt);  
  40.                         i=(int)mun/100000;  
  41.                         switch(i){  
  42.                         case 0:  
  43.                             b0[a]=mun;  
  44.                             a++;  
  45.                             break;  
  46.                         case 1:  
  47.                             b1[b]=mun;  
  48.                             b++;  
  49.                             break;  
  50.                         case 2:  
  51.                             b2[c]=mun;  
  52.                             c++;  
  53.                             break;  
  54.                         case 3:  
  55.                             b3[d]=mun;  
  56.                             d++;  
  57.                             break;  
  58.                         case 4:  
  59.                             b4[e]=mun;  
  60.                             e++;  
  61.                             break;  
  62.                         case 5:  
  63.                             b5[f]=mun;  
  64.                             f++;  
  65.                             break;  
  66.                         case 6:  
  67.                             b6[g]=mun;  
  68.                             g++;  
  69.                             break;  
  70.                         case 7:  
  71.                             b7[h]=mun;  
  72.                             h++;  
  73.                             break;  
  74.                         case 8:  
  75.                             b8[j]=mun;  
  76.                             j++;  
  77.                             break;  
  78.                         case 9:  
  79.                             b9[k]=mun;  
  80.                             k++;  
  81.                             break;  
  82.                             }  
  83.                     }  
  84.                     Bubble bb0=new Bubble(a,b0);  
  85.                     Bubble bb1=new Bubble(b,b1);  
  86.                     Bubble bb2=new Bubble(c,b2);  
  87.                     Bubble bb3=new Bubble(d,b3);  
  88.                     Bubble bb4=new Bubble(e,b4);  
  89.                     Bubble bb5=new Bubble(f,b5);  
  90.                     Bubble bb6=new Bubble(g,b6);  
  91.                     Bubble bb7=new Bubble(h,b7);  
  92.                     Bubble bb8=new Bubble(j,b8);  
  93.                     Bubble bb9=new Bubble(k,b9);  
  94.                     b0=bb0.bubbleSort();  
  95.                     b1=bb1.bubbleSort();  
  96.                     b2=bb2.bubbleSort();  
  97.                     b3=bb3.bubbleSort();  
  98.                     b4=bb4.bubbleSort();  
  99.                     b5=bb5.bubbleSort();  
  100.                     b6=bb6.bubbleSort();  
  101.                     b7=bb7.bubbleSort();  
  102.                     b8=bb8.bubbleSort();  
  103.                     b9=bb9.bubbleSort();  
  104.                     long t2=System.currentTimeMillis();  
  105.                     System.out.println("运行该程序所耗时间:");  
  106.                     System.out.println(t2-t1);                      
  107.                     read.close();  
  108.         }else{  
  109.             System.out.println("找不到指定的文件");  
  110.         }  
  111.         } catch (Exception e) {  
  112.             System.out.println("读取文件内容出错");  
  113.             e.printStackTrace();  
  114.         }  
  115.        
  116.     }  
  117.       
  118. }  

  1. package index;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. public class Bubble {  
  7.     private int len;  
  8.     private double[] mun;  
  9.       
  10.     Bubble(int len,double[] mun){  
  11.         this.len=len;  
  12.         this.mun=mun;  
  13.     }  
  14.       
  15.     public double[] bubbleSort(){  
  16.         double temp=0;  
  17.         for(int i=mun.length-1;i>0;--i){  
  18.             for(int j=0;j<i;++j){  
  19.                 if(mun[j]>mun[j+1]){  
  20.                     temp=mun[j];  
  21.                     mun[j]=mun[j+1];  
  22.                     mun[j+1]=temp;  
  23.                 }  
  24.             }  
  25.         }  
  26.         return mun;  
  27.     }  

运行时间约1小时37分钟

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值