merge sort algo

// @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]);

4.25
4
Awgiedawgie 440215 points

                                    def mergeSort(arr): 
    if len(arr) &gt;1: 
        mid = len(arr)//2 # Finding the mid of the array 
        L = arr[:mid] # Dividing the array elements  
        R = arr[mid:] # into 2 halves 
  
        mergeSort(L) # Sorting the first half 
        mergeSort(R) # Sorting the second half 
  
        i = j = k = 0
          
        # Copy data to temp arrays L[] and R[] 
        while i &lt; len(L) and j &lt; len(R): 
            if L[i] &lt; R[j]: 
                arr[k] = L[i] 
                i+= 1
            else: 
                arr[k] = R[j] 
                j+= 1
            k+= 1
          
        # Checking if any element was left 
        while i &lt; len(L): 
            arr[k] = L[i] 
            i+= 1
            k+= 1
          
        while j &lt; len(R): 
            arr[k] = R[j] 
            j+= 1
            k+= 1
  
# Code to print the list 
def printList(arr): 
    for i in range(len(arr)):         
        print(arr[i], end =&quot; &quot;) 
    print() 
  
# driver code to test the above code 
if __name__ == '__main__': 
    arr = [12, 11, 13, 5, 6, 7]  
    print (&quot;Given array is&quot;, end =&quot;\n&quot;)  
    printList(arr) 
    mergeSort(arr) 
    print(&quot;Sorted array is: &quot;, end =&quot;\n&quot;) 
    printList(arr)

4.25 (4 Votes)
0
3.67
3
IllusiveBrian 18110 points

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

#include &lt;stdio.h&gt;

// 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 &lt; 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 &lt;= q &amp;&amp; j &lt;= r)
    {
        if(a[i] &lt; a[j])
        {
            b[k++] = a[i++];    // same as b[k]=a[i]; k++; i++;
        }
        else
        {
            b[k++] = a[j++];
        }
    }
  
    while(i &lt;= q)
    {
        b[k++] = a[i++];
    }
  
    while(j &lt;= r)
    {
        b[k++] = a[j++];
    }
  
    for(i=r; i &gt;= 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 &lt; size; i++)
    {
        printf(&quot;%d &quot;, a[i]);
    }
    printf(&quot;\n&quot;);
}
 
int main()
{
    int arr[] = {32, 45, 67, 2, 7};
    int len = sizeof(arr)/sizeof(arr[0]);
 
    printf(&quot;Given array: \n&quot;);
    printArray(arr, len);
    
    // calling merge sort
    mergeSort(arr, 0, len - 1);
 
    printf(&quot;\nSorted array: \n&quot;);
    printArray(arr, len);
    return 0;
}

3.67 (3 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
where to use merge sort merge sort working merge sort uses which technique to implement sorting write a program for merge sort merge sort alogorithm how the merge sort algorithm works Write an algorithm for Merge Sort with an given example Merge sort: what is the use of merge sort assume that a merge sort algorithm merge sorting example merge sort method algorithm merge sort algorithm def def merge sort Design merge sort algorithm merge sort algorithms two way merge sort different ways to do merge sort 2 way merge sort algorithm merge and merge sort The Merge Sort algorithm is an example for: Merge sort algorithm online merge algorith merge sort merge sort algorihtm merge sort algorithm representation merge sort explainde mergesort algo merge sort algoritm explained merge sort uses which of the following techniques to implement sorting Merge sort is in place sorting algorithm when do we use merge sort merge sort algrithm what is merge sort in algorithm merge sort simple algorithm Which of the following algorithm design technique is used in merge sort? implementing merge sort in-place sorting merge sort merge sort algotrithm merge sort algorithm works merge Sort is in place merge sort programiz which of the following algorithm design technique is used in merge sort merge sort algorithm example step by step algorithms merge sort algoritmo Merge sort merge sort algorithm is in place? program to implement merge sort merge sorty merge sort example steps how does a merge sort algorithm work how does a mergey sort algorithm work algorithm for merge sort steps of merge sort algorithm merge sort merging how does a merge sort work merge sort algorithm coding explanation merge sort in place algorithm is merge sort in place algorithm merge sort algorithm elplemention who invented merge sort merge sortz merge sort algorithm step by step where we use merge sort Which of the following algorithm design techniques is used in merge sort? merge algorithm in the merge sort algorithm simple merge sort easy Merge Sort explanation o(1) algorithm merge sort what is merge' sort merge sort for sorted arrays merge sort algorithm explained algorithm merge sort merging algorithm in mergesort merge sort uses which of the following algorithm to implement sorting Write a program to implement Merge Sort . Merge sort uses which of the following method to implement sorting? * working merge sort algo examp,e merge sort algrotj,s merge sort algorithms merge sort algorythm Merge sort uses which of the following method to implement sorting? use of merge sort merge sorting algorithm merge sort matching algorithm what is the basic operation in merge sort merge sort tutorial mergesort algorithm explained how to perform a merge sort merge sort algorithm jenny HOw merge sort works? how to merge sort merge sort algorithm purpose mergesort algorithmus merge in merge sort merge sort nedir how to sort in merge sort merge() algo in merge sort what is merge sort algorithm Which of the following sorting algorithm makes use of merge sort? how merge sort works merge sort works best for in merge sort, you create explain merge sort step by step algorithm merge sort algorithms explained program for merge sort Merge Sort algorithm. how to indicate order in merge sort merge sort in place merge sort. merge sorting algorithm basic operations merge sort for algoritmi merge sort what is a merge sort how created the merge sort algorithm How to write a program to implement merge sort? explain merge sort algorithm merge sort baeldung merge sort step by step algorithm merge sort wiki how to implement merge sort Which best describes a merge sort algorithm? merge sort uses merge sort steps two way merge sort algorithm is merge Sort the best sorting algorithm merge sort use which of the following technique to implement sorting Discuss merge sort algorithm with an example. merge sort explanation which algorithm technique merge sort uses approach to do merge sort is merge sort an in-place algorithm merge sort algorithm in place merge sorte merge sort defination merge sort algorithm programiz how does merge sort work merge sort demo what is merge sort used for merge sort simple algo merge sort simple alg Merge sort uses which of the following technique to implement sorting? implement a merge sort merge sort algorithm poudo merge sort algoritmo Is the merge sort an in-place algorithm? Merge sort uses which of the following technique to implement sorting? * explain merge sort in-place sorting algorithm merge sort Write algorithm for Merge sort method. Explain the working of Merge Sort? merge in merge sort explained Merge sort uses which of the following algorithm to implement sorting? merge sort explained merge sort implement best merge sort algorithm merge sort example merge sort ascending order merge sort algotithm algorithms mergesort merge sort \ algoritma merge sort merge sort a algorithm of merge sort what is merge sort? purpose of merge sort Merge sort uses which of the following technique to implement sorting what does a merge sort do merge sort algorthm merge sort algorithm, implement merge sort merge sort implementation merge sort com is merge sort an in place algorithm how merge sort algorithm works how to identify merge sort algorithm works sorting programiz merge sort algorithm 9. Running merge sort on an array of size n which is already sorted is * insertion sort algorithm merge sort time complexity how long does the merge sort algorithm runtime how long does the merge sort algorithm's runtime merge sort site:rosettacode.org is merge sort a in place algorithm mergesort \ merge sort method in java merge sort algorithm in java recurrence relation of merges ort merge sort complexity analysis merge sort merge sort sort complexity merge sort code example merge sort algoithm complexity of 3 way merge sort 3way merge sort merge sort algorithm in CLR book explanation merge sort complexity merge sort in c++ with proper format merge sort in an array gfg merge sort selection insertion merge sort c++ merge sort algorithm steps Write the Merge sort algorithm steps. merge sprt mergesort gfg how to merge sorted Merge Sort program merge sort algo gfg Explain the concept of Merge Sort on the following data to sort the list: 27,72, 63, 42, 36, 18, 29. What is the best case and worst case time complexity of Merge Sort algorithm? sort the array using merge sort merge sort function c merge search merge sort geeks for geeks Let P be a mergesort program to sort numbers in ascendinng order on a unknown data structure which take O(n^2) time to find the mid element, rest property is unknown. Then recurrence relation for the same is ? merge sort is also called as big o notation merge sort Merge sort order pseudocode merge sort merger sort c merge sort also merge sort nlogn mergesort complexity merge sort quora merge sort of array java merge sort algo Merge sort uses an algorithmic technique known as merge sort applicable for repeating elements merge and sort algorithm a recursive function that sorts a sequence of numbers in ascending order using the merge function .c++ use merge sort to sort in array with n elements what is the worst case time required for the Fort shaker sort c geeks Write an algorithm for merge sort and compute its complexity. algo for merge sort merge algorithm Merge Sort divides the list in recursive merge sort c merge sort algorythem write an algorithm for merge sort merge sort solve merge sort big o Write C functions to sort a set of integers in descending order using top-down approach of merge sort For the merge sort algorithm discussed in class, if the following change is made, the worst-case runtime would be merge sort in greek what is the time complexity of traversing an array using merge sort method Briefly explain Merge sort technique merge sort function in algorithm.h merging in data structure top down merge c language average complexity of merge sort Merge Sort divides the list in i. Two equal parts ii. N equal parts iii. Two parts, may not be equal iv. N parts, may not be equal c++ merge sort array no of merges require to get largest block application of mergesort where gfg Explain Merge Sort with example examples of algorithms merge sort technique of merge sort mergesort python merge sort speduocode algo of merge sort sort merge algorithm merge sort i Describe the concept of Merge sort merge sort in ds merge sort in python c++ merge sort recursive. c++ merge sort. merge sort divide and conquer merge algorithm in merge sort marge sort in c contents of array before final merge sort procedure pseudocode for merge sort pseudo code for meerge sort merge sort explanation in c what is merge soer merge sort question merge and sort merge sort t(n) expression merge sort algorithm is merge sort in order merge sort implementation in c apply merge sort to sort the list In Merge Sort, what is the worst case complexity? average tc of merge sort merge procedure of merge sort merge function c merge sort in matrix merge while sorting two arrays recursively mergesort wiki merge-sort algorithm worst case complexity of merge sort what is the recursive call of the merge sort in data structure merge sort using recursion cpp best merge sort implementation merge method for merge sort merge sort in c++ program merge sort algorithm pseudocode algorithm of mergesort merge sort using divide and conquer in c merge sort for sorting numbers mergesort i, j sort array merg sort in c if a merge sortt is divided into 5 parts recurrnce time c++ code for merge sort Mergesort complexity in already ascending order Given an array of n elements, that is reverse sorted. The complexity of merge sort to sort it is merge sort for denominations Implement following Merge sort algorithm using recursion print passes of merging algorithm. formula for dividing size of merge sort mergesort implementation c++ full implementation merge sort c++ c++ merge sort code mearge sort c merge sort in c using recursion pointers total no. of operations in merge sort merge sort divide list into n/4 and3n/4 divide and conquer sorting algorithm code in c++ merge sort technique list steps that the merge sort algorithm would make in sorting the following values 4,1,3,2 7 way split merge sort merge sort recursive java merge sort complete example merge sort in merge sort pseduod merge sort by divide and conquer Sort an array A using Merge Sort. Change in the input array itself. So no need to return or print anything. merge sort recursion merge function merge sort time and space complexity space complexity of merge sort merge sort pseudocode merge sort recursion c++ mergesort recursion merge sort recursion merge sort faboconi sort an array using recursion time complexity using merge-sort egg merge sort gfg merge sorrt merge sort program in c mergesort merge function merge sort algorithm c implimentation geeks for geeks merge sort recursive geeks for geeks merge sort MergeSort() merge sort gfg solution merge sort geeksforgeeks C merge sort void* mergesort c merge sort theory mergesort algorithm technique used in merge sort c++ recursive merge sort merge sort c++ recursive merge function for merge sort implementation of merge sort algorithm in c++ A c code for merge sort Use tabbing with binary search in the merging process. merge sort examples c++ merge sort function merge sort in cpp code why do we call mid+1 in merge function merge sort implementation example mergesort function source code stl merge sort for array merge sort javascript How many passes will be needed to sort an array which contains 5 elements using Merge Sort pseudo code for merge sort program to sort an array using merge sort merge sort algorithm with example merge sort example with steps merge sort java recursion python merge sort recursive merge sort where we use the index and not the array merge sort code c++ Determine the appropriate sorting algorithm corresponding to the below function: Function (m,n) {If(m&lt;n) {Mid=(m+n)/2; Sort(m,middle); Sort(middle+1,n); Sort(m,middle,n);}} * code merge sort merge sort expressed mathematically Merger sort mergesort code merge sort code for c++ merge sort recursive c++ code merge sort in array merege functions merege sort To sort an array of Integers using Recursive Merge Sort Algorithm. merge sort code in c++ merge sort in cpp code for merge sort in c merge sort implementation java java merge sort simple merge sort implementation in c++ merje sort code in c++ merge sort java recursive code function mergeSort(nums) { // Write merge sort code here. merge sort un c++ merge sort python complexity merge function in merge sort code for merge sort erge sort code merge sort cpp merge sort c++ program merge sort algorithm example merge sort python c++ merge sort merge sort and sort in c++ merge sort function in c++ mergesort function in c++ merge sorted array sorting string merge sort merge sort java merge sort in java MERGE sort TO SORT ARRAY merging sorting mergesort in c merged sort merge soring c++ merge sorting merge sorr merge sort source code how does the mergesort work what is merge sort merge sort more efficent way merge sort using auxiliary merge sort c++ geeksforgeeks merge sort recursive merge sort cpp merge sort definition merge sort array algo mergesort implementation java merge sort code merge sort algorithm geekforgeeks? merge sort algorithm? mergesort cpp code merge sort psuedocode merge sort c merge sort algo merge sort program in c++ merge sort in c merge sort algorithm merge sort code in c merge sort in c++ mergesort merge sort ascending c++ merge sort
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source