第三周作业——冒泡排序和归并排序
来源:互联网 发布:淘宝客推广效果如何 编辑:程序博客网 时间:2024/06/05 14:29
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++]; } } }
0 0
- 第三周作业——冒泡排序和归并排序
- 第三周作业——冒泡排序和归并排序
- 第三周作业——冒泡排序和归并排序
- 第三周作业——冒泡排序和归并排序
- 第三周作业——冒泡排序和归并排序
- 第三周作业——冒泡排序和归并排序
- 第三周作业——冒泡排序和归并排序
- 第三周作业——冒泡排序和归并排序
- 第三周作业——冒泡排序和归并排序
- 第三周作业——冒泡排序和归并排序
- 第三周作业——冒泡排序和归并排序
- 第三周作业——冒泡排序和归并排序
- 第三周作业——冒泡排序和归并排序
- 第三周作业——冒泡排序和归并排序
- 第三周作业——冒泡排序和归并排序
- 第三周作业——冒泡排序和归并排序
- 第三周作业——冒泡排序和归并排序
- 第三周作业——冒泡排序和归并排序
- WinCE 下结构体占用空间的分析
- 大型网站架构演化
- MySQL 模拟条件索引
- javaweb问题求解
- oc第一天课程代码(NSString常用函数及初始化方式)
- 第三周作业——冒泡排序和归并排序
- Button的使用(七):RadioGroup、RadioButton
- 第四周作业--图的表示
- Java读取property配置文件的方法
- c++ 命名空间
- 获取 WinCE 移动设备屏幕旋转方向
- 第五周项目1-5参数初始化表
- boost中tokenizer的用法举例
- WinCE 下进程可访问的代码页的地址获取