25/04/2026 02:47am

JS2GO EP.35 Implementing Sorting Algorithms in JavaScript and Go
#JS2GO
#Go
#JavaScript
#Algorithm
#Sorting Algorithms
#Quick Sort
#Merge Sort
#Bubble Sort
#Sorting
Sorting is one of the most fundamental concepts in programming Whether it’s searching, ranking, or data processing every system relies on efficient sorting algorithms to organize data in a meaningful way.
In this article, we’ll explore and compare some of the most popular sorting algorithms: Bubble Sort, Merge Sort, and Quick Sort, along with how to use Go’s built-in sort package. You’ll see real code examples in both JavaScript and Go, and learn which language and approach provide the best performance for your project. 🔍
1. Bubble Sort
Concept: The simplest sorting algorithm that works by repeatedly comparing adjacent elements and swapping them until the entire list is sorted.
🔹 JavaScript: Bubble Sort
function bubbleSort(arr) {
let n = arr.length;
for (let i = 0; i < n - 1; i++) {
for (let j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
}
console.log(bubbleSort([5, 3, 8, 4, 2]));
Output:
[2, 3, 4, 5, 8]
🔹 Go: Bubble Sort
package main
import "fmt"
func bubbleSort(arr []int) []int {
n := len(arr)
for i := 0; i < n-1; i++ {
for j := 0; j < n-i-1; j++ {
if arr[j] > arr[j+1] {
arr[j], arr[j+1] = arr[j+1], arr[j]
}
}
}
return arr
}
func main() {
arr := []int{5, 3, 8, 4, 2}
fmt.Println(bubbleSort(arr))
}
Output:
[2 3 4 5 8]
Summary: Bubble Sort is great for learning the basics of sorting but not suitable for real-world use due to its O(n²) time complexity, making it inefficient for large datasets.
2. Merge Sort
Concept: A “Divide and Conquer” algorithm that splits data into smaller parts, sorts each part recursively, and merges them back together in order.
🔹 JavaScript: Merge Sort
function mergeSort(arr) {
if (arr.length <= 1) return arr;
const mid = Math.floor(arr.length / 2);
const left = mergeSort(arr.slice(0, mid));
const right = mergeSort(arr.slice(mid));
return merge(left, right);
}
function merge(left, right) {
let result = [], i = 0, j = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j]) result.push(left[i++]);
else result.push(right[j++]);
}
return result.concat(left.slice(i)).concat(right.slice(j));
}
console.log(mergeSort([5, 3, 8, 4, 2]));
🔹 Go: Merge Sort
package main
import "fmt"
func mergeSort(arr []int) []int {
if len(arr) <= 1 {
return arr
}
mid := len(arr) / 2
left := mergeSort(arr[:mid])
right := mergeSort(arr[mid:])
return merge(left, right)
}
func merge(left, right []int) []int {
result := []int{}
i, j := 0, 0
for i < len(left) && j < len(right) {
if left[i] < right[j] {
result = append(result, left[i])
i++
} else {
result = append(result, right[j])
j++
}
}
result = append(result, left[i:]...)
result = append(result, right[j:]...)
return result
}
func main() {
arr := []int{5, 3, 8, 4, 2}
fmt.Println(mergeSort(arr))
}
Summary: Merge Sort has a time complexity of O(n log n) and performs efficiently with large datasets. However, it requires additional memory to hold subarrays during the merge process.
3. Quick Sort
Concept: Another “Divide and Conquer” algorithm that selects a pivot element and partitions the array into two subarrays values less than the pivot and values greater than the pivot then recursively sorts each part.
🔹 JavaScript: Quick Sort
function quickSort(arr) {
if (arr.length <= 1) return arr;
const pivot = arr[arr.length - 1];
const left = arr.filter(x => x < pivot);
const right = arr.filter(x => x > pivot);
return [...quickSort(left), pivot, ...quickSort(right)];
}
console.log(quickSort([5, 3, 8, 4, 2]));
🔹 Go: Quick Sort
package main
import "fmt"
func quickSort(arr []int) []int {
if len(arr) <= 1 {
return arr
}
pivot := arr[len(arr)-1]
left, right := []int{}, []int{}
for _, v := range arr[:len(arr)-1] {
if v < pivot {
left = append(left, v)
} else {
right = append(right, v)
}
}
left = quickSort(left)
right = quickSort(right)
return append(append(left, pivot), right...)
}
func main() {
arr := []int{5, 3, 8, 4, 2}
fmt.Println(quickSort(arr))
}
Summary:
Quick Sort typically performs faster than Merge Sort in average cases (O(n log n)).
However, in the worst case (e.g., when the pivot selection is poor), it can degrade to O(n²).
4. Using the sort Package in Go
Go provides a built-in sort package that allows you to sort data efficiently without writing your own algorithm.
package main
import (
"fmt"
"sort"
)
func main() {
nums := []int{5, 3, 8, 4, 2}
sort.Ints(nums)
fmt.Println(nums)
}
Output:
[2 3 4 5 8]
Advantages:
✅ Highly optimized (O(n log n))
✅ Easy to use for production-level systems
✅ Supports sort.Strings, sort.Slice, and sort.Float64s
5. Algorithm Comparison
| Algorithm | Average Complexity | Speed | Best For | Recommended Language |
|---|---|---|---|---|
| Bubble Sort | O(n²) | Slow | Teaching Basics | JS / Go |
| Merge Sort | O(n log n) | Fast | Large datasets | Both |
| Quick Sort | O(n log n) | Very Fast | General Use | Both |
| Go sort Package | O(n log n) | Excellent | Production systems | Go |
Summary and Recommendations
- 💡 JavaScript is ideal for experimenting with algorithms or client-side sorting in smaller datasets.
- ⚙️ Go is perfect for production-level systems that require high performance such as APIs, backend processing, or data pipelines.
- 🧠 Use Go’s
sortpackage for maximum efficiency and cleaner code. - 🚀 For JavaScript, Quick Sort or Merge Sort are the best options for handling large-scale data sorting.
Next Episode
In JS2GO EP.36, we’ll dive into Searching Algorithms in Go and JavaScript exploring techniques like Linear Search, Binary Search, and Map-based Lookup, along with how to choose the right one for your real-world system. 🔍
Read more
🔵 Facebook: Superdev Academy
🔴 YouTube: Superdev Academy
📸 Instagram: Superdev Academy
🎬 TikTok: https://www.tiktok.com/@superdevacademy?lang=th-TH
🌐 Website: https://www.superdevacademy.com/en