第三周作业——冒泡排序和归并排序
2014-04-02 19:17
471 查看
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.List; public class ArrangeMent{ static String Path="D:/largeW.txt"; static String bubbleSortDesPath="src/largeW_bubble.txt"; static String mergeSortDesPath="src/largeW_merge.txt"; public static void main(String[] args) { try { File file=new File(Path); FileInputStream fis=new FileInputStream(file); InputStreamReader reader=new InputStreamReader(fis, "gb2312"); BufferedReader bufferedreader=new BufferedReader(reader); String nums=""; int num=0; List<Integer> list=new ArrayList<Integer>(); while((nums=bufferedreader.readLine())!=null){ num=num+1; list.add(Integer.parseInt(nums.trim())); } Integer[] NumsArray=(Integer[])list.toArray(new Integer[num]); long timeStart=System.currentTimeMillis(); bubbleSort(NumsArray); long timeEnd=System.currentTimeMillis(); System.out.println("冒泡排序所花费的时间是:"+(timeEnd-timeStart)/1000+"秒"); ResultSortWriteTxt(NumsArray,0); NumsArray=(Integer[])list.toArray(new Integer[num]); Integer[] num1=new Integer[NumsArray.length]; timeStart=System.currentTimeMillis(); Integer[] result = mergeSort(NumsArray,0, NumsArray.length - 1,num1); timeEnd=System.currentTimeMillis(); System.out.println("归并排序所花费的时间是:"+(timeEnd-timeStart)/1000+"秒"); ResultSortWriteTxt(result,1); System.out.println("文本中总共有:"+num+"条数据!"); } catch (Exception e) { e.printStackTrace(); } } public static void bubbleSort(Integer[] Array)throws IOException{ Integer[] NumsArray=(Integer[]) Array; int temp=0; for(int i=0;i<NumsArray.length;i++){ for(int j=i+1;j<NumsArray.length;j++){ if(NumsArray[i]>NumsArray[j]){ temp=NumsArray[i]; NumsArray[i]=NumsArray[j]; NumsArray[j]=temp; } } } } public static void ResultSortWriteTxt(Integer[] NumsArray,int whichSort) throws IOException{ File file=null; if(whichSort==0){ file =new File(bubbleSortDesPath); }else if(whichSort==1){ file =new File(mergeSortDesPath); } if(!file.exists()){ file.createNewFile(); } FileOutputStream fos=new FileOutputStream(file); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(fos,"gb2312")); for(int i=0;i<NumsArray.length;i++){ //System.out.println(NumsArray[i]); bw.write(NumsArray[i].toString()+"\r\n"); bw.flush(); } fos.close(); bw.close(); } private static Integer[] mergeSort(Integer[] num, int s, int t, Integer[] num1) { int temp; Integer[] num2 = new Integer[t + 1]; if (s == t){ num1[s] = num[s]; }else { temp = (s + t) / 2; mergeSort(num, s, temp, num2); mergeSort(num, temp + 1, t, num2); merg(num2, s, temp, t, num1); } return num1; } private static void merg(Integer[] num2, int l, int m, int n, Integer[] num1) { int i, j, k; i = l; j = m + 1; k = l; while (i <= m && j <= n) { if (num2[i] < num2[j]){ num1[k++] = num2[i++]; }else{ num1[k++] = num2[j++]; } } while (i <= m) { num1[k++] = num2[i++]; } while (j <= n) { num1[k++] = num2[j++]; } } }
相关文章推荐
- 环形队中实现队列的基本运算
- 环形队中实现队列的基本运算
- MySQL 模拟条件索引
- WinCE 下结构体占用空间的分析
- 【翻译】五步快速使用LINQPad尝鲜StreamInsight
- 手机验证码倒计时
- Java读取property配置文件的方法
- 解决linux的-bash: ./test: Permission denied
- Button的使用(七):RadioGroup、RadioButton
- FAFU-1398 面积 矩形面积并 线段树+扫描线
- STM32片外内存的使用
- 一个男人关心的东西 决定了他的层次
- javaweb问题求解
- oc第一天课程代码(NSString常用函数及初始化方式)
- How to send sms by .net
- 大型网站架构演化
- 提取投影直方图的多个主分量
- 2-sat入门(tarjan)hdu(3062)
- Could not find the main class: org.apache.catalina.startup.Boostrap. Program will exit
- 第五周项目1-4使用有默认参数的构造函数