[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-js2go-ep35-sorting-algorithms-javascript-go-all--*":3,"academy-blog-translations-y3iy7hx7duwf9hu":99},{"data":4,"page":98,"perPage":98,"totalItems":98,"totalPages":98},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":93,"keywords":94,"locale":69,"published_at":95,"scheduled_at":13,"school_blog":90,"short_description":96,"status":88,"title":6,"updated":97,"updated_by":13,"slug":91,"views":92},"JS2GO EP.35 Implementing Sorting Algorithms in JavaScript and Go","sclblg987654321","school_blog_translations","\u003Cp>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.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>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 \u003Ccode inline=\"\">sort\u003C\u002Fcode> 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. 🔍\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>1. Bubble Sort\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Concept: The simplest sorting algorithm that works by repeatedly comparing adjacent elements and swapping them until the entire list is sorted.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 JavaScript: Bubble Sort\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">function bubbleSort(arr) {\n  let n = arr.length;\n  for (let i = 0; i &lt; n - 1; i++) {\n    for (let j = 0; j &lt; n - i - 1; j++) {\n      if (arr[j] &gt; arr[j + 1]) {\n        [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];\n      }\n    }\n  }\n  return arr;\n}\n\nconsole.log(bubbleSort([5, 3, 8, 4, 2]));\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Output:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">[2, 3, 4, 5, 8]\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 Go: Bubble Sort\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport \"fmt\"\n\nfunc bubbleSort(arr []int) []int {\n\tn := len(arr)\n\tfor i := 0; i &lt; n-1; i++ {\n\t\tfor j := 0; j &lt; n-i-1; j++ {\n\t\t\tif arr[j] &gt; arr[j+1] {\n\t\t\t\tarr[j], arr[j+1] = arr[j+1], arr[j]\n\t\t\t}\n\t\t}\n\t}\n\treturn arr\n}\n\nfunc main() {\n\tarr := []int{5, 3, 8, 4, 2}\n\tfmt.Println(bubbleSort(arr))\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Output:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">[2 3 4 5 8]\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>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.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>2. Merge Sort\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Concept: A “Divide and Conquer” algorithm that splits data into smaller parts, sorts each part recursively, and merges them back together in order.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 JavaScript: Merge Sort\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">function mergeSort(arr) {\n  if (arr.length &lt;= 1) return arr;\n\n  const mid = Math.floor(arr.length \u002F 2);\n  const left = mergeSort(arr.slice(0, mid));\n  const right = mergeSort(arr.slice(mid));\n\n  return merge(left, right);\n}\n\nfunction merge(left, right) {\n  let result = [], i = 0, j = 0;\n  while (i &lt; left.length &amp;&amp; j &lt; right.length) {\n    if (left[i] &lt; right[j]) result.push(left[i++]);\n    else result.push(right[j++]);\n  }\n  return result.concat(left.slice(i)).concat(right.slice(j));\n}\n\nconsole.log(mergeSort([5, 3, 8, 4, 2]));\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 Go: Merge Sort\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport \"fmt\"\n\nfunc mergeSort(arr []int) []int {\n\tif len(arr) &lt;= 1 {\n\t\treturn arr\n\t}\n\tmid := len(arr) \u002F 2\n\tleft := mergeSort(arr[:mid])\n\tright := mergeSort(arr[mid:])\n\treturn merge(left, right)\n}\n\nfunc merge(left, right []int) []int {\n\tresult := []int{}\n\ti, j := 0, 0\n\tfor i &lt; len(left) &amp;&amp; j &lt; len(right) {\n\t\tif left[i] &lt; right[j] {\n\t\t\tresult = append(result, left[i])\n\t\t\ti++\n\t\t} else {\n\t\t\tresult = append(result, right[j])\n\t\t\tj++\n\t\t}\n\t}\n\tresult = append(result, left[i:]...)\n\tresult = append(result, right[j:]...)\n\treturn result\n}\n\nfunc main() {\n\tarr := []int{5, 3, 8, 4, 2}\n\tfmt.Println(mergeSort(arr))\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>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.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>3. Quick Sort\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>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.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 JavaScript: Quick Sort\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">function quickSort(arr) {\n  if (arr.length &lt;= 1) return arr;\n  const pivot = arr[arr.length - 1];\n  const left = arr.filter(x =&gt; x &lt; pivot);\n  const right = arr.filter(x =&gt; x &gt; pivot);\n  return [...quickSort(left), pivot, ...quickSort(right)];\n}\n\nconsole.log(quickSort([5, 3, 8, 4, 2]));\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 Go: Quick Sort\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport \"fmt\"\n\nfunc quickSort(arr []int) []int {\n\tif len(arr) &lt;= 1 {\n\t\treturn arr\n\t}\n\tpivot := arr[len(arr)-1]\n\tleft, right := []int{}, []int{}\n\tfor _, v := range arr[:len(arr)-1] {\n\t\tif v &lt; pivot {\n\t\t\tleft = append(left, v)\n\t\t} else {\n\t\t\tright = append(right, v)\n\t\t}\n\t}\n\tleft = quickSort(left)\n\tright = quickSort(right)\n\treturn append(append(left, pivot), right...)\n}\n\nfunc main() {\n\tarr := []int{5, 3, 8, 4, 2}\n\tfmt.Println(quickSort(arr))\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Summary:\u003Cbr>Quick Sort typically performs faster than Merge Sort in average cases (O(n log n)).\u003Cbr>However, in the worst case (e.g., when the pivot selection is poor), it can degrade to O(n²).\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>4. Using the \u003Ccode inline=\"\">sort\u003C\u002Fcode> Package in Go\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Go provides a built-in \u003Ccode inline=\"\">sort\u003C\u002Fcode> package that allows you to sort data efficiently without writing your own algorithm.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport (\n\t\"fmt\"\n\t\"sort\"\n)\n\nfunc main() {\n\tnums := []int{5, 3, 8, 4, 2}\n\tsort.Ints(nums)\n\tfmt.Println(nums)\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Output:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">[2 3 4 5 8]\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Advantages:\u003Cbr>✅ Highly optimized (O(n log n))\u003Cbr>✅ Easy to use for production-level systems\u003Cbr>✅ Supports \u003Ccode inline=\"\">sort.Strings\u003C\u002Fcode>, \u003Ccode inline=\"\">sort.Slice\u003C\u002Fcode>, and \u003Ccode inline=\"\">sort.Float64s\u003C\u002Fcode>\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>5. Algorithm Comparison\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cfigure class=\"table\">\u003Ctable>\u003Cthead>\u003Ctr>\u003Cth>Algorithm\u003C\u002Fth>\u003Cth>Average Complexity\u003C\u002Fth>\u003Cth>Speed\u003C\u002Fth>\u003Cth>Best For\u003C\u002Fth>\u003Cth>Recommended Language\u003C\u002Fth>\u003C\u002Ftr>\u003C\u002Fthead>\u003Ctbody>\u003Ctr>\u003Ctd>Bubble Sort\u003C\u002Ftd>\u003Ctd>O(n²)\u003C\u002Ftd>\u003Ctd>Slow\u003C\u002Ftd>\u003Ctd>Teaching Basics\u003C\u002Ftd>\u003Ctd>JS \u002F Go\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Merge Sort\u003C\u002Ftd>\u003Ctd>O(n log n)\u003C\u002Ftd>\u003Ctd>Fast\u003C\u002Ftd>\u003Ctd>Large datasets\u003C\u002Ftd>\u003Ctd>Both\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Quick Sort\u003C\u002Ftd>\u003Ctd>O(n log n)\u003C\u002Ftd>\u003Ctd>Very Fast\u003C\u002Ftd>\u003Ctd>General Use\u003C\u002Ftd>\u003Ctd>Both\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Go sort Package\u003C\u002Ftd>\u003Ctd>O(n log n)\u003C\u002Ftd>\u003Ctd>Excellent\u003C\u002Ftd>\u003Ctd>Production systems\u003C\u002Ftd>\u003Ctd>Go\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003C\u002Ffigure>\u003Cp>&nbsp;\u003C\u002Fp>\u003Chr>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>Summary and Recommendations\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cul>\u003Cli>💡 JavaScript is ideal for experimenting with algorithms or client-side sorting in smaller datasets.\u003C\u002Fli>\u003Cli>⚙️ Go is perfect for production-level systems that require high performance such as APIs, backend processing, or data pipelines.\u003C\u002Fli>\u003Cli>🧠 Use Go’s \u003Ccode inline=\"\">sort\u003C\u002Fcode> package for maximum efficiency and cleaner code.\u003C\u002Fli>\u003Cli>🚀 For JavaScript, Quick Sort or Merge Sort are the best options for handling large-scale data sorting.\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>Next Episode\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>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. 🔍\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp data-start=\"498\" data-end=\"834\">\u003Cstrong>Read more\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FGolang\">\u003Cstrong>Golang The Series\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FJS2GO\">\u003Cstrong>JS2GO\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fen\u002Fblogs\u002Fcategories\u002FTailwind%20CSS\">\u003Cstrong>10 Eps That Will Make You a Pro Tailwind CSS Overnight\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>\u003Cstrong>🔵 Facebook: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🔴 YouTube: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.youtube.com\u002Fchannel\u002FUC2eI2RxcA2zbTqZyHyWIfRg\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>📸 Instagram: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.instagram.com\u002Fsuperdevschool\u002F\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🎬 TikTok: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\">\u003Cstrong>https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🌐 Website: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdevacademy.com\u002Fen\">\u003Cstrong>https:\u002F\u002Fwww.superdevacademy.com\u002Fen\u003C\u002Fstrong>\u003C\u002Fa>\u003Cstrong>&nbsp;\u003C\u002Fstrong>\u003C\u002Fp>","70_11zon_mod563c9r4.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002F84ko7hwh4y673id\u002F70_11zon_mod563c9r4.webp","2026-03-04 08:45:40.498Z","",{"keywords":15,"locale":63,"school_blog":73},[16,23,28,33,38,43,48,53,58],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:45:30.765Z","nayuhpre9emd5xw","JS2GO","2026-04-10 16:12:59.160Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:20:42.484Z","vslzz9nvv6n77cx","JavaScript","2026-04-10 16:07:28.339Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:35:05.133Z","ydn3q6brn8wh2st","Algorithm","2026-04-10 16:12:36.773Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:35:00.761Z","rdb0iaragxhrgwk","Sorting Algorithms","2026-04-10 16:12:35.076Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:45:39.089Z","4p14lr9icx6fqm2","Quick Sort","2026-04-10 16:13:01.066Z",{"collectionId":17,"collectionName":18,"created":49,"created_by":13,"id":50,"name":51,"updated":52,"updated_by":13},"2026-03-04 08:45:39.321Z","jb1nbj4tkoyx89d","Merge Sort","2026-04-10 16:13:01.148Z",{"collectionId":17,"collectionName":18,"created":54,"created_by":13,"id":55,"name":56,"updated":57,"updated_by":13},"2026-03-04 08:45:39.715Z","fjjhdcqsies69y2","Bubble Sort","2026-04-10 16:13:01.240Z",{"collectionId":17,"collectionName":18,"created":59,"created_by":13,"id":60,"name":61,"updated":62,"updated_by":13},"2026-03-04 08:45:39.965Z","b9c614ysjg0x349","Sorting","2026-04-10 16:13:01.402Z",{"code":64,"collectionId":65,"collectionName":66,"created":67,"flag":68,"id":69,"is_default":70,"label":71,"updated":72},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"category":74,"collectionId":75,"collectionName":76,"created":13,"expand":77,"id":90,"slug":91,"updated":13,"views":92},"hsa1afr8fcnd6qb","pbc_2105096300","school_blogs",{"category":78},{"blogIds":79,"collectionId":80,"collectionName":81,"created":82,"created_by":13,"id":74,"image":83,"image_alt":13,"image_path":84,"label":85,"name":21,"priority":86,"publish_at":87,"scheduled_at":13,"status":88,"updated":89,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:24:37.986Z","js2_go_2_11zon_y6paxmuz32.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fhsa1afr8fcnd6qb\u002Fjs2_go_2_11zon_y6paxmuz32.webp",{"en":21,"th":21},10,"2025-08-11 03:41:08.820Z","published","2026-04-25 02:32:14.339Z","y3iy7hx7duwf9hu","js2go-ep35-sorting-algorithms-javascript-go",211,"84ko7hwh4y673id",[20,25,30,35,40,45,50,55,60],"2025-11-03 01:48:08.980Z","Explore and compare popular sorting algorithms Bubble Sort, Merge Sort, and Quick Sort along with Go’s built-in sort package. Learn through real code examples in both JavaScript and Go, and discover which approach best fits your system.","2026-04-25 02:47:55.619Z",1,{"th":91,"en":91}]