---
id: 69b279bae0f20eda63163d03
title: Step 25
challengeType: 1
dashedName: step-25
---

# --description--

Now that `numbers` is sorted, log `'Sorted array: '` to the console followed by the `numbers` array.

With that, the merge sort algorithm is complete.

# --hints--

You should log `'Sorted array: '` to the console.

```js
assert.match(
  __helpers.removeJSComments(code),
  /console\.log\s*\(\s*['"]Sorted array:\s*['"]\s*\)\s*;?/
);
```

You should log the `numbers` array to the console after logging `'Sorted array: '`.

```js
assert.match(
  __helpers.removeJSComments(code),
  /mergeSort\s*\(\s*numbers\s*\)\s*;?\s*console\.log\s*\(\s*['"]Sorted array:\s*['"]\s*\)\s*;?\s*console\.log\s*\(\s*numbers\s*\)\s*;?/
);
```

# --seed--

## --seed-contents--

```js
function mergeSort(array) {
  if (array.length <= 1) {
    return;
  }

  const middlePoint = Math.floor(array.length / 2);
  const leftPart = array.slice(0, middlePoint);
  const rightPart = array.slice(middlePoint);

  mergeSort(leftPart);
  mergeSort(rightPart);

  let leftArrayIndex = 0;
  let rightArrayIndex = 0;
  let sortedIndex = 0;

  while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
    if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
      array[sortedIndex] = leftPart[leftArrayIndex];
      leftArrayIndex += 1;
    } else {
      array[sortedIndex] = rightPart[rightArrayIndex];
      rightArrayIndex += 1;
    }
    sortedIndex += 1;
  }

  while (leftArrayIndex < leftPart.length) {
    array[sortedIndex] = leftPart[leftArrayIndex];
    leftArrayIndex += 1;
    sortedIndex += 1;
  }
  
  while (rightArrayIndex < rightPart.length) {
    array[sortedIndex] = rightPart[rightArrayIndex];
    rightArrayIndex += 1;
    sortedIndex += 1;
  }

}
const numbers = [4, 10, 6, 14, 2, 1, 8, 5];
console.log('Unsorted array: ');
console.log(numbers);
mergeSort(numbers);
--fcc-editable-region--

--fcc-editable-region--
```

# --solutions--

```js
function mergeSort(array) {
  if (array.length <= 1) {
    return;
  }

  const middlePoint = Math.floor(array.length / 2);
  const leftPart = array.slice(0, middlePoint);
  const rightPart = array.slice(middlePoint);

  mergeSort(leftPart);
  mergeSort(rightPart);

  let leftArrayIndex = 0;
  let rightArrayIndex = 0;
  let sortedIndex = 0;

  while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
    if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
      array[sortedIndex] = leftPart[leftArrayIndex];
      leftArrayIndex += 1;
    } else {
      array[sortedIndex] = rightPart[rightArrayIndex];
      rightArrayIndex += 1;
    }
    sortedIndex += 1;
  }

  while (leftArrayIndex < leftPart.length) {
    array[sortedIndex] = leftPart[leftArrayIndex];
    leftArrayIndex += 1;
    sortedIndex += 1;
  }

  while (rightArrayIndex < rightPart.length) {
    array[sortedIndex] = rightPart[rightArrayIndex];
    rightArrayIndex += 1;
    sortedIndex += 1;
  }
}

const numbers = [4, 10, 6, 14, 2, 1, 8, 5];
console.log('Unsorted array:');
console.log(numbers);
mergeSort(numbers);
console.log('Sorted array:');
console.log(numbers);
```
