zoukankan      html  css  js  c++  java
  • 数据结构与算法--冒泡排序

    冒泡排序(Bubble Sort)

    冒泡排序是一种交换排序

    基本思想:两两比较相邻的的元素,如果反须则交换,直到没有反序的记录为止

    代码实现:

      import java.text.SimpleDateFormat;
      import java.util.Arrays;
      import java.util.Date;
      public class BubbleSort {
          public static void main(String[] args) {
              int arr[] = {3,9,-1,10,-2};
              System.out.println("排序前:");
             System.out.println(Arrays.toString(arr));
              bubbleSort(arr);
              System.out.println("排序后:");
              System.out.println(Arrays.toString(arr));
    
          }
          public static void bubbleSort(int arr[]){
           int temp = 0;
              //冒泡排序的时间复杂度为O(n^2)
              for (int i = 0; i < arr.length - 1; i++) {
                  for (int j = 0; j < arr.length - 1 - i; j++) {
                      if (arr[j] > arr[j+1]){
                          temp = arr[j];
                          arr[j] =  arr[j+1];
                          arr[j+1] = temp;
                      }
                  }
              }
          }
      }
    

    由于排序时可能会出现不需要全部比较就已经是正确的排序结果的问题,因此需要对代码进行优化:

      public static void bubbleSort(int arr[]){
              int temp = 0;
              boolean flag = false;
              //冒泡排序的时间复杂度为O(n^2)
              for (int i = 0; i < arr.length - 1; i++) {
                  for (int j = 0; j < arr.length - 1 - i; j++) {
                      if (arr[j] > arr[j+1]){
                          flag = true;
                          temp = arr[j];
                          arr[j] =  arr[j+1];
                          arr[j+1] = temp;
                      }
                  }
                  System.out.println("第"+(i+1)+"次排序结果:");
                  System.out.println(Arrays.toString(arr));
                  if (!flag){
                      break;
                  }else {
                      flag = false; //重置flag,进行下次判断
                  }
              }
          }
    

    对冒泡排序的时间复杂度进行测试:

      public class BubbleSort {
          public static void main(String[] args) {
              //测试冒泡排序的时间复杂度
              int[] arrTest = new int[100000];
              for (int i = 0; i < 100000; i++) {
                  arrTest[i] = (int)(Math.random()*1000000); //生成一个[0,1000000)之间的数
              }
              Date date1 = new Date();
              SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
              String date1Str = simpleDateFormat.format(date1);
              System.out.println("排序前时间是:"+date1Str);
      
              bubbleSort(arrTest);
    
              Date date2 = new Date();
              String date2Str = simpleDateFormat.format(date2);
              System.out.println("排序前时间是:"+date2Str);
    
          }
          public static void bubbleSort(int arr[]){
              int temp = 0;
              boolean flag = false;
              //冒泡排序的时间复杂度为O(n^2)
              for (int i = 0; i < arr.length - 1; i++) {
                  for (int j = 0; j < arr.length - 1 - i; j++) {
                      if (arr[j] > arr[j+1]){
                          flag = true;
                          temp = arr[j];
                          arr[j] =  arr[j+1];
                          arr[j+1] = temp;
                      }
                  }
                  if (!flag){
                      break;
                  }else {
                      flag = false; //重置flag,进行下次判断
                  }
              }
          }
      }
    

    最终的测试结果可能因电脑性能而异!!

  • 相关阅读:
    线程,协程
    python魔法方法详解
    Sorted方法排序用法
    time模块
    Haroopad安装与配置: Linux系统下最好用的Markdown编辑器
    C++ Primer第五版答案
    Ubuntu14.04安装有道词典(openyoudao)
    Ubuntu14.04下Sublime Text 3解决无法输入中文
    OpenLTE安装教程
    GNU Radio: Overview of the GNU Radio Scheduler
  • 原文地址:https://www.cnblogs.com/ysera-y/p/13729427.html
Copyright © 2011-2022 走看看