7. Basic data structure and algorithms in java - Merge sort

Merge sort

Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l…m] and arr[m+1…r] are sorted and merges the two sorted sub-arrays into one.

MergeSort(arr[], l, r)
If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)

在这里插入图片描述
在这里插入图片描述

public class Study {
    /**
     * main function to sort array using merge sort
     *
     * @param args
     */
    public static void main(String[] args) {
        int[] arr = {1, -10, 100, -100, 0, 2, 5, 8, 25};
        print(arr);
        System.out.println();
        mergeAsc(arr);
        // if we want descending order we just reverse the ascending order
        print(arr);
    }

    /**
     * merge sort arr
     *
     * @param arr
     */
    private static void mergeAsc(int[] arr) {
        mergeSort(arr, 0, arr.length - 1);
    }

    /**
     * merge sort arr index from start to end
     *
     * @param arr
     * @param start
     * @param end
     */
    public static void mergeSort(int[] arr, int start, int end) {
        if (start >= end) return; // if start >= end directly return
        int mid = (start + end) / 2; // get the mid index
        mergeSort(arr, start, mid); // sort the left part
        mergeSort(arr, mid + 1, end); // sort the right part
        merge(arr, start, mid, end); // merge the two part together
    }

    /**
     * merge two sorted array into one array
     *
     * @param arr
     * @param start
     * @param mid
     * @param end
     */
    public static void merge(int[] arr, int start, int mid, int end) {
        // if the first part's largest element is smaller than
        // the second part's smallest element directly return
        if (arr[mid] <= arr[mid + 1]) return;
        // i, j and tempIndex to merge two parts together
        int i = start;
        int j = mid + 1;
        int tempIndex = 0;
        // temp array to hold sorted element from start to end in the original array
        int[] temp = new int[end - start + 1];
        // keep merge two array when any one of the two parts still have some elements
        while (i <= mid && j <= end) {
            temp[tempIndex++] = arr[i] <= arr[j] ? arr[i++] : arr[j++];
        }
        while (i <= mid) { // copy left part into temp
            temp[tempIndex++] = arr[i++];
        }
        while (j <= end) { // copy right part into temp
            temp[tempIndex++] = arr[j++];
        }
        int idx = start; // copy element from temp into original array from index start to end
        for (int k = 0; k < tempIndex; k++) {
            arr[idx++] = temp[k];
        }
    }

    /**
     * print the array
     *
     * @param arr
     */
    private static void print(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print("arr[" + i + "] = " + arr[i] + " ");
        }
    }
}

Question
When we want to make this sort into descending order, how can we do this?

  1. Sort it into ascending order and then reverse the whole array into descending order
  2. Modify the code to directly sort array into descending order, I still don’t figure out it, how to modify this code???
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值