Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

merge sort javascript

let array = [70, 50, 30, 10, 20, 40, 60]

function mergeSort(array) {
    let l = 0
    let r = array.length
    let m = Math.round((r - l) / 2)

    if (r === 1) {
        return   // returns recursively
    }

    let L = []   // left half of current array
    let R = []   // right half of current array

    for (let i = l; i < m; i++) {
        L.push(array[i])
    }
    for (let j = m; j < r; j++) {
        R.push(array[j])
    }

    mergeSort(L)
    mergeSort(R)

    let i = 0, j = 0, k = 0

    // Merging part
    while (i < L.length && j < R.length) {
        if (L[i] < R[j]) {
            array[k] = L[i]
            i++
        } else {
            array[k] = R[j]
            j++
        }
      
        k++
    }

    while (i < L.length) {
        array[k] = L[i]
        i++
        k++
    }

    while (j < R.length) {
        array[k] = R[j]
        j++
        k++
    }
}

mergeSort(array)
console.log(array)	 // [Log]: [10, 20, 30, 40, 50, 60, 70]

// Time complexity of merge sort is O(NlogN)
// Space complexity is N, because we use an auxiliary array to sort all elements
Comment

merge sort function in JavaScript

function mergeSort(arr) {
  if (arr.length < 2) return arr;
  if (arr.length > 1) {
    const mid = Math.floor(arr.length / 2);
    let left = arr.slice(0, mid);
    let right = arr.slice(mid, arr.length);
    return Merge(mergeSort(left), mergeSort(right));
  }
}

function Merge(left, right) {
  let i = 0;
  let j = 0;
  let k = 0;
  let arr = [];
  while (i < left.length && j < right.length) {
    if (left[i] <= right[j]) {
      arr[k] = left[i];
      i++;
    } else {
      arr[k] = right[j];
      j++;
    }
    k++;
  }
  while (i < left.length) {
    arr[k] = left[i];
    i++;
    k++;
  }
  while (j < right.length) {
    arr[k] = right[j];
    j++;
    k++;
  }
  return arr;
}
let arr = [8, 4, 20, 45, 1 ,29]
console.log(mergeSort(arr));
Comment

merge-sort js

// Implementation of the merge sort algorithm.
// Let n be size of list to sort.
// Time complexity: O(n log2(n))
// Space complexity: O(n)

function mergeSort(array) {
  if (array.length <= 1) return array;
  mergeSortHelper(array, 0, array.length - 1);
  return array;
}

// Sort the array recursively
function mergeSortHelper(array, leftIdx, rightIdx) {
  // Base case: no more recursive calls needed
  if (rightIdx <= leftIdx) return;
  const middleIdx = Math.floor(leftIdx + (rightIdx - leftIdx) / 2);
  // Recursive step for sorting half of current array
  mergeSortHelper(array, leftIdx, middleIdx);
  mergeSortHelper(array, middleIdx + 1, rightIdx);
  // Merge recursively sorted subarrays
  merge(array, leftIdx, middleIdx, middleIdx + 1, rightIdx);
}

// Merge sorted subarrays
function merge(array, left, leftEnd, right, rightEnd) {
  const sizeSortedArray = rightEnd - left + 1;
  const sortedArray = [];
  let leftIdx = left,
    rightIdx = right;
  let sortedArrayIdx = 0;
  // Always get smaller value from sorted subarrays
  while (leftIdx <= leftEnd && rightIdx <= rightEnd) {
    if (array[leftIdx] < array[rightIdx]) {
      sortedArray[sortedArrayIdx++] = array[leftIdx++];
    } else {
      sortedArray[sortedArrayIdx++] = array[rightIdx++];
    }
  }
  // Get remaining elements if any
  while (leftIdx <= leftEnd) {
    sortedArray[sortedArrayIdx++] = array[leftIdx++];
  }
  // Get remaining elements if any
  while (rightIdx <= rightEnd) {
    sortedArray[sortedArrayIdx++] = array[rightIdx++];
  }
  // Copy sorted version of array back to original array
  arrayCopy(sortedArray, 0, array, left, sizeSortedArray);
}

function arrayCopy(srcArray, srcIndex, destArray, destIndex, length) {
  destArray.splice(
    destIndex,
    length,
    ...srcArray.slice(srcIndex, srcIndex + length)
  );
}
Comment

Merge Sort Algorithm in javascript

function mergeSortedAlgo(array) {
    if(array.length <= 1) return array;

    let midPoint = Math.floor(array.length / 2);
    let leftArray = mergeSortedAlgo(array.slice(0, midPoint));
    let rightArray = mergeSortedAlgo(array.slice(midPoint));

    return mergeSortedArray(leftArray, rightArray);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: nextjs open browser automatically 
Javascript :: EACCES: permission denied 
Javascript :: javascript toggle button 
Javascript :: javascript duplicate an array 
Javascript :: textarea javascript set value 
Javascript :: linux cli format json 
Javascript :: Javascript Show HTML Elements 
Javascript :: debounce 
Javascript :: replace last element of array javascript 
Javascript :: javascript parse date dd/mm/yyyy hh:mm:ss 
Javascript :: how to convert string to camel case in javascript 
Javascript :: javascript href on load delay 
Javascript :: http get response body node js 
Javascript :: javascript insert html before element 
Javascript :: check object is empty javascript 
Javascript :: install bootstrap in react 
Javascript :: install react bootstrap 
Javascript :: link reload page react 
Javascript :: how to create a cookie in javascript 
Javascript :: javascript get element by id and class 
Javascript :: print whole array javascript 
Javascript :: js canvas draw image 
Javascript :: js loop away backward 
Javascript :: react does not send the cookie automatically 
Javascript :: javascript min max array 
Javascript :: react router 6 multiple routes layout 
Javascript :: javascript image to blob 
Javascript :: secure random nodejs 
Javascript :: mdn clonenode 
Javascript :: document.queryselector null check 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =