“Fusionar sort javascript” Código de respuesta

fusionar 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
Mysterious Macaque

Fusionar sort javascript

// 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)
  );
}
Wissam

TypeScript / JavaScript Fusion Matricas ordenadas

function MergeSorted(arr1: number[], arr2: number[])
: number[]{
    let x: number = 0;
    let y: number = 0;

    let Arr1Index: number = arr1[x];
    let Arr2Index: number = arr2[y];

    let MergedArray: number[] = [];

    while(Arr1Index || Arr2Index){
        if(!Arr2Index || (Arr1Index < Arr2Index)){
            MergedArray.push(Arr1Index);
            Arr1Index = arr1[x];
            x++;
        }else{
            MergedArray.push(Arr2Index);
            Arr2Index = arr2[y];
            y++;
        }
    }

    return MergedArray;
}
Tired Turtle

Respuestas similares a “Fusionar sort javascript”

Preguntas similares a “Fusionar sort javascript”

Más respuestas relacionadas con “Fusionar sort javascript” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código