"merge sort algo" Code Answer's

You're definitely familiar with the best coding language Javascript that developers use to develop their projects and they get all their queries like "merge sort algo" answered properly. Developers are finding an appropriate answer about merge sort algo related to the Javascript coding language. By visiting this online portal developers get answers concerning Javascript codes question like merge sort algo. Enter your desired code related query in the search bar and get every piece of information about Javascript code related question on merge sort algo. 

merge sort

By adriancmirandaadriancmiranda on May 31, 2020
// @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]);

Add Comment

2

merge sort algo

By Innocent IguanaInnocent Iguana on Jul 09, 2020
def mergeSort(arr): 
    if len(arr) >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 < len(L) and j < len(R): 
            if L[i] < 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 < len(L): 
            arr[k] = L[i] 
            i+= 1
            k+= 1
          
        while j < 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 =" ") 
    print() 
  
# driver code to test the above code 
if __name__ == '__main__': 
    arr = [12, 11, 13, 5, 6, 7]  
    print ("Given array is", end ="\n")  
    printList(arr) 
    mergeSort(arr) 
    print("Sorted array is: ", end ="\n") 
    printList(arr)

Add Comment

0

All those coders who are working on the Javascript based application and are stuck on merge sort algo can get a collection of related answers to their query. Programmers need to enter their query on merge sort algo related to Javascript code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about merge sort algo for the programmers working on Javascript code while coding their module. Coders are also allowed to rectify already present answers of merge sort algo while working on the Javascript language code. Developers can add up suggestions if they deem fit any other answer relating to "merge sort algo". Visit this developer's friendly online web community, CodeProZone, and get your queries like merge sort algo resolved professionally and stay updated to the latest Javascript updates. 

Javascript answers related to "merge sort algo"

View All Javascript queries

Javascript queries related to "merge sort algo"

Browse Other Code Languages

CodeProZone