您的位置:首页 > 其它

第三周作业——冒泡排序(BubbleSort)与归并排序(MergeSort)

2014-03-27 09:11 525 查看
冒泡排序(BubbleSort)

基本思想:两两比较待排序数据元素的大小,发现两个数据元素的次序相反时即进行交换,直到没有反序的数据元素为止。



程序代码如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class bubbleSort {

public static void main(String[] args) {

double start_all = System.currentTimeMillis();
String path = "src/largeW.txt";
ArrayList<Integer> list = read(path);
int temp;
double start = System.currentTimeMillis();
for (int i = 0; i < list.size(); i++) {
for (int j = i; j < list.size(); j++) {
if (list.get(i) > list.get(j)) {
temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
double end = System.currentTimeMillis();
System.out.println("冒泡排序时间为: " + (end - start)+"毫秒");
// 写入txt文件
write(list);
System.out.println("成功写入");
double end_all = System.currentTimeMillis();
System.out.println("总运行时间为: " + (end_all - start_all)+"毫秒");
}
//创建并写入largeW_bubble.txt文件
public static void write(ArrayList list) {
File f = new File("src/largeW_bubble.txt");
FileOutputStream fou = null;
try {

fou = new FileOutputStream(f, true);

for (int i = 0; i < list.size(); i++) {
String s = list.get(i).toString();
String a = "" + s + "\t\n";
// byte []bytes=new byte[1024];
// 如何把string转换byte数组
fou.write(a.getBytes());

}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
fou.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//读取文件到Arraylist 数组
public static ArrayList read(String path) {
ArrayList<Integer> list = new ArrayList<Integer>();
BufferedReader input = null;
try {
FileReader in = new FileReader(path);
input = new BufferedReader(in);
String ss;
try {
while ((ss = input.readLine()) != null) {
String[] s = ss.split("\r\n");
for (int i = 0; i < s.length; i++) {
list.add(Integer.parseInt(s[i].trim())); // 将String s中的内容添加到动态数组中
}
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
in.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
}

return list;
}

}


归并排序:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class mergeSort {

public static void main(String[] args) {

double start_all = System.currentTimeMillis();
String path = "src/largeW.txt";
ArrayList<Integer> list=read(path);
double start = System.currentTimeMillis();
mergeSort(list);
double end = System.currentTimeMillis();
System.out.println("归并排序时间为: " + (end - start)+"毫秒");
// 写入txt文件
write(list);
System.out.println("写入完成");
double end_all = System.currentTimeMillis();
System.out.println("总运行时间为: " + (end_all - start_all)+"毫秒");
}
//创建并写入largeW_merge.txt文件
public static void write(ArrayList<Integer> list) {
File f = new File("src/txt/largeW_merge.txt");
FileOutputStream fou = null;
try {

fou = new FileOutputStream(f, true);

for (int i = 0; i < list.size(); i++) {
String s = String.valueOf(list.get(i));
String a = "" + s + "\t\n";
// byte []bytes=new byte[1024];

fou.write(a.getBytes());

}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
fou.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

//归并排序
public static void mergeSort(ArrayList<Integer> data) {
sort(data, 0, data.size() - 1);
}

public static void sort(ArrayList<Integer> data, int left, int right) {
if (left >= right)
return;
// 找出中间索引
int center = (left + right) / 2;
// 对左边数组进行递归
sort(data, left, center);
// 对右边数组进行递归
sort(data, center + 1, right);
// 合并
merge(data, left, center, right);

}

public static void merge(ArrayList<Integer> data, int left, int center, int right) {
// 临时数组
int[] tmpArr = new int[data.size()];
// 右数组第一个元素索引
int mid = center + 1;
// third 记录临时数组的索引
int third = left;
// 缓存左数组第一个元素的索引
int tmp = left;
while (left <= center && mid <= right) {
// 从两个数组中取出最小的放入临时数组
if (data.get(left) <= data.get(mid)) {
tmpArr[third++] = data.get(left++);
} else {
tmpArr[third++] = data.get(mid++);
}
}

while (mid <= right) {
tmpArr[third++] = data.get(mid++);
}
while (left <= center) {
tmpArr[third++] = data.get(left++);
}

while (tmp <= right) {
data.set(tmp, tmpArr[tmp++]);
}
}

//读取文件到int数组
public static ArrayList read(String path) {
ArrayList<Integer> list = new ArrayList<Integer>();
BufferedReader input = null;
try {
FileReader in = new FileReader(path);
input = new BufferedReader(in);
String ss;
try {
while ((ss = input.readLine()) != null) {
String[] s = ss.split("\r\n");
for (int i = 0; i < s.length; i++) {
list.add(Integer.parseInt(s[i].trim())); // 将String s中的内容添加到动态数组中
}
}
} catch (IOException e) {

e.printStackTrace();
}
in.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
}

return list;
}

}


运行结果:

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