zoukankan      html  css  js  c++  java
  • 归并排序

    public class MergeSort {
    
        private static void sort(int[] a, int i, int length, int[] b) {
            if (i < length) {
                int mid = (length + i) / 2;
                sort(a, i, mid, b);
                sort(a, mid + 1, length, b);
                merge(a, b, mid, i, length);
            }
        }
    
        private static void merge(int[] a, int[] b, int mid, int i, int length) {
            int t = 0;
            int o = i;
            int k = mid + 1;
            while (i <= mid && k <= length) {
                if (a[i] > a[k]) {
                    b[t++] = a[k++];
                } else {
                    b[t++] = a[i++];
                }
            }
            while (i <= mid) {
                b[t++] = a[i++];
            }
    
            while (k <= length) {
                b[t++] = a[k++];
            }
    
            t = 0;
            while (o <= length) {
                a[o++] = b[t++];
            }
        }
    
        public static void main(String[] args) {
            int a[] = { 3, 2, 1, 6, 8 };
            int b[] = new int[a.length];
            sort(a, 0, a.length - 1, b);
            System.out.println(Arrays.toString(a));
        }
    }
  • 相关阅读:
    Count_sort C++
    Priority_Queue C++
    HeapSort C++
    Randomized_QuickSort C++
    QuickSort c++
    我的开发环境
    github and eclipse
    centos 7 install eclipse cdt and use github
    github commands
    文化-梁晓声
  • 原文地址:https://www.cnblogs.com/hansc-blog/p/9351878.html
Copyright © 2011-2022 走看看