【1】归并排序算法详解

package test;
/**
*
* @author lizongbao
*
*/
public class pack {
/**
*
* @param args
*/
public static void main(String[] args) {
int[] array = { 4, 3, 6, 5, 9, 0, 8, 1, 7, 2 };
// merge(array, 0, mid, array.length-1);
System.out.println(args.length);
System.out.println(args);

    sortCore(array);
    for (int i = 0; i < array.length; i++) {
        System.out.println(array[i]);
    }
}

/**
 * 
 * @param array
 */
private static void sortCore(int[] array) {
    //边界条件新检验
    if(array==null||array.length==0){
        return;
    }
    int length = array.length;

    int groupSize = 1;
    while (groupSize < length) {
        for (int i = 0; i < length; i += (groupSize * 2)) {
            int low = i;
            int hight = Math.min(i + groupSize * 2 - 1, length - 1);
            int middle = low + groupSize - 1;
            merge(array, low, middle >= hight ? (low + hight) / 2 : middle, hight);
        }
        groupSize *= 2;
    }·

    // 对分组中的奇数情况进行另外处理

// if (groupSize / 2 < length) {
// int low = 0;
// int hight = length - 1;
// merge(array, low, groupSize / 2 - 1, hight);
// }
}

/**
 * 
 * @param array
 * @param low
 * @param mid
 * @param hight
 */
private static void merge(int[] array, int low, int mid, int hight) {
    if (low >= hight) {
        return;
    }

    int[] auxArray = new int[hight - low + 1];
    int index1 = low;
    int index2 = mid + 1;

    int i = 0;
    while (index1 <= mid && index2 <= hight) {
        if (array[index1] <= array[index2]) {
            auxArray[i] = array[index1];
            index1++;
            i++;
        } else {
            auxArray[i] = array[index2];
            index2++;
            i++;
        }
    }

    // 继续合并前半段数组中未被合并的部分
    while (index1 <= mid) {
        auxArray[i] = array[index1];
        index1++;
        i++;
    }

    // 继续合并后半段数组中未被合并的部分
    while (index2 <= hight) {
        auxArray[i] = array[index2];
        index2++;
        i++;
    }

    // 将合并好的序列写回到数组中
    for (int j = 0; j < auxArray.length; j++) {
        array[low + j] = auxArray[j];
    }
}

}

先让我们看看原题的三个任务介绍: Task 1: Sorting the LINEITEM table by External Merge Sort Consider two cases: 1) using 5 buffer pages in memory for the external merge sort; 2) using 129 buffer pages in memory for the external merge sort. In the implementation, each buffer page occupies 8K bytes. The ORDERKEY attribute of the LINEITEM table is assumed to be the sort key in the external merge sort. Please report the number of passes and also the running time of the external merge sort in each case. Task 2: Organizing the sorted LINEITEM table into disk pages Please use the page format for storing variable-length records to organize the LINEITEM table sorted in Task 1. In the implementation, each disk page occupies 1K bytes. For each page we maintain a directory of slots, with a pair per slot. Both “record offset” and “record length” are 4 bytes wide. Task 3: Building a B-Tree over LINEITEM disk pages by Bulk Loading. Please use bulk loading to build a B-Tree over the disk pages of the LINEITEM table, which are generated in Task 2. The ORDERKEY attribute of the LINEITEM table is used as the (search) key for building the B-Tree. In the B-Tree, each internal node corresponds to a page of 1K bytes, both key and pointer are 4 bytes wide. Please report the running time of the bulk loading. A query interface is required for checking the B-Tree. For a reasonable ORDERKEY value, please print out all the pages visited along the path to find the corresponding record. Please also report the running time of the search.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值