您的位置:首页 > 其它

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

2014-03-27 22:01 232 查看
编程实现冒泡排序(方法名:bubbleSort) 与 归并排序(mergeSort),把排序后的结果分别保存到largeW_bubble.txt 和 largeW_merge.txt 中,把两种排序结果的运行时间输出到屏幕中(发博文时把运行时间的截图作为运行结果提交)。


1,冒泡排序:

package index;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class index {
public static void main(String args[]){
System.out.println( "Hello World! \n" );
String filepath="E:/迅雷下载/largeW.txt";
readTxtFile(filepath);
}

public static void readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
long t1=System.currentTimeMillis();
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
int i=0;
double mun=0;
double[] b0=new double[150000];
double[] b1=new double[150000];
double[] b2=new double[150000];
double[] b3=new double[150000];
double[] b4=new double[150000];
double[] b5=new double[150000];
double[] b6=new double[150000];
double[] b7=new double[150000];
double[] b8=new double[150000];
double[] b9=new double[150000];
int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,j=0,k=0;
while((lineTxt = bufferedReader.readLine()) != null){
mun=Double.parseDouble(lineTxt);
i=(int)mun/100000;
switch(i){
case 0:
b0[a]=mun;
a++;
break;
case 1:
b1=mun;
b++;
break;
case 2:
b2[c]=mun;
c++;
break;
case 3:
b3[d]=mun;
d++;
break;
case 4:
b4[e]=mun;
e++;
break;
case 5:
b5[f]=mun;
f++;
break;
case 6:
b6[g]=mun;
g++;
break;
case 7:
b7[h]=mun;
h++;
break;
case 8:
b8[j]=mun;
j++;
break;
case 9:
b9[k]=mun;
k++;
break;
}
}
Bubble bb0=new Bubble(a,b0);
Bubble bb1=new Bubble(b,b1);
Bubble bb2=new Bubble(c,b2);
Bubble bb3=new Bubble(d,b3);
Bubble bb4=new Bubble(e,b4);
Bubble bb5=new Bubble(f,b5);
Bubble bb6=new Bubble(g,b6);
Bubble bb7=new Bubble(h,b7);
Bubble bb8=new Bubble(j,b8);
Bubble bb9=new Bubble(k,b9);
b0=bb0.bubbleSort();
b1=bb1.bubbleSort();
b2=bb2.bubbleSort();
b3=bb3.bubbleSort();
b4=bb4.bubbleSort();
b5=bb5.bubbleSort();
b6=bb6.bubbleSort();
b7=bb7.bubbleSort();
b8=bb8.bubbleSort();
b9=bb9.bubbleSort();
long t2=System.currentTimeMillis();
System.out.println("运行该程序所耗时间:");
System.out.println(t2-t1);
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}

}

}


package index;

import java.util.ArrayList;
import java.util.List;

public class Bubble {
private int len;
private double[] mun;

Bubble(int len,double[] mun){
this.len=len;
this.mun=mun;
}

public double[] bubbleSort(){
double temp=0;
for(int i=mun.length-1;i>0;--i){
for(int j=0;j<i;++j){
if(mun[j]>mun[j+1]){
temp=mun[j];
mun[j]=mun[j+1];
mun[j+1]=temp;
}
}
}
return mun;
}
}



[b]本个冒泡排序算法用时512230毫秒,约为9分钟。


2,归并排序:

public static void readFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
File merge_file=new File("E:/迅雷下载/ largeW_merge.txt ");
FileWriter out=new FileWriter(merge_file);
if(file.isFile() && file.exists()){ //判断文件是否存在
long t1=System.currentTimeMillis();
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
double mun=0;
int i=0;
int t=0;
double[] mer=new double[1000000];
double[] mer1=new double[1000000];
while((lineTxt = bufferedReader.readLine()) != null){
mun=Double.parseDouble(lineTxt);
mer[i++]=mun;
}
--i;
MergeSort1(mer,mer1,i);
for(int j=0;j<=i;j++){
lineTxt=String.valueOf(mer1[j]);
out.write(lineTxt);
out.write("\r\n");
}
out.close();
System.out.println("all munber show in here:");
System.out.println(i);
long t2=System.currentTimeMillis();
System.out.println("运行该程序所耗时间:");
System.out.println(t2-t1);
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}

/*
* 一次归并排序
*/
public static void Merge(double r[],double r1[],int s,int m,int t){
int i=s;
int j=m+1;
int k=s;
while(i<=m&&j<=t){
if(r[i]<=r[j])
r1[k++]=r[i++];
else
r1[k++]=r[j++];
}
if(i<=m)
while(i<=m)
r1[k++]=r[i++];
else
while(j<=t)
r1[k++]=r[j++];
}

/*
* 归并排序
*/
public static void MergePass(double r[],double r1[],int n,int h){
int i=1;
while(i<=n-2*h+1){
Merge(r,r1,i,i+h-1,i+2*h-1);
i+=2*h;
}
if(i<n-h+1)
Merge(r,r1,i,i+h-1,n);
else
for(int k=i;k<=n;k++)
r1[k]=r[k];
}

/*
* 归并排序
*/
public static void MergeSort1(double r[],double r1[],int n){
int h=1;
while(h<n)
{
MergePass(r,r1,n,h);
h=2*h;
MergePass(r1,r,n,h);
h=2*h;
}
}




[b]本个归并排序算法用时1025毫秒,约为1秒。

[/b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: