冒泡排序运行的时间和归并排序运行时间的比较
2014-03-27 00:35
344 查看
下面是一万个随机数据采用冒泡排序所用的时间(由于老师给的一百万个数据太大,我在等了半小时后没有结果,),具体代码如下;
冒泡排序:
运行结果:
归并排序:
运行结果:
排序结果一样:
总结:从上面的排序时间来说,归并排序的时间明显比冒泡排序用的时间少!经查资料(数据结构)得知,归并排序是一种比较稳定的排序方法!
总的时间代价是O(nlog2n),而冒泡排序的时间是O(n的二次方)
冒泡排序:
package three.suanfa.whp; 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) { // TODO 自动生成的方法存根 double start_all = System.currentTimeMillis(); String path = "txt/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("txt/largeW_bubble.txt"); FileOutputStream fou = null; try { fou = new FileOutputStream(f, true);// 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) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return list; } }
运行结果:
归并排序:
package three.suanfa.whp; 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) { // TODO 自动生成的方法存根 double start_all = System.currentTimeMillis(); String path = "txt/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("txt/largeW_merge.txt"); FileOutputStream fou = null; try { fou = new FileOutputStream(f, true);// 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]; // 如何把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(); } } } //归并排序 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只会执行其中一个) while (mid <= right) { tmpArr[third++] = data.get(mid++); } while (left <= center) { tmpArr[third++] = data.get(left++); } // 将临时数组中的内容拷贝回原数组中 // (原left-right范围的内容被复制回原数组) 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) { // TODO 自动生成的 catch 块 e.printStackTrace(); } in.close(); input.close(); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return list; } }
运行结果:
排序结果一样:
总结:从上面的排序时间来说,归并排序的时间明显比冒泡排序用的时间少!经查资料(数据结构)得知,归并排序是一种比较稳定的排序方法!
总的时间代价是O(nlog2n),而冒泡排序的时间是O(n的二次方)
相关文章推荐
- 比较 N个元素 一个一个地插入到一个堆中 以线性时间建立一个堆 已排序、反序、随机输入的运行时间
- 冒泡排序,快速排序,插入排序以及二叉树查找的O级时间的比较
- 选择、冒泡、快速、归并、插入排序算法的运行时间比较
- 数据结构(C#)--冒泡、插入、快速、堆、归并、希尔、选择各种排序排序过程比较以及各种排序的所用时间的对比
- 六种简单排序结构运行时间比较
- 体验复杂度 2种排序方法的运行时间比较
- 算法运行时间、logN、NlogN 之间的比较
- iOS开发--图形化排序算法比较:快速排序、插入排序、选择排序、冒泡排序
- 比较String[i].charAt(j)与char[i][j]的运行时间
- 冒泡排序和比较排序(系统排序)
- 快速排序 选择排序 冒泡排序 二分插入排序时间性能
- GRNN/PNN:基于GRNN、PNN两神经网络实现并比较鸢尾花种类识别正确率、各个模型运行时间对比—Jason niu
- 插入排序、冒泡排序、选择排序、快速排序、堆排序、归并排序算法比较
- 冒泡排序,选择排序,插入排序,快速排序的比较及优化
- 算法学习 - 归并排序,快速排序,冒泡排序
- 实例说明:多进程和多线程 运行时间比较
- 一个比较容易理解的归并排序--递归实现
- 基于比较的排序,时间复杂度下界是o(nlogn)的小证明
- 编写查找一个单链表特定元素的程序。分别使用递归和非递归方法实现,并比较它们的运行时间。
- 排序--选择排序,插入排序,冒泡排序,shell排序,快速排序(递归,迭代,改进版本),归并排序