Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Merge sort vs quick sort in Javascript
Merge Sort and Quick Sort are two popular divide-and-conquer sorting algorithms in JavaScript. While both are efficient, they differ in their approach, stability, and performance characteristics.
Merge Sort Overview
Merge Sort is a stable sorting algorithm that recursively divides the array into halves until each sub-array contains a single element, then merges them back in sorted order. It guarantees O(n log n) time complexity in all cases but requires additional space for merging.
Quick Sort Overview
Quick Sort selects a pivot element and partitions the array around it, placing smaller elements to the left and larger elements to the right. It then recursively sorts the sub-arrays. While it has an average time complexity of O(n log n), the worst case is O(n²).
Merge Sort Implementation
<!DOCTYPE html>
<html>
<head>
<title>Merge Sort in JavaScript</title>
</head>
<body>
<h3>Merge Sort Example</h3>
<p id="mergeOutput"></p>
<script>
function merge(arr, left, mid, right) {
const leftSize = mid - left + 1;
const rightSize = right - mid;
// Create temporary arrays
const leftArr = new Array(leftSize);
const rightArr = new Array(rightSize);
// Copy data to temporary arrays
for (let i = 0; i < leftSize; i++) {
leftArr[i] = arr[left + i];
}
for (let j = 0; j < rightSize; j++) {
rightArr[j] = arr[mid + 1 + j];
}
// Merge the temporary arrays back
let i = 0, j = 0, k = left;
while (i < leftSize && j < rightSize) {
if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
}
k++;
}
// Copy remaining elements
while (i < leftSize) {
arr[k] = leftArr[i];
i++;
k++;
}
while (j < rightSize) {
arr[k] = rightArr[j];
j++;
k++;
}
}
function mergeSort(arr, left, right) {
if (left < right) {
const mid = Math.floor((left + right) / 2);
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
// Test merge sort
const mergeArr = [64, 34, 25, 12, 22, 11, 90];
document.getElementById('mergeOutput').innerHTML =
'Original array: ' + mergeArr.join(', ') + '<br>';
mergeSort(mergeArr, 0, mergeArr.length - 1);
document.getElementById('mergeOutput').innerHTML +=
'Sorted array: ' + mergeArr.join(', ');
</script>
</body>
</html>
Original array: 64, 34, 25, 12, 22, 11, 90 Sorted array: 11, 12, 22, 25, 34, 64, 90
Quick Sort Implementation
<!DOCTYPE html>
<html>
<head>
<title>Quick Sort in JavaScript</title>
</head>
<body>
<h3>Quick Sort Example</h3>
<p id="quickOutput"></p>
<script>
function swap(arr, i, j) {
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
function partition(arr, low, high) {
const pivot = arr[high]; // Choose last element as pivot
let i = low - 1;
for (let j = low; j < high; j++) {
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) {
const pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, high);
}
}
// Test quick sort
const quickArr = [64, 34, 25, 12, 22, 11, 90];
document.getElementById('quickOutput').innerHTML =
'Original array: ' + quickArr.join(', ') + '<br>';
quickSort(quickArr, 0, quickArr.length - 1);
document.getElementById('quickOutput').innerHTML +=
'Sorted array: ' + quickArr.join(', ');
</script>
</body>
</html>
Original array: 64, 34, 25, 12, 22, 11, 90 Sorted array: 11, 12, 22, 25, 34, 64, 90
Comparison
| Aspect | Merge Sort | Quick Sort |
|---|---|---|
| Time Complexity (Best) | O(n log n) | O(n log n) |
| Time Complexity (Worst) | O(n log n) | O(n²) |
| Space Complexity | O(n) | O(log n) |
| Stability | Stable | Not stable |
| In-place Sorting | No | Yes |
When to Use Which
Use Merge Sort when:
- You need guaranteed O(n log n) performance
- Stability is required (maintaining relative order of equal elements)
- Working with linked lists
Use Quick Sort when:
- Memory usage is a concern (in-place sorting)
- Average case performance is more important than worst case
- Working with arrays where random access is efficient
Conclusion
Both algorithms are efficient divide-and-conquer approaches, but Merge Sort offers consistent performance and stability, while Quick Sort provides better space efficiency and typically faster average performance. Choose based on your specific requirements for stability, memory usage, and performance guarantees.
