All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is MergeSort? When and by whom was it created? How does MergeSort work?

This content has been automatically translated from Ukrainian.
MergeSort is an efficient sorting algorithm that uses the "divide and conquer" principle. It is based on the idea of splitting the input array into smaller parts, sorting each part separately, and then merging them into a sorted array.
The main idea of MergeSort is to split the original array in half until the base case is reached, where there is only one or no element left in the array. Then the merging process begins by combining two sorted parts to create a new sorted array. This process is repeated recursively until all parts are fully merged into one sorted array.
One of the main advantages of MergeSort is that it always operates with a time complexity of O(n log n), where n is the number of elements in the input array. This makes it efficient for sorting large arrays of data.

When and by whom was MergeSort created?

MergeSort was developed by John von Neumann in 1945. However, its publication occurred later, in 1948, when his colleague Hermann Goldstine described von Neumann's work during a presentation at a meeting of the American Mathematical Society. Thus, the MergeSort algorithm began to gain popularity and became an important part of modern algorithmic knowledge and computer science.
Von Neumann was a prominent mathematician, physicist, computer scientist, and electrical engineer. He made significant contributions to the development of computation theory, computer architecture, and numerical methods. MergeSort is one of the sorting algorithms he developed and has become the basis for numerous variants and optimizations that are still used today.

Example of the MergeSort algorithm (JavaScript).

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

function merge(left, right) {
  const mergedArr = [];
  let i = 0;
  let j = 0;
  
  while (i < left.length && j < right.length) {
    if (left[i] <= right[j]) {
      mergedArr.push(left[i]);
      i++;
    } else {
      mergedArr.push(right[j]);
      j++;
    }
  }
  
  while (i < left.length) {
    mergedArr.push(left[i]);
    i++;
  }
  
  while (j < right.length) {
    mergedArr.push(right[j]);
    j++;
  }
  
  return mergedArr;
}

// Example usage
const arr = [8, 5, 2, 9, 1, 7, 6, 3];
const sortedArr = mergeSort(arr);
console.log(sortedArr);
Result:
[1, 2, 3, 5, 6, 7, 8, 9]
The mergeSort() function recursively splits the input array in half, sorts each half by calling mergeSort(), and then merges them using the merge() function. The merge() function merges two sorted arrays into one sorted array by comparing elements from both arrays.

Example of the MergeSort algorithm (Ruby).

def merge_sort(arr)
  return arr if arr.length <= 1

  mid = arr.length / 2
  left = arr[0...mid]
  right = arr[mid..-1]

  merge(merge_sort(left), merge_sort(right))
end

def merge(left, right)
  merged_arr = []
  i = 0
  j = 0

  while i < left.length && j < right.length
    if left[i] <= right[j]
      merged_arr << left[i]
      i += 1
    else
      merged_arr << right[j]
      j += 1
    end
  end

  while i < left.length
    merged_arr << left[i]
    i += 1
  end

  while j < right.length
    merged_arr << right[j]
    j += 1
  end

  merged_arr
end

# Example usage
arr = [8, 5, 2, 9, 1, 7, 6, 3]
sorted_arr = merge_sort(arr)
puts sorted_arr
Result:
[1, 2, 3, 5, 6, 7, 8, 9]
The merge_sort() function recursively splits the input array in half, sorts each half by calling merge_sort(), and then merges them using the merge() function. The merge() function merges two sorted arrays into one sorted array by comparing elements from both arrays.

This post doesn't have any additions from the author yet.

26 Jun 03:52

What is PCIe (Peripheral Component Interconnect Express)?

meme code
meme code@memecode
26 Jun 05:44

What is Ubuntu Pro? What is the difference between Ubuntu and Ubuntu Pro?

meme code
meme code@memecode
26 Jun 05:44

Final stage of installing Ubuntu. Post-installation configuration of Ubuntu.

meme code
meme code@memecode
29 Jun 13:20

What are ASC and DESC? What is the difference? Examples of use in SQL, JavaScript, and Ruby.

meme code
meme code@memecode
30 Jun 10:07

What is QuickSort? When and by whom was it created? How does QuickSort work?

meme code
meme code@memecode
30 Jun 10:50

What is Recursion? An example of recursion in real life. The consequences of infinite recursion.

meme code
meme code@memecode
30 Jun 12:15

What is the difference between QuickSort and MergeSort?

meme code
meme code@memecode
03 Jul 05:03

What is MS-DOS? When and by whom was MS-DOS created?

meme code
meme code@memecode
03 Jul 06:45

What is an API (Application Programming Interface)?

meme code
meme code@memecode
10 Jul 05:43

What is a distribution?

meme code
meme code@memecode
24 Jul 11:02

What is apt-get in Ubuntu?

meme code
meme code@memecode
24 Jul 11:25

How to install Steam on Ubuntu? Installing Steam via the terminal.

meme code
meme code@memecode