algorithm


This draft deletes the entire topic.

Introduction

Introduction

expand all collapse all

Examples

  • 5

    Merge Sort is a divide-and-conquer algorithm. It divides the input list of length n in half successively until there are n lists of size 1. Then, pairs of lists are merged together with the smaller first element among the pair of lists being added in each step. Through successive merging and through comparison of first elements, the sorted list is built.

    An example:

    Merge Sort example

    Time Complexity: T(n) = 2T(n/2) + Θ(n)

    The above recurrence can be solved either using Recurrence Tree method or Master method. It falls in case II of Master Method and solution of the recurrence is Θ(nLogn). Time complexity of Merge Sort is Θ(nLogn) in all 3 cases (worst, average and best) as merge sort always divides the array in two halves and take linear time to merge two halves.

    Auxiliary Space: O(n)

    Algorithmic Paradigm: Divide and Conquer

    Sorting In Place: Not in a typical implementation

    Stable: Yes

  • 1
    package main
    
    import "fmt"
    
    func mergeSort(a []int) []int {
        if len(a) < 2 {
            return a
        }
        m := (len(a)) / 2
    
        f := mergeSort(a[:m])
        s := mergeSort(a[m:])
    
        return merge(f, s)
    }
    
    func merge(f []int, s []int) []int {
        var i, j int
        size := len(f) + len(s)
    
        a := make([]int, size, size)
    
        for z := 0; z < size; z++ {
            lenF := len(f)
            lenS := len(s)
    
            if i > lenF-1 && j <= lenS-1 {
                a[z] = s[j]
                j++
            } else if j > lenS-1 && i <= lenF-1 {
                a[z] = f[i]
                i++
            } else if f[i] < s[j] {
                a[z] = f[i]
                i++
            } else {
                a[z] = s[j]
                j++
            }
        }
    
        return a
    }
    
    func main() {
        a := []int{75, 12, 34, 45, 0, 123, 32, 56, 32, 99, 123, 11, 86, 33}
        fmt.Println(a)
        fmt.Println(mergeSort(a))
    }
    
  • 0
    public class MergeSortBU {
        private static Integer[] array = { 4, 3, 1, 8, 9, 15, 20, 2, 5, 6, 30, 70, 60,80,0,9,67,54,51,52,24,54,7 };
    
        public MergeSortBU() {
        }
    
        private static void merge(Comparable[] arrayToSort, Comparable[] aux, int lo,int mid, int hi) {
    
            for (int index = 0; index < arrayToSort.length; index++) {
                aux[index] = arrayToSort[index];
            }
    
            int i = lo;
            int j = mid + 1;
            for (int k = lo; k <= hi; k++) {
                if (i > mid)
                    arrayToSort[k] = aux[j++];
                else if (j > hi)
                    arrayToSort[k] = aux[i++];
                else if (isLess(aux[i], aux[j])) {
                    arrayToSort[k] = aux[i++];
                } else {
                    arrayToSort[k] = aux[j++];
                }
    
            }
        }
    
        public static void sort(Comparable[] arrayToSort, Comparable[] aux, int lo, int hi) {
            int N = arrayToSort.length;
            for (int sz = 1; sz < N; sz = sz + sz) {
                for (int low = 0; low < N; low = low + sz + sz) {
                    System.out.println("Size:"+ sz);
                    merge(arrayToSort, aux, low, low + sz -1 ,Math.min(low + sz + sz - 1, N - 1));
                    print(arrayToSort);
                }
            }
    
        }
    
        public static boolean isLess(Comparable a, Comparable b) {
            return a.compareTo(b) <= 0;
        }
    
        private static void print(Comparable[] array) {http://stackoverflow.com/documentation/algorithm/5732/merge-sort#
            StringBuffer buffer = new StringBuffer();http://stackoverflow.com/documentation/algorithm/5732/merge-sort#
            for (Comparable value : array) {
                buffer.append(value);
                buffer.append(' ');
            }
            System.out.println(buffer);
        }
    
        public static void main(String[] args) {
            Comparable[] aux = new Comparable[array.length];
            print(array);
            MergeSortBU.sort(array, aux, 0, array.length - 1);
        }
    }
    
Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about Merge Sort? Ask Question

Topic Outline