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.

  • 相关阅读:
    Android SD卡读写文件
    Android 是什么
    Canvas 类
    Java IO流之字节流 FileInputStream
    Android中asset文件夹和raw文件夹区别
    随手收藏
    Java IO流
    Android私有文件资源文件的存取
    ubuntu 下的jdk安装
    Paint类
  • 原文地址:https://www.cnblogs.com/codingforum/p/6209283.html
Copyright © 2011-2022 走看看