吐血(3)归并排序

记录一下今晚踩过的坑

/**
 * author : Jerry
 **/
package algorithm;

/**
 * Created by sooglejay on 16/8/1.
 */
public class MergeSort {
    public static void main(String[] args) {

        int A[] = new int[]{1, 2,10,11,22, 3, 4, 5, 1, 2, 3};
        mergeSort(A, 0, A.length - 1);
        for (int a : A) {
            System.out.print(a + " ");
        }

        //fix bug : int[] is instanceof Object
        //is true as syntax
        System.out.print(A instanceof Object);
        int test = 100;
// you will get error if you execute the Commented-Out Code
// System.out.print(test instanceof Object);

    }

    public static void mergeSort(int A[], int p, int r) {
        if (p < r) {
            int q = (p + r) / 2;
            mergeSort(A, p, q);
            mergeSort(A, q + 1, r);
            merge(A, p, q, r);
        }
    }
    //each index is the real index used by array,should not minus one
    public static void merge(int A[], int m, int s, int r) {
        int n1 = s - m + 1;
        int n2 = r - s;
        int L[] = new int[n1];
        int R[] = new int[n2];

        int i = 0;
        for (; i < n1; i++) {
            L[i] = A[i + m];//last element is A[n1-1+m=s]
        }
        int j = 0;
        for (; j < n2; j++) {
            R[j] = A[j + s + 1];//last element is A[n2-1+s+1]=A[r]
        }
        //fix bug
        i = 0;
        j = 0;//you have to reset the index of L and R

        //fix bug
        int k = m;//please note here, k=m not k=A.length,and also pls remember the index of r
        for (; k <= r; k++) {
            if (i == n1 || j == n2) {
                break;
            }
            if (L[i] < R[j]) {
                A[k] = L[i];
                i++;
            } else {
                A[k] = R[j];
                j++;
            }
        }
        while (i < n1) {
            A[k] = L[i];
            i++;
            k++;
        }
        while (j < n2) {
            A[k] = R[j];
            j++;
            k++;
        }

    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值