算法导论排序算法汇总

插入排序

INSERTION-SORT(A)
1 for j=2 to A.length
2   key=A[j]
3   // insert A[j] into the sorted sequence A[1...j-1].
4   i=j-1
5   while i>0 and A[i]>key
6     A[i+1]=A[i]
7     i=i-1
8   A[i+1]=key

最坏运行时间为 Θ(n2) ,平均运行时间为 Θ(n2)
java代码如下:

    /**
     * 插入排序
     * @param A
     */
    public static void Insertion_Sort(double[] A){
        for (int i = 1; i < A.length; i++){
            double key = A[i];
            int j = i - 1;
            while (j >= 0 && A[j] > key){
                A[j + 1] = A[j];
                j--;
            }
            A[j + 1] = key;
        }
    }

归并排序

MERGE-SORT(A,p,r)
1 if p<r
2   q=(p+r)/2向下取整
3   MERGE-SORT(A,p,q)
4   MERGE-SORT(A,q+1,r)
5   MERGE(A,p,q,r)

其中 MERGE(A,p,q,r) 为:

MERGE(A,p,q,r)
1  n_1=q-p+1
2  n_2=r-q
3  let L[1..n_1+1] and R[1..n_2+1] be new arrays
4  for i = 1 to n_1
5    L[i] = A[p+i-1]
6  for j = 1 to n_2
7    R[j] = A[q+j]
8  L[n_1+1]=无穷大
9  R[n_2+1]=无穷大
10 i=1
11 j=1
12 for k=p to r
13   if L[i] <= R[j]
14     A[k]=L[i]
15     i=i+1
16   else A[k]=R[j]
17     j=j+1

最坏情况运行时间为 Θ(nlgn) ,平均情况为 Θ(nlgn)
java代码如下:

    /**
     * 归并排序
     * @param A
     * @param p
     * @param r
     */
    public static void Merge_Sort(double[] A, int p, int r){
        if (p < r){
            int q = (p + r)/2;
            Merge_Sort(A, p,q);
            Merge_Sort(A, q+1, r);
            Merge(A, p, q, r);
        }
    }

    public static void Merge(double[] A, int p, int q, int r){
        int n1 = q - p + 1;
        int n2 = r - q;
        double[] L = new double[n1 + 1];
        double[] R = new double[n2 + 1];
        for (int i = 0; i < n1; i++){
            L[i] = A[p + i];
        }
        for (int j = 0; j < n2; j++){
            R[j] = A[q + j + 1];
        }
        L[n1] = R[n2] = 100000;
        for (int k = p, i = 0, j = 0; k <= r; k++){
            if (L[i] <= R[j]){
                A[k] = L[i];
                i++;
            }else{
                A[k] = R[j];
                j++;
            }
        }
    }

冒泡排序

BUBBLESORT(A)
1 for i = 1 to A.length -1
2   for j = A.length downto i + 1
3     if A[j] < A[j-1]
4       exchange A[j] with A[j-1]

最坏情况的运行时间为 Θ(n2) ,平均运行时间为 O(n2)
java代码如下:

    /**
     * 冒泡排序
     * @param A
     */
    public static void Bubble_Sort(double[] A){
        for (int i = 0; i < A.length -1; i++){
            for (int j = A.length - 1; j >= i + 1; j--){
                if (A[j] < A[j - 1]){
                    double x = A[j];
                    A[j] = A[j - 1];
                    A[j - 1] = x;
                }
            }
        }
    }

堆排序

首先需要用到的几个函数:

LEFT(i)
1 return 2i

RIGHT(i)
1 return 2i + 1

然后MAX-HEAPIFY(A,i)函数:

MAX-HEAPIFY(A,i)
1  l = LEFT(i)
2  r = RIGHT(i)
3  if l <= A.heap-size and A[l] > A[i]
4    largest = l
5  else largest = i
6  if r <= A.heap-size and A[r] > A[largest]
7    largest = r
8  if largest 不等于 i
9    exchange A[i] with A[largest]
10   MAX-HEAPIFY(A,largest)

还有BUILD-MAX-HEAPIFY(A)函数:

BUILD-MAX-HEAP(A)
1 A.heap-size = A.length
2 for i = \lfloor A.length/2 \rfloor downto 1
3   MAX-HEAPIFY(A,i)

最后的堆排序算法:

HEAPSORT(A)
1 BUILD-MAX-HEAP(A)
2 for i = A.length downto 2
3   exchange A[1] with A[i]
4   A.heap-size = A.heap-size - 1
5   MAX-HEAPIFY(A,1)

堆排序的最坏情况运行时间为 O(nlgn)
java代码如下:

public static void Max_Heapify(double[] A, int heap_size, int i){
        int l = 2*i + 1;
        int r = 2*i + 2;
        int largest;
        if (l <= heap_size - 1 && A[l] > A[i]){
            largest = l;
        }else {
            largest = i;
        }
        if (r <= heap_size - 1 && A[r] > A[largest]){
            largest = r;
        }
        if (largest != i){
            double x = A[i];
            A[i] = A[largest];
            A[largest] = x;
            Max_Heapify(A, heap_size, largest);
        }
    }

    public static void Build_Max_Heapify(double[] A, int heap_size){
        heap_size = A.length;
        for (int i = A.length/2; i >= 0; i--){
            Max_Heapify(A, heap_size, i);
        }
    }

    /**
     * 堆排序
     * @param A
     */
    public static void Heap_Sort(double[] A){
        int heap_size = A.length;
        Build_Max_Heapify(A, heap_size);
        for (int i = A.length - 1; i >= 1; i--){
            double x = A[0];
            A[0] = A[i];
            A[i] = x;
            heap_size--;
            Max_Heapify(A, heap_size, 0);
        }
    }

