Answers for "what is a merge sort"

1

merge sort iterative (string)

//Joshua Khumalo
import java.util.Arrays;
public class Demo{
   public static void merge_sort(int[] my_arr){
      if(my_arr == null){
         return;
      }
      if(my_arr.length > 1){
         int mid = my_arr.length / 2;
         int[] left = new int[mid];
         for(int i = 0; i < mid; i++){
            left[i] = my_arr[i];
         }
         int[] right = new int[my_arr.length - mid];
         for(int i = mid; i < my_arr.length; i++){
            right[i - mid] = my_arr[i];
         }
         merge_sort(left);
         merge_sort(right);
         int i = 0;
         int j = 0;
         int k = 0;
         while(i < left.length && j < right.length){
            if(left[i] < right[j]){
               my_arr[k] = left[i];
               i++;
            } else {
               my_arr[k] = right[j];
               j++;
            }
            k++;
         }
         while(i < left.length){
            my_arr[k] = left[i];
            i++;
            k++;
         }
         while(j < right.length){
            my_arr[k] = right[j];
            j++;
            k++;
         }
      }
   }
   public static void main(String[] args){
      int my_arr[] = {56, 78, 91, 21, 34, 0, 11};
      int i=0;
      merge_sort(my_arr);
      System.out.println("The array after sorting is ");
      for(i=0; i<my_arr.length; i++)
      System.out.print(my_arr[i]+" ");
   }
}
Posted by: Guest on September-23-2020
6

merge sort

MergeSort(arr[], l,  r)
If r > l
     1. Find the middle point to divide the array into two halves:  
             middle m = l+ (r-l)/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)
Posted by: Guest on June-27-2021
2

Merge sort

class Sort 
{
    void merge(int arr[], int left, int middle, int right)
    {
        int low = middle - left + 1;                    //size of the left subarray
        int high = right - middle;                      //size of the right subarray
 
        int L[] = new int[low];                             //create the left and right subarray
        int R[] = new int[high];

        int i = 0, j = 0;
 
        for (i = 0; i < low; i++)                               //copy elements into left subarray
        {
            L[i] = arr[left + i];
        }
        for (j = 0; j < high; j++)                              //copy elements into right subarray
        {
            R[j] = arr[middle + 1 + j];
        }
        
 
        int k = left;                                           //get starting index for sort
        i = 0;                                             //reset loop variables before performing merge
        j = 0;

        while (i < low && j < high)                     //merge the left and right subarrays
        {
            if (L[i] <= R[j]) 
            {
                arr[k] = L[i];
                i++;
            }
            else 
            {
                arr[k] = R[j];
                j++;
            }
            k++;
        }
 
        while (i < low)                             //merge the remaining elements from the left subarray
        {
            arr[k] = L[i];
            i++;
            k++;
        }
 
        while (j < high)                           //merge the remaining elements from right subarray
        {
            arr[k] = R[j];
            j++;
            k++;
        }
    }
 

    void mergeSort(int arr[], int left, int right)       //helper function that creates the sub cases for sorting
    {
        int middle;
        if (left < right) {                             //sort only if the left index is lesser than the right index (meaning that sorting is done)
            middle = (left + right) / 2;
 
            mergeSort(arr, left, middle);                    //left subarray
            mergeSort(arr, middle + 1, right);               //right subarray
 
            merge(arr, left, middle, right);                //merge the two subarrays
        }
    }
 
    void display(int arr[])                 //display the array
    {  
        for (int i=0; i<arr.length; ++i) 
        {
            System.out.print(arr[i]+" ");
        } 
    } 

    public static void main(String args[])
    {
        int arr[] = { 9, 3, 1, 5, 13, 12 };
        Sort ob = new Sort();
        ob.mergeSort(arr, 0, arr.length - 1);
        ob.display(arr);
    }
}
Posted by: Guest on May-31-2021
2

merge sort

