# Merge sort vs quick sort in Javascript

In this article, we are going to discuss about the differences between merge sort and quick sort in JavaScript with appropriate examples.

Merge sort and Quick sort are used to sort the elements, but the approach is different. Both Merge sort and quick sort are based on Divide and Conquer Strategy.

Merge sort

It is a stable sorting algorithm. In the merge sort, it follows a recursive approach that repeatedly splits the array into half until no more division is possible i.e., the array either remains empty or has a single element. Then by comparing the two small array elements we sort them and merge it by making a large array. Worst case Time complexity for Merge sort – O(nlogn).

Quick sort

In quick sort, it picks an element as pivot. The pivot element can be the first element or last element or median element or any random element. After selecting the pivot element, it partitions the array around the selected pivot. Worst case Time complexity for Quick sort – O(n^2).

Quick sort is Better than Merge sort in terms of Auxiliary space, Locality of reference. Merge sort uses extra space for sorting. Quick sort is better than merge sort in terms of extra space, where it requires only little extra space. When we have larger data structures, then merge sort is better than quick sort.

### Example 1

This is an example program to implement Merge sort.

<!DOCTYPE html>
<html>
<title>Merge sort in JavaScript</title>
<body style="text-align : center">
<h3>MergeSort in JavaScript</h3>
<p id='output'></p>
<script>
function merge(arr, l, m, r) {
var size1 = m - l + 1;
var size2 = r - m;
// Create two temporary arrays i.e. LeftSub and RightSub Arrays.
var LeftSub = new Array(size1);
var RightSub = new Array(size2);
// Copy data from arr to LeftSub and RightSub Arrays respectively.
for (var i = 0; i < size1; i++)
LeftSub[i] = arr[l + i];
for (var j = 0; j < size2; j++)
RightSub[j] = arr[m + 1 + j];
var i = 0,
j = 0,
k = l;
// Merge the temporary arrays back into arr[l..r]
while (i < size1 && j < size2) {
if (LeftSub[i] <= RightSub[j]) {
arr[k] = LeftSub[i];
i++;
} else {
arr[k] = RightSub[j];
j++;
}
k++;
}
// Copy the remaining elements of LeftSub into arr
while (i < size1) {
arr[k] = LeftSub[i];
i++;
k++;
}
// Copy the remaining elements of RightSub into arr
while (j < size2) {
arr[k] = RightSub[j];
j++;
k++;
}
}
function mergeSort(arr, left, right) {
if (left >= right) {
return;
}
var mid = left + parseInt((right - left) / 2);
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
function displayArray(A) {
A.forEach(ele => {
document.getElementById('output').innerHTML += ele + " ";
});
document.getElementById('output').innerHTML += '<br/>';
}
var arr = [10,7,27,5,2,9];
var size = arr.length;
document.getElementById('output').innerHTML += 'Before sorting : ';
displayArray(arr);
mergeSort(arr, 0, size - 1);
document.getElementById('output').innerHTML += '<br/>' + 'After sorting : ';
displayArray(arr);
</script>
</body>
</html>


On executing the above code, the following output is generated.

### Example 2

This is an example program to implement Quick sort.

<!DOCTYPE html>
<html>
<title>Quick sort in JavaScript</title>
<body style="text-align : center">
<h3>quickSort in JavaScript</h3>
<p id='output'></p>
<script>
/* Swap method uses temp variable to swap the elements */
function swap(arr, a, b) {
let temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
/* The method partition takes last element in the array as a pivot element, places elements which are lesser than pivot to the left of pivot in the array and places elements which are greater than pivot to the right of pivot in the array */
function partition(arr, low, high) {
let pivot = arr[high];
let i = (low - 1);
for (let j = low; j <= high - 1; j++) {
// If element is less than pivot
if (arr[j] < pivot) {
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return (i + 1);
}
function quickSort(arr, low, high) {
if (low < high) {
let pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
function displayArray(A) {
A.forEach(ele => {
document.getElementById('output').innerHTML += ele + " ";
});
document.getElementById('output').innerHTML += '<br/>';
}
var arr = [25,9,1,16,4,49,36];
var size = arr.length;
document.getElementById('output').innerHTML += 'Before sorting : ';
displayArray(arr);
quickSort(arr, 0, size - 1);
document.getElementById('output').innerHTML += '<br/>' + 'After sorting : ';
displayArray(arr);
</script>
</body>
</html>


On executing the above code, the following output is generated.