zoukankan      html  css  js  c++  java
  • Basic Sort Algorithms

    1. Bubble Sort

    public void bubbleSort(int[] arr) {
      boolean swapped = true;
      int j = 0;
      int tmp;
      while (swapped) {
        swapped = false;
        j++;
        for (int i = 0; i < arr.length - j; i++) {
          if (arr[i] > arr[i + 1]) {
          tmp = arr[i];
          arr[i] = arr[i + 1];
          arr[i + 1] = tmp;
          swapped = true;
          }
        }
     }
    }

    Performance

    Worst case performance O(n^2)
    Best case performance O(n)
    Average case performance O(n^2)
    Worst case space complexity O(1) auxiliary

    2. Selection Sort

      public void doSelectionSort(int[] arr){ 
        for (int i = 0; i < arr.length - 1; i++){
          int index = i;
          for (int j = i + 1; j < arr.length; j++){
             if (arr[j] < arr[index]){
                index = j;
             }
          }
          int smallerNumber = arr[index]; 
          arr[index] = arr[i];
          arr[i] = smallerNumber;
       }
      }

    Performance

    Worst case performance О(n2)
    Best case performance О(n2)
    Average case performance О(n2)
    Worst case space complexity О(n) total, O(1) auxiliary

    3. Insertion Sort

    public static void insertionSort(int array[]) {
      int n = array.length;
      for (int j = 1; j < n; j++) {
        int key = array[j];
        int i = j-1;
        while ( (i > -1) && ( array [i] > key ) ) {
          array [i+1] = array [i];
          i--;
        }
        array[i+1] = key;
      }
    }

    Performance

    Worst case performance О(n2) comparisons, swaps
    Best case performance O(n) comparisons, O(1) swaps
    Average case performance О(n2) comparisons, swaps
    Worst case space complexity О(n) total, O(1) auxiliary

    Comparison:

    There’s probably no point in using the bubble sort, unless you don’t have your algorithm book handy. The bubble sort is so simple that you can write it from memory. Even so, it’s practical only if the amount of data is small.

    The selection sort minimizes the number of swaps, but the number of comparisons is still high. This sort might be useful when the amount of data is small and swapping data items is very time-consuming compared with comparing them. The insertion sort is the most versatile of the three and is the best bet in most situa- tions, assuming the amount of data is small or the data is almost sorted. For larger amounts of data, quicksort is generally considered the fastest approach.

    It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:

    We’ve compared the sorting algorithms in terms of speed. Another consideration for any algorithm is how much memory space it needs. All three of the algorithms in this chapter carry out their sort in place, meaning that, besides the initial array, very little extra memory is required. All the sorts require an extra variable to store an item temporarily while it’s being swapped.

  • 相关阅读:
    Flutter第一个应用--踩坑之路
    今天注册博客园了!
    广深小龙-基于unittest、pytest自动化测试框架之demo来学习啦!!!
    python接口自动化10-excel设计模式实战
    python接口自动化9-ddt数据驱动
    Docker学习4-学会如何让容器开机自启服务【坑】
    pytest-4-分布式运行与自定义顺序执行用例
    Docker学习10-docker-slenium进行web自动化测试
    linux+jenkins生成测试报告及任意IP打开链接能看到allure报告
    MySQL-Python实现-测试/生产环境各个表与字段进行对比的小工具
  • 原文地址:https://www.cnblogs.com/codingforum/p/6209330.html
Copyright © 2011-2022 走看看