您的位置:首页 > 编程语言

算法第三次作业(1. 排序。对文件 largeW.txt(下载链接)中的数据,编程实现冒泡排序(方法名:bubbleSort) 与 归并排序(mergeSort),把排序后的结果分别保存到largeW

u013995202 2014-03-27 12:28 483 查看
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.Date;
import java.util.List;

public class PAIXU {

/**
*  冒泡排序和归并排序
*/
static String srcPath="E:\\Java项目\\third\\src\\third\\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(srcPath);
FileInputStream fis=new FileInputStream(file);
InputStreamReader reader=new InputStreamReader(fis, "gb2312");
BufferedReader bufferedreader=new BufferedReader(reader);
String lineData="";
int num=0;
List<Integer> list=new ArrayList<Integer>();
while((lineData=bufferedreader.readLine())!=null){
System.out.println(lineData.trim());
//bubbleSort(lineData);
num=num+1;
list.add(Integer.parseInt(lineData.trim()));
}
Integer[] lineDataArray=(Integer[])list.toArray(new Integer[num]);
//开始冒泡排序
long timeStart=System.currentTimeMillis();//开始冒泡排序计时
bubbleSort(lineDataArray);
long timeEnd=System.currentTimeMillis();//冒泡排序计时结束
System.out.println("冒泡排序所花费的时间是:"+(timeEnd-timeStart)/1000+"秒");
//把冒泡排序的结果写到文本文件
ResultSortWriteTxt(lineDataArray,0);

//开始归并排序
lineDataArray=(Integer[])list.toArray(new Integer[num]);//为了防止冒泡排序之后的数组对归并排序产生影响,所以重新赋值
Integer[] num1=new Integer[lineDataArray.length];
timeStart=System.currentTimeMillis();//开始归并排序计时
Integer[] result  = mergeSort(lineDataArray, 0, lineDataArray.length - 1, num1);
timeEnd=System.currentTimeMillis();//归并排序计时结束
System.out.println("归并排序所花费的时间是:"+(timeEnd-timeStart)/1000+"秒");
//把归并排序的结果写到文本文件
ResultSortWriteTxt(lineDataArray,1);
System.out.println("文本中总共有:"+num+"条数据....");
} catch (Exception e) {
e.printStackTrace();
}

}

/**
* 冒泡排序
* @param lineDataArray
* @throws IOException
*/
public static void bubbleSort(Integer[] Array) throws IOException{
Integer[] lineDataArray=(Integer[]) Array;
int temp=0;
for(int i=0;i<lineDataArray.length;i++){
for(int j=i+1;j<lineDataArray.length;j++){
if(lineDataArray[i]>lineDataArray[j]){
temp=lineDataArray[i];
lineDataArray[i]=lineDataArray[j];
lineDataArray[j]=temp;
}
}
System.out.println(lineDataArray[i]);
}
}
/**
* 把排序的结果写到文本文件
* @param lineDataArray
* @throws IOException
*/
public static void  ResultSortWriteTxt(Integer[] lineDataArray,int whichSort) throws IOException{
//循环输出冒泡排序后的数据到文本文件
System.out.println("创建");
File file=null;
if(whichSort==0){//冒泡排序是表示0
file =new File(bubbleSortDesPath);
}else if(whichSort==1){//归并排序是表示1
file =new File(mergeSortDesPath);
}
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fos=new FileOutputStream(file);
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(fos,"gb2312"));
for(int i=0;i<lineDataArray.length;i++){
writer.write(lineDataArray[i].toString()+"\r\n");
}
}

/**
* 归并排序
* @param Array
*/
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);// 由num2去归并,返回的值放到num1中,num1赋新值,其实就是更新num2,然后让num2再去归并,返回新的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++];
}
}

}

                                            
标签: 
相关文章推荐