快速排序

QUICKSORT(A, p, r)
1 if p < r
2   q = PARTITION(A, p, r)
3   QUICKSORT(A, p, q - 1)
4   QUICKSORT(A, q + 1, r)

其中

PARTITION(A, p, r)
1 x = A[r]
2 i = p - 1
3 for j = p to r - 1
4   if A[j] <= x
5     i = i + 1
6     exchange A[i] with A[j]
7 exchange A[i + 1] with A[r]
8 return i + 1

java实现代码如下:

    /**
     * 快速排序
     * @param A
     * @param p p应该从0开始
     * @param r r应该从A.length-1开始
     */
    public static void Quick_Sort(double[] A, int p, int r){
        if (p < r){
            int q = Partition(A, p, r);
            Quick_Sort(A, p, q - 1);
            Quick_Sort(A, q + 1, r);
        }
    }

    private static int Partition(double[] A, int p, int r) {
        // TODO Auto-generated method stub
        double x = A[r];
        int i = p - 1;
        for (int j = p; j < r; j++){
            if (A[j] <= x){
                i++;
                double m = A[i];
                A[i] = A[j];
                A[j] = m;
            }
        }
        double m = A[i + 1];
        A[i + 1] = A[r];
        A[r] = m;
        return i + 1;
    }

计数排序

COUNTING-SORT(A, B, k)
1  let C[0 ..k] be a new array
2  for i = 0 to k
3    C[i] = 0
4  for j = 1 to A.length
5    C[A[j]] = C[A[j]] + 1
6  // C[i] now contains the number of elements equal to i.
7  for i = 1 to k
8    C[i] = C[i] + C[i - 1]
9  // C[i] now contains the number of elements less than or equal to i.
10 for j = A.length downto 1
11   B[C[A[j]]] = A[j]
12   C[A[j]] = C[A[j]] - 1

java代码如下:

    /**
     * 要求A中元素必须是非负整数
     * @param A
     * @param k A中最大的元素加1
     */
    public static int[] Counting_Sort(int[] A, int k){
        int[] C = new int[k];
        int[] B = new int[A.length];
        for (int i = 0; i < A.length; i++){
            C[A[i]] = C[A[i]] + 1;
        }
        for (int i = 1; i < C.length; i++){
            C[i] = C[i] + C[i - 1];
        }
        for (int j = A.length - 1; j >= 0; j--){
            B[C[A[j]] - 1] = A[j];
            C[A[j]] = C[A[j]] - 1;
        }
        return B;
    }

基数排序

RADIX-SORT(A, d)
1 for i = 1 to d
2   use a stable sort sort array A on digit i

java实现代码:

    /**
     * 基数排序
     * @param A
     * @param d 数组A中最大数的位数
     * @return
     */
    public static int[] Radix_Sort(int[] A, int d){
        int[] B = new int[A.length];            // 用来存储每次排序好的数组
        int[] temp = new int[A.length];         // 用来复制排序好的数组,并在下次循环中对其中的数据的第i位进行排序
        System.arraycopy(A, 0, temp, 0, A.length);
        for (int i = 1; i <= d; i++){
            int[] C = new int[10];

            for (int j = 0; j < temp.length; j++){
                int Ai = (temp[j]/(int)Math.pow(10, i - 1))%10;     // 求每位数的第i位
                C[Ai]++;
            }

            for (int k = 1; k < C.length; k++){
                C[k] = C[k] + C[k - 1]; 
            }

            for (int h = temp.length - 1; h >= 0; h--){
                int Ai = (temp[h]/(int)Math.pow(10, i - 1))%10;
                B[C[Ai] - 1] = temp[h];
                C[Ai] = C[Ai] - 1;
            }
            System.arraycopy(B, 0, temp, 0, B.length);
        }
        return B;
    }

桶排序

BUCKET-SORT(A)
1 n = A.length
2 let B[0..n-1] be a new array
3 for i = 0 to n - 1
4   nake B[i] an empty list
5 for i = 1 to n
6   insert A[i] into list B[\lfloor nA[i] \rfloor]
7 for i = 0 to n - 1
8   sort list B[i] with insertion sort
9 concatenate the lists B[0], B[1], ... , B[n-1] togather in ordera

java实现代码如下:

    /**
     * 桶排序
     * @param A
     * @return
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static double[] Bucket_Sort(double[] A){
        int n = A.length;
        List[] B = new ArrayList[n];
        for (int i = 0; i < n; i++){
            B[i] = new ArrayList();
        }
        for (int i = 0; i < n; i++){
            B[(int)(n*A[i])].add(A[i]);
        }
        for (int i = 0; i < n; i++){
            Insertion_Sort(B[i]);
        }
        double[] C = new double[A.length];
        int j = 0;
        for (int i = 0; i < n; i++){
            for (Object x : B[i]){
                C[j++] = (double)x;
            }
        }
        return C;
    }

    public static void Insertion_Sort(List A){
        for (int i = 1; i < A.size(); i++){
            double key = (double)A.get(i);
            int j = i - 1;
            while (j >= 0 && (double)A.get(j) > key){
                A.set(j + 1, A.get(j));
                j--;
            }
            A.set(j + 1, key);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值