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

    1. Merge Sort

    public class Mergesort {
     private int[] numbers;
     private int[] helper;
    
     private int number;
    
     public void sort(int[] values) {
       this.numbers = values;
       number = values.length;
       this.helper = new int[number];
       mergesort(0, number - 1);
     }
    
     private void mergesort(int low, int high) {
       if (low < high) {
         int middle = low + (high - low) / 2;
         mergesort(low, middle);
         mergesort(middle + 1, high);
         merge(low, middle, high);
       }
     }
    
     private void merge(int low, int middle, int high) {
       for (int i = low; i <= high; i++) {
         helper[i] = numbers[i];
       }
       int i = low;
       int j = middle + 1;
       int k = low;
       while (i <= middle && j <= high) {
         if (helper[i] <= helper[j]) {
         numbers[k] = helper[i];
         i++;
         } else {
         numbers[k] = helper[j];
         j++;
         }
       k++;
      } 
      while (i <= middle) {
        numbers[k] = helper[i];
        k++;
        i++;
      }
     }
    }

    Performance
    Worst case performance O(n log n)
    Best case performance  O(n log n) typical, O(n) natural variant
    Average case performance O(n log n)
    Worst case space complexity O(n) auxiliary

    2. Shell Sort

    public static void main(String[] args) {
      int[]a={49,38,65,97,76,13,27,49,78,34,12,64,1};
      System.out.println("Beforeing:");
      for(int i=0;i<a.length;i++){
        System.out.print(a[i]+"");
      }
      int d=a.length;
      while(true) {
        d=d/2;
        for(int x=0;x<d;x++){
          for(int i=x+d;i<a.length;i=i+d){
            int temp=a[i];
            int j;
            for(j=i-d;j>=0&&a[j]>temp;j=j-d){
              a[j+d]=a[j];
            }
            a[j+d]=temp;
          }
        }
       if(d==1){
         break;
       }
      }
      System.out.println();
      System.out.println("Aftering:");
      for(inti=0;i<a.length;i++){
        System.out.print(a[i]+"");
      }
    }

    Performance
    Worst case performance O(n2)
    Best case performance O(n log n)
    Average case performance depends on gap sequence
    Worst case space complexity О(n) total, O(1) auxiliary

    Comparison

    The Shellsort is good for medium-sized arrays, perhaps up to a few thousand items, depending on the particular implementation. It’s not quite as fast as quicksort and other O(N*logN) sorts, so it’s not optimum for very large files. However, it’s much faster than the O(N2) sorts like the selection sort and the insertion sort, and it’s very easy to implement.

    3. Quick Sort

    public class Quicksort {
     private int[] numbers;
     private int number;
    
     public void sort(int[] values) {
       if (values ==null || values.length==0){
       return;
       }
       this.numbers = values;
       number = values.length;
       quicksort(0, number - 1);
     }
    
     private void quicksort(int low, int high) {
       int i = low, j = high;
       int pivot = numbers[low + (high-low)/2];
       while (i <= j) {
         while (numbers[i] < pivot) {
         i++;
         }
       while (numbers[j] > pivot) {
         j--;
       }
       if (i <= j) {
       exchange(i, j);
       i++;
       j--;
       }
      }
     if (low < j)
       quicksort(low, j);
     if (i < high)
       quicksort(i, high);
    }
    
     private void exchange(int i, int j) {
       int temp = numbers[i];
       numbers[i] = numbers[j];
       numbers[j] = temp;
     }
    }

    Performance
    Worst case performance O(n2)
    Best case performance O(n log n) (simple partition) or O(n) (three -way partition and equal keys)
    Average case performance O(n log n)
    Worst case space complexity O(n) auxiliary (naive) O(log n) auxiliary (Sedgewick 1978)

    Comparison
    Quicksort is undoubtedly the most popular sorting algorithm, and for good reason: In the majority of situations, it’s the fastest, operating in O(N*logN) time. (This is only true for internal or in-memory sorting; for sorting data in disk files, other algorithms may be better.)

    4. Radix Sort

    public void radixsort(int[] input) {
     final int RADIX = 10;
     List<Integer>[] bucket = new ArrayList[RADIX];
     for (int i = 0; i < bucket.length; i++) {
       bucket[i] = new ArrayList<Integer>();
     }
     
     boolean maxLength = false;
     int tmp = -1, placement = 1;
     while (!maxLength) {
       maxLength = true;
       for (Integer i : input) {
         tmp = i / placement;
         bucket[tmp % RADIX].add(i);
         if (maxLength && tmp > 0) {
           maxLength = false;
         }
       }
      int a = 0;
      for (int b = 0; b < RADIX; b++) {
        for (Integer i : bucket[b]) {
          input[a++] = i;
        }
        bucket[b].clear();
      }
     placement *= RADIX;
    }
    }

    Performance
    Worst case performance O(kN)
    Worst case space complexity O(k + N)

    Comparison
    Of course, like mergesort, the radix sort uses about twice as much memory as quicksort.  It’s generally true that if you have more data items, you’ll need longer keys. If you have 10 times as much data, you may need to add another digit to the key. The number of copies is proportional to the number of data items times the number of digits in the key. The number of digits is the log of the key values, so in most situations we’re back to O(N*logN) efficiency, the same as quicksort.

  • 相关阅读:
    静态绑定与动态绑定
    Java之浅拷贝与深拷贝
    python类对象及类实例的创建过程
    以订单和商品为例进行详细的组内数据获取的分析
    根据给定时间及偏移的年份求偏移后时间的前一天(支持偏移量为正和负)
    mysql取到组内的前几条数据
    python进程通信的几种实现方式
    python-redis之数据类型二
    python-redis之数据类型
    python-redis
  • 原文地址:https://www.cnblogs.com/codingforum/p/6209283.html
Copyright © 2011-2022 走看看