// @see https://www.youtube.com/watch?v=es2T6KY45cA&vl=en
// @see https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html

function merge(list, start, midpoint, end) {
    const left = list.slice(start, midpoint);
    const right = list.slice(midpoint, end);
    for (let topLeft = 0, topRight = 0, i = start; i < end; i += 1) {
        if (topLeft >= left.length) {
            list[i] = right[topRight++];
        } else if (topRight >= right.length) {
            list[i] = left[topLeft++];
        } else if (left[topLeft] < right[topRight]) {
            list[i] = left[topLeft++];
        } else {
            list[i] = right[topRight++];
        }
    }
}

function mergesort(list, start = 0, end = undefined) {
    if (end === undefined) {
        end = list.length;
    }
    if (end - start > 1) {
        const midpoint = ((end + start) / 2) >> 0;
        mergesort(list, start, midpoint);
        mergesort(list, midpoint, end);
        merge(list, start, midpoint, end);
    }
    return list;
}

mergesort([4, 7, 2, 6, 4, 1, 8, 3]);
Posted by: Guest on May-31-2020
0

merge sort

void merge(int arr[], int l, int m, int r)
    {
         int n=r-l+1,temp[n];
         int i=l,j=m+1,k=0;
         while(i<=m and j<= r)
         {
             if(arr[i]< arr[j]){
                temp[k++] = arr[i++];
             else
                temp[k++] = arr[j++];
         }
         while(i<=m)
            temp[k++] = arr[i++];
         while(j<=r)
            temp[k++] = arr[j++];
         int ind= 0;
         for(int i=l;i<=m;i++)
             arr[i] = temp[ind++];
         for(int j=m+1;j<=r;j++)
            arr[j] = temp[ind++];
    }
    
    void mergeSort(int arr[], int l, int r)
    {
       if(l<r)
       {
           int mid = (r+l)/2;
           mergeSort(arr,l,mid);
           mergeSort(arr,mid+1,r);
           merge(arr,l,mid,r);
       }
    }
Posted by: Guest on July-19-2021
0

merge sort algorithm

/*  
    a[] is the array, p is starting index, that is 0, 
    and r is the last index of array. 
*/

#include <stdio.h>

// lets take a[5] = {32, 45, 67, 2, 7} as the array to be sorted.

// merge sort function
void mergeSort(int a[], int p, int r)
{
    int q;
    if(p < r)
    {
        q = (p + r) / 2;
        mergeSort(a, p, q);
        mergeSort(a, q+1, r);
        merge(a, p, q, r);
    }
}

// function to merge the subarrays
void merge(int a[], int p, int q, int r)
{
    int b[5];   //same size of a[]
    int i, j, k;
    k = 0;
    i = p;
    j = q + 1;
    while(i <= q && j <= r)
    {
        if(a[i] < a[j])
        {
            b[k++] = a[i++];    // same as b[k]=a[i]; k++; i++;
        }
        else
        {
            b[k++] = a[j++];
        }
    }
  
    while(i <= q)
    {
        b[k++] = a[i++];
    }
  
    while(j <= r)
    {
        b[k++] = a[j++];
    }
  
    for(i=r; i >= p; i--)
    {
        a[i] = b[--k];  // copying back the sorted list to a[]
    } 
}

// function to print the array
void printArray(int a[], int size)
{
    int i;
    for (i=0; i < size; i++)
    {
        printf("%d ", a[i]);
    }
    printf("n");
}
 
int main()
{
    int arr[] = {32, 45, 67, 2, 7};
    int len = sizeof(arr)/sizeof(arr[0]);
 
    printf("Given array: n");
    printArray(arr, len);
    
    // calling merge sort
    mergeSort(arr, 0, len - 1);
 
    printf("nSorted array: n");
    printArray(arr, len);
    return 0;
}
Posted by: Guest on August-08-2020

Python Answers by Framework

Browse Popular Code Answers by Language