{"id":779,"date":"2026-10-06T13:00:00","date_gmt":"2026-10-06T07:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/"},"modified":"2026-11-29T01:29:37","modified_gmt":"2026-11-28T19:59:37","slug":"go-sorting-programs","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/","title":{"rendered":"Go Sorting Programs: Bubble, Merge, Quick Sort and slices.Sort"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go sorting programs put data in order, such as products by price, students by marks or names from A to Z. In this post, you will write the classic sorting algorithms by hand (bubble, selection, insertion, merge, quick and counting sort) and then use Go&#8217;s built-in <code>slices.Sort<\/code> and <code>slices.SortFunc<\/code> for real work. Every program shows its exact output, and the interview section covers speed, stability and tricky sort questions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post builds on our <a href=\"https:\/\/smitabharat.com\/blog\/go-slice-programs\/\">Go slice programs<\/a>, because every sort here works on a slice. We also use recursion from our <a href=\"https:\/\/smitabharat.com\/blog\/go-number-programs\/\">Go number programs<\/a> for merge sort and quick sort. All programs were tested on Go 1.27.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Go Sorting Programs?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go sorting programs arrange the items of a slice in order, usually from smallest to largest. You can write the algorithm yourself to learn how it works, or call the standard library to sort fast and safely.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of sorting a deck of playing cards. Some people pick the smallest card each time, others slide each new card into its place, and others split the deck in half and merge the piles. Each way is a different sorting algorithm, and some are much faster than others.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Sorting Programs Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sorted data is easier to search, show and compare, which is why Go sorting programs appear in so many apps. For example, an online shop sorts by price, a leaderboard sorts by score and a report sorts by date. Interviewers also love sorting, because it shows if you understand loops, recursion and time complexity.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Interviews:<\/strong> you may be asked to write bubble, insertion, merge or quick sort by hand.<\/li>\n\n\n<li><strong>Real code:<\/strong> you will use <code>slices.Sort<\/code> and <code>slices.SortFunc<\/code> almost every week.<\/li>\n\n\n<li><strong>Algorithms:<\/strong> binary search and many other tricks need sorted data first.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How Go Sorting Programs Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Most Go sorting programs repeat two steps: compare two items and swap them if they are in the wrong order. The algorithms differ in which pairs they compare and how often.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-diagram-1-1024x640.png\" alt=\"Go sorting programs diagram showing bubble sort passes moving the largest value to the end\" class=\"wp-image-766\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">In each bubble sort pass, the largest remaining value moves to the end, so the sorted part grows from the right.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The table below compares the algorithms in this post.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Algorithm<\/th><th>Average time<\/th><th>Extra memory<\/th><th>Stable<\/th><\/tr><\/thead><tbody><tr><td>Bubble sort<\/td><td>O(n squared)<\/td><td>O(1)<\/td><td>Yes<\/td><\/tr><tr><td>Selection sort<\/td><td>O(n squared)<\/td><td>O(1)<\/td><td>No<\/td><\/tr><tr><td>Insertion sort<\/td><td>O(n squared)<\/td><td>O(1)<\/td><td>Yes<\/td><\/tr><tr><td>Merge sort<\/td><td>O(n log n)<\/td><td>O(n)<\/td><td>Yes<\/td><\/tr><tr><td>Quick sort<\/td><td>O(n log n)<\/td><td>O(log n)<\/td><td>No<\/td><\/tr><tr><td>Counting sort<\/td><td>O(n + k)<\/td><td>O(k)<\/td><td>Yes<\/td><\/tr><tr><td>slices.Sort (pdqsort)<\/td><td>O(n log n)<\/td><td>O(log n)<\/td><td>No<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">A sort is <strong>stable<\/strong> when equal items keep their original order. This matters when you sort records by one field after another.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Simple Go Sorting Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These first four Go sorting programs are easy to write and good for small lists.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 1: Compare and Swap Two Items<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Every sort is built from one small step. You compare two items, and if they are in the wrong order, you swap them.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ := creates a variable and gives it a value in one go.\n    numbers := []int{9, 4}\n    fmt.Println(\"Before:\", numbers)\n\n    \/\/ Is the first item bigger than the second? Then they are in the wrong order.\n    if numbers[0] &gt; numbers[1] {\n        \/\/ Swap: Go reads both right-side values first, then sets both left sides.\n        numbers[0], numbers[1] = numbers[1], numbers[0]\n    }\n    fmt.Println(\"After:\", numbers)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Before: [9 4]\nAfter: [4 9]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The program checks whether 9 is bigger than 4. It is, so the two items swap places. Bubble sort below repeats this exact step over the whole slice.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 2: Bubble Sort<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Bubble sort swaps neighbours that are out of order. You stop early if a pass makes no swaps.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ bubbleSort sorts numbers from small to large by swapping neighbours.\nfunc bubbleSort(numbers []int) {\n    \/\/ Each pass moves the largest remaining number to the end.\n    for i := 0; i &lt; len(numbers)-1; i++ {\n        swapped := false\n        \/\/ The last i items are already in place, so skip them.\n        for j := 0; j &lt; len(numbers)-1-i; j++ {\n            if numbers[j] &gt; numbers[j+1] {\n                numbers[j], numbers[j+1] = numbers[j+1], numbers[j] \/\/ swap\n                swapped = true\n            }\n        }\n        fmt.Println(\"Pass\", i+1, numbers)\n        \/\/ No swaps means the slice is already sorted.\n        if !swapped {\n            break\n        }\n    }\n}\n\nfunc main() {\n    numbers := []int{5, 1, 4, 2, 8}\n    bubbleSort(numbers) \/\/ a slice shares its data, so the function sorts our numbers\n    fmt.Println(\"Sorted:\", numbers)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Pass 1 [1 4 2 5 8]\nPass 2 [1 2 4 5 8]\nPass 3 [1 2 4 5 8]\nSorted: [1 2 4 5 8]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The inner loop compares each pair of neighbours and swaps them if needed. After each pass, the largest remaining number sits at the end. Pass 3 made no swaps, so the loop stopped early.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 3: Selection Sort<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Selection sort finds the smallest item and moves it to the front, one position at a time.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ selectionSort puts the smallest remaining number at the front on each pass.\nfunc selectionSort(numbers []int) {\n    for i := 0; i &lt; len(numbers)-1; i++ {\n        \/\/ Step 1: find the position of the smallest number from i to the end.\n        smallestIndex := i\n        for j := i + 1; j &lt; len(numbers); j++ {\n            if numbers[j] &lt; numbers[smallestIndex] {\n                smallestIndex = j\n            }\n        }\n        \/\/ Step 2: swap it into position i.\n        numbers[i], numbers[smallestIndex] = numbers[smallestIndex], numbers[i]\n    }\n}\n\nfunc main() {\n    numbers := []int{64, 25, 12, 22, 11}\n    selectionSort(numbers)\n    fmt.Println(numbers)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[11 12 22 25 64]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On each pass, the inner loop finds the smallest number that is left. Then one swap moves it to the front of the unsorted part. It makes at most n &#8211; 1 swaps, but it always does about n squared comparisons.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 4: Insertion Sort<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Insertion sort takes each item and slides it left until it fits, like sorting cards in your hand.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ insertionSort slides each number left until it sits in the right place.\nfunc insertionSort(numbers []int) {\n    for i := 1; i &lt; len(numbers); i++ {\n        \/\/ Step 1: pick up the current card.\n        current := numbers[i]\n\n        \/\/ Step 2: move bigger numbers one place to the right.\n        j := i - 1\n        for j &gt;= 0 &amp;&amp; numbers[j] &gt; current {\n            numbers[j+1] = numbers[j]\n            j--\n        }\n\n        \/\/ Step 3: drop the card into the gap.\n        numbers[j+1] = current\n    }\n}\n\nfunc main() {\n    numbers := []int{12, 11, 13, 5, 6}\n    insertionSort(numbers)\n    fmt.Println(numbers)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[5 6 11 12 13]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You hold <code>current<\/code> and shift every bigger number one place right. When the loop stops, the gap is the right spot for <code>current<\/code>. Insertion sort is very fast on data that is almost sorted.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fast Go Sorting Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Go sorting programs split the work and run in O(n log n) time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 5: Merge Sort<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Merge sort splits the slice in half, sorts each half, then merges the two sorted halves.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ mergeSort returns a new sorted slice. It calls itself on each half (recursion).\nfunc mergeSort(numbers []int) []int {\n    \/\/ A slice with 0 or 1 items is already sorted.\n    if len(numbers) &lt;= 1 {\n        return numbers\n    }\n    middle := len(numbers) \/ 2\n    left := mergeSort(numbers[:middle])  \/\/ [:middle] is the first half\n    right := mergeSort(numbers[middle:]) \/\/ [middle:] is the second half\n    return merge(left, right)\n}\n\n\/\/ merge joins two sorted slices into one sorted slice.\nfunc merge(left, right []int) []int {\n    \/\/ make creates an empty slice with room for all items.\n    result := make([]int, 0, len(left)+len(right))\n    i, j := 0, 0\n    \/\/ Take the smaller front item from either side, until one side runs out.\n    for i &lt; len(left) &amp;&amp; j &lt; len(right) {\n        if left[i] &lt;= right[j] {\n            result = append(result, left[i])\n            i++\n        } else {\n            result = append(result, right[j])\n            j++\n        }\n    }\n    \/\/ Add whatever is left. ... spreads a slice into separate values.\n    result = append(result, left[i:]...)\n    result = append(result, right[j:]...)\n    return result\n}\n\nfunc main() {\n    fmt.Println(mergeSort([]int{38, 27, 43, 3, 9, 82, 10}))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[3 9 10 27 38 43 82]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>mergeSort<\/code> keeps splitting until each piece has one item. Then <code>merge<\/code> joins two sorted pieces by always taking the smaller front item. Using <code>&lt;=<\/code> keeps equal items in order, which makes merge sort stable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 6: Quick Sort<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Quick sort picks a pivot, puts smaller items on its left and bigger items on its right, then repeats on each side.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ quickSort sorts numbers in place around a pivot, then sorts each side.\nfunc quickSort(numbers []int) {\n    if len(numbers) &lt; 2 {\n        return\n    }\n    \/\/ Step 1: use the last item as the pivot.\n    last := len(numbers) - 1\n    pivot := numbers[last]\n\n    \/\/ Step 2: move every number smaller than the pivot to the front.\n    \/\/ boundary is where the next small number should go.\n    boundary := 0\n    for j := 0; j &lt; last; j++ {\n        if numbers[j] &lt; pivot {\n            numbers[boundary], numbers[j] = numbers[j], numbers[boundary]\n            boundary++\n        }\n    }\n\n    \/\/ Step 3: put the pivot right after the small numbers.\n    numbers[boundary], numbers[last] = numbers[last], numbers[boundary]\n\n    \/\/ Step 4: sort the left side and the right side the same way.\n    quickSort(numbers[:boundary])\n    quickSort(numbers[boundary+1:])\n}\n\nfunc main() {\n    numbers := []int{10, 80, 30, 90, 40, 50, 70}\n    quickSort(numbers)\n    fmt.Println(numbers)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[10 30 40 50 70 80 90]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The pivot here is 70, so all smaller numbers move to the front. Then 70 goes right after them, and each side is sorted the same way. Its worst case is O(n squared) on already sorted data, so real libraries choose the pivot more carefully.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 7: Counting Sort<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When values sit in a small range, counting sort beats every comparison sort.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    ages := []int{5, 3, 8, 3, 1, 5, 3, 9}\n\n    \/\/ Step 1: count how many times each age appears.\n    \/\/ count[3] will hold how many 3s there are.\n    var count [10]int\n    for _, age := range ages { \/\/ _ skips the index we do not need\n        count[age]++\n    }\n\n    \/\/ Step 2: write each age as many times as it was counted.\n    sorted := make([]int, 0, len(ages))\n    for age, times := range count {\n        for range times { \/\/ range over a number repeats the loop that many times\n            sorted = append(sorted, age)\n        }\n    }\n    fmt.Println(sorted)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[1 3 3 3 5 5 8 9]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First, you count each age in the <code>count<\/code> array. Then you walk the array from 0 to 9 and write each age as many times as it appeared. No two items are ever compared.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Built-In Go Sorting Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In real code, Go sorting programs should use the standard library. It is fast, tested and short.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 8: slices.Sort for Numbers and Strings<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\nfunc main() {\n    numbers := []int{42, 7, 19, 3}\n    slices.Sort(numbers) \/\/ sorts the slice itself, small to large\n    fmt.Println(numbers)\n\n    names := []string{\"ravi\", \"Asha\", \"meera\", \"Kiran\"}\n    slices.Sort(names)\n    fmt.Println(names)\n    fmt.Println(slices.IsSorted(names)) \/\/ true if already in order\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[3 7 19 42]\n[Asha Kiran meera ravi]\ntrue<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>slices.Sort<\/code> sorts the slice in one line. Strings sort by byte value, so capital letters come before small ones. To ignore case, use <code>slices.SortFunc<\/code> with <code>strings.ToLower<\/code>, as in Program 10.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 9: Show the Top Three Scores<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This everyday program sorts scores and then keeps only the best three.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\nfunc main() {\n    scores := []int{72, 95, 60, 88, 79, 91}\n\n    \/\/ Step 1: sort from small to large.\n    slices.Sort(scores)\n\n    \/\/ Step 2: flip the order, so the biggest score comes first.\n    slices.Reverse(scores)\n\n    \/\/ Step 3: [:3] takes the first three items.\n    topThree := scores[:3]\n    fmt.Println(\"Top three:\", topThree)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Top three: [95 91 88]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First, <code>slices.Sort<\/code> puts the scores from low to high. Then <code>slices.Reverse<\/code> flips them, so the best score is first. Finally, <code>scores[:3]<\/code> keeps the first three.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 10: Sort Descending and Case-Insensitive<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"cmp\"\n    \"fmt\"\n    \"slices\"\n    \"strings\"\n)\n\n\/\/ largeFirst compares two numbers so bigger numbers come first.\n\/\/ cmp.Compare(x, y) gives -1 if x is smaller, 0 if equal and 1 if bigger.\nfunc largeFirst(first, second int) int {\n    return cmp.Compare(second, first) \/\/ second goes first, so the order is flipped\n}\n\n\/\/ ignoreCase compares two words as if both were lower case.\nfunc ignoreCase(first, second string) int {\n    return cmp.Compare(strings.ToLower(first), strings.ToLower(second))\n}\n\nfunc main() {\n    numbers := []int{42, 7, 19, 3}\n    slices.SortFunc(numbers, largeFirst) \/\/ sort using our compare function\n    fmt.Println(numbers)\n\n    names := []string{\"ravi\", \"Asha\", \"meera\", \"Kiran\"}\n    slices.SortFunc(names, ignoreCase)\n    fmt.Println(names)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[42 19 7 3]\n[Asha Kiran meera ravi]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>slices.SortFunc<\/code> asks your function how two items compare. The function returns a negative number, zero or a positive number. Swapping <code>first<\/code> and <code>second<\/code> in <code>cmp.Compare<\/code> flips the order to descending.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 11: Sort Structs by a Field<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"cmp\"\n    \"fmt\"\n    \"slices\"\n)\n\n\/\/ Product is one item in a shop, with a name and a price.\ntype Product struct {\n    Name  string\n    Price int\n}\n\n\/\/ byPrice puts cheaper products first.\nfunc byPrice(first, second Product) int {\n    return cmp.Compare(first.Price, second.Price)\n}\n\nfunc main() {\n    products := []Product{{\"Bag\", 799}, {\"Pen\", 20}, {\"Book\", 350}, {\"Lamp\", 450}}\n    slices.SortFunc(products, byPrice)\n    for _, product := range products {\n        fmt.Println(product.Price, product.Name)\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>20 Pen\n350 Book\n450 Lamp\n799 Bag<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The compare function looks only at the <code>Price<\/code> field. So <code>slices.SortFunc<\/code> orders the products from cheapest to most expensive. The loop then prints each price with its name.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 12: Sort by Two Fields with cmp.Or<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>cmp.Or<\/code> returns the first non-zero comparison. So it sorts by the first field, then breaks ties with the second.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"cmp\"\n    \"fmt\"\n    \"slices\"\n)\n\n\/\/ Student holds a name and the marks scored.\ntype Student struct {\n    Name  string\n    Marks int\n}\n\n\/\/ byMarksThenName sorts by marks high to low, and by name A to Z when marks tie.\nfunc byMarksThenName(first, second Student) int {\n    return cmp.Or(\n        cmp.Compare(second.Marks, first.Marks), \/\/ marks high to low\n        cmp.Compare(first.Name, second.Name),   \/\/ then name A to Z\n    )\n}\n\nfunc main() {\n    students := []Student{{\"Ravi\", 82}, {\"Asha\", 91}, {\"Kiran\", 82}, {\"Meera\", 91}}\n    slices.SortFunc(students, byMarksThenName)\n    fmt.Println(students)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[{Asha 91} {Meera 91} {Kiran 82} {Ravi 82}]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If two students have different marks, the first comparison decides. When the marks tie, it returns 0, so <code>cmp.Or<\/code> moves on to the names. That is why Asha comes before Meera.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 13: Stable Sort Keeps Equal Items in Order<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"cmp\"\n    \"fmt\"\n    \"slices\"\n)\n\n\/\/ Task is a job with a priority, where 1 is the most urgent.\ntype Task struct {\n    Name     string\n    Priority int\n}\n\n\/\/ byPriority puts the most urgent tasks first.\nfunc byPriority(first, second Task) int {\n    return cmp.Compare(first.Priority, second.Priority)\n}\n\nfunc main() {\n    tasks := []Task{{\"email\", 2}, {\"deploy\", 1}, {\"review\", 2}, {\"fix bug\", 1}, {\"lunch\", 3}}\n    \/\/ SortStableFunc keeps tasks with equal priority in their input order.\n    slices.SortStableFunc(tasks, byPriority)\n    fmt.Println(tasks)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[{deploy 1} {fix bug 1} {email 2} {review 2} {lunch 3}]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&#8220;deploy&#8221; stays before &#8220;fix bug&#8221; and &#8220;email&#8221; stays before &#8220;review&#8221;, just as in the input. A normal sort does not promise this. Use the stable version when the old order still matters.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 14: Sort a Map by Its Values<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Maps have no order, so you copy the keys into a slice and sort that slice.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"cmp\"\n    \"fmt\"\n    \"maps\"\n    \"slices\"\n)\n\nfunc main() {\n    sales := map[string]int{\"Pune\": 420, \"Delhi\": 980, \"Goa\": 150, \"Mumbai\": 760}\n\n    \/\/ Step 1: maps.Keys hands out each key, and slices.Collect puts them in a slice.\n    cities := slices.Collect(maps.Keys(sales))\n\n    \/\/ Step 2: sort the city names by their sales, high to low.\n    \/\/ This small function inside the call is a closure: it can read the sales map.\n    slices.SortFunc(cities, func(first, second string) int {\n        return cmp.Compare(sales[second], sales[first])\n    })\n\n    for _, city := range cities {\n        fmt.Println(city, sales[city])\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Delhi 980\nMumbai 760\nPune 420\nGoa 150<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>slices.Collect<\/code> turns the key iterator into a slice. Then you sort the city names by their sales, from high to low. The function sits inside the call, because it needs to read the <code>sales<\/code> map.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Hand-Written vs Built-In Go Sorting Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The diagram and table below compare writing a sort yourself with using the library.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-diagram-2-1024x640.png\" alt=\"Go sorting programs comparison of hand-written sorts and slices.Sort\" class=\"wp-image-767\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Hand-written sorts are for learning and interviews, while slices.Sort is for real code.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Hand-written sort<\/th><th>slices.Sort and SortFunc<\/th><\/tr><\/thead><tbody><tr><td>Speed on big data<\/td><td>Depends, often slow<\/td><td>Fast pdqsort<\/td><\/tr><tr><td>Bugs<\/td><td>Easy to make<\/td><td>Well tested<\/td><\/tr><tr><td>Code length<\/td><td>15 to 30 lines<\/td><td>One line<\/td><\/tr><tr><td>Custom order<\/td><td>Edit the algorithm<\/td><td>Pass a compare function<\/td><\/tr><tr><td>Best for<\/td><td>Learning, interviews<\/td><td>Production code<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In short, practise the classic algorithms for interviews, but always use <code>slices.Sort<\/code> or <code>slices.SortFunc<\/code> at work.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes in Go Sorting Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Off-by-one in bubble sort:<\/strong> the inner loop must stop at <code>len(numbers)-1-i<\/code>, or it reads past the end.<\/li>\n\n\n<li><strong>Forgetting the result of merge sort:<\/strong> our merge sort returns a new slice, so assign it.<\/li>\n\n\n<li><strong>Expecting stability:<\/strong> <code>slices.Sort<\/code> and <code>slices.SortFunc<\/code> are not stable. Use <code>SortStableFunc<\/code> when order matters.<\/li>\n\n\n<li><strong>Case surprises:<\/strong> &#8220;Zoo&#8221; sorts before &#8220;apple&#8221;, because capitals come first in byte order.<\/li>\n\n\n<li><strong>Sorting a map directly:<\/strong> maps cannot be sorted. Sort a slice of their keys instead.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Sorting Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Use slices.Sort<\/strong> for basic types, since it is generic and fast.<\/li>\n\n\n<li><strong>Call cmp.Compare inside SortFunc<\/strong> instead of writing your own if chains.<\/li>\n\n\n<li><strong>Combine fields with cmp.Or<\/strong>, which keeps tie-breaking clear.<\/li>\n\n\n<li><strong>Clone before sorting<\/strong> if the caller still needs the original order.<\/li>\n\n\n<li><strong>Explain time and memory<\/strong> for any algorithm you write in an interview.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Go Sorting Programs Interview Questions and Answers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These interview questions on Go sorting programs test both algorithms and the standard sort tools. Each program is complete, with real output from Go 1.27.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Algorithm Questions on Go Sorting Programs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1. Which sort is best for almost sorted data?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Insertion sort. Each item moves only a few places, so it runs close to O(n). Bubble sort with an early stop is also fast here.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2. Why is merge sort stable but quick sort not?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Merge sort takes from the left half first when items are equal, so their order stays. Quick sort swaps items across the pivot, which can jump an item past an equal one.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    numbers := []int{3, 1, 2}\n    swaps := 0\n    for i := 0; i &lt; len(numbers); i++ {\n        for j := 0; j &lt; len(numbers)-1-i; j++ {\n            if numbers[j] &gt; numbers[j+1] {\n                numbers[j], numbers[j+1] = numbers[j+1], numbers[j] \/\/ swap neighbours\n                swaps++\n            }\n        }\n    }\n    fmt.Println(numbers, swaps)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[1 2 3] 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3 swaps with 1, then 3 swaps with 2. After that, the slice is sorted, so the count is 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4. What is the worst case of quick sort, and how do libraries avoid it?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is O(n squared), which happens when the pivot is always the smallest or largest item. Go&#8217;s <code>slices.Sort<\/code> uses pdqsort, which picks pivots carefully and falls back to heap sort if things go badly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Go Sort Tool Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\nfunc main() {\n    values := []float64{2.5, -1, 3.75, 0}\n    slices.Sort(values)\n    fmt.Println(values)\n\n    \/\/ BinarySearch returns the position and found (true if the value is there).\n    position, found := slices.BinarySearch(values, 2.5)\n    fmt.Println(position, found)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[-1 0 2.5 3.75]\n2 true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After sorting, <code>slices.BinarySearch<\/code> finds 2.5 at index 2. We cover searching fully in the next post.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6. What is the difference between sort.Ints and slices.Sort?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Both sort a slice of ints. <code>sort.Ints<\/code> is from the older <code>sort<\/code> package and works only for ints. <code>slices.Sort<\/code> is generic, so it works for any ordered type, and it is the modern choice.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sort\"\n)\n\n\/\/ Person holds a name and an age.\ntype Person struct {\n    Name string\n    Age  int\n}\n\nfunc main() {\n    people := []Person{{\"Asha\", 30}, {\"Ravi\", 25}, {\"Meera\", 35}}\n    \/\/ sort.Slice needs a \"less\" function: is the item at i smaller than the item at j?\n    sort.Slice(people, func(i, j int) bool {\n        return people[i].Age &lt; people[j].Age\n    })\n    fmt.Println(people)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[{Ravi 25} {Asha 30} {Meera 35}]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>sort.Slice<\/code> takes a &#8220;less&#8221; function with two indexes. You will see it a lot in older code, while new code prefers <code>slices.SortFunc<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"cmp\"\n    \"fmt\"\n)\n\nfunc main() {\n    \/\/ cmp.Compare gives -1 (smaller), 0 (equal) or 1 (bigger).\n    fmt.Println(cmp.Compare(3, 7), cmp.Compare(\"b\", \"a\"), cmp.Compare(5, 5))\n    \/\/ cmp.Or gives the first value that is not zero.\n    fmt.Println(cmp.Or(0, 0, -1, 1))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>-1 1 0\n-1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>cmp.Compare<\/code> returns -1, 0 or 1. <code>cmp.Or<\/code> returns the first value that is not zero, which is -1 here.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\nfunc main() {\n    words := []string{\"go\", \"Go\", \"GO\", \"gO\"}\n    slices.Sort(words)\n    fmt.Println(words)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[GO Go gO go]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Upper case letters have smaller byte values than lower case ones. So &#8220;GO&#8221; comes first and &#8220;go&#8221; comes last.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10. How do you sort a slice without changing the original?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\nfunc main() {\n    original := []int{9, 4, 7}\n    \/\/ slices.Values hands out each item, and slices.Sorted builds a new sorted slice.\n    sorted := slices.Sorted(slices.Values(original))\n    fmt.Println(original, sorted)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[9 4 7] [4 7 9]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>slices.Sorted<\/code> collects the values into a new slice and sorts that. You could also call <code>slices.Clone<\/code> and then <code>slices.Sort<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11. Can you sort a slice of structs by many fields?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Inside <code>slices.SortFunc<\/code>, combine comparisons with <code>cmp.Or<\/code>, from the most important field to the least. Program 12 shows this.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12. What is the time complexity of slices.Sort?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is O(n log n) on average and in the worst case. It also handles already sorted or reversed data very quickly, because pdqsort spots these patterns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This task is one of the most practical Go sorting programs. Write a program that sorts a list of employees by department name A to Z, and inside each department by salary from high to low. Print each employee on one line. Hint: use <code>slices.SortFunc<\/code> with <code>cmp.Or<\/code> and two <code>cmp.Compare<\/code> calls.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pkg.go.dev\/slices#SortFunc\">slices package: Sort and SortFunc<\/a><\/li>\n\n\n<li><a href=\"https:\/\/pkg.go.dev\/cmp\">cmp package documentation<\/a><\/li>\n\n\n<li><a href=\"https:\/\/pkg.go.dev\/sort\">sort package documentation<\/a><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go sorting programs can be hand-written for learning or built-in for real work.<\/li>\n\n\n<li>Bubble, selection and insertion sort are O(n squared), while merge and quick sort are O(n log n).<\/li>\n\n\n<li>A stable sort keeps equal items in their original order.<\/li>\n\n\n<li>Use <code>slices.Sort<\/code> for simple types and <code>slices.SortFunc<\/code> with <code>cmp.Compare<\/code> for custom order.<\/li>\n\n\n<li>Sort maps by copying their keys into a slice first.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now that our data is in order, we can find things in it quickly. Read <a href=\"https:\/\/smitabharat.com\/blog\/go-searching-programs\/\">Go searching programs with output<\/a>, with linear search, binary search and <code>slices.BinarySearch<\/code>. For more on slices, revisit our <a href=\"https:\/\/smitabharat.com\/blog\/go-slice-programs\/\">Go slice programs<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go sorting programs put data in order, such as products by price, students by marks or names from A to Z. In this post,&hellip;<\/p>\n","protected":false},"author":1,"featured_media":768,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[106],"tags":[96,95,11,104,114],"swapz_series":[],"class_list":["post-779","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-programs","tag-beginner","tag-go-interview-questions","tag-go-lang","tag-go-programs","tag-go-sorting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Sorting Programs: Bubble, Merge, Quick Sort<\/title>\n<meta name=\"description\" content=\"Go sorting programs with output: bubble, selection, insertion, merge, quick and counting sort, slices.Sort, SortFunc and sorting interview questions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Sorting Programs: Bubble, Merge, Quick Sort\" \/>\n<meta property=\"og:description\" content=\"Go sorting programs with output: bubble, selection, insertion, merge, quick and counting sort, slices.Sort, SortFunc and sorting interview questions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-06T07:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-28T19:59:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sorting-programs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sorting-programs\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Sorting Programs: Bubble, Merge, Quick Sort and slices.Sort\",\"datePublished\":\"2026-10-06T07:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sorting-programs\\\/\"},\"wordCount\":1993,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sorting-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-sorting-programs-featured.png\",\"keywords\":[\"Beginner\",\"Go Interview Questions\",\"Go Lang\",\"Go Programs\",\"Go Sorting\"],\"articleSection\":[\"Go Programs\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sorting-programs\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sorting-programs\\\/\",\"name\":\"Go Sorting Programs: Bubble, Merge, Quick Sort\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sorting-programs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sorting-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-sorting-programs-featured.png\",\"datePublished\":\"2026-10-06T07:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:37+00:00\",\"description\":\"Go sorting programs with output: bubble, selection, insertion, merge, quick and counting sort, slices.Sort, SortFunc and sorting interview questions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sorting-programs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sorting-programs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sorting-programs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-sorting-programs-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-sorting-programs-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Sorting Programs featured image: bubble, merge and quick sort output\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sorting-programs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Sorting Programs: Bubble, Merge, Quick Sort and slices.Sort\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Sorting Programs: Bubble, Merge, Quick Sort","description":"Go sorting programs with output: bubble, selection, insertion, merge, quick and counting sort, slices.Sort, SortFunc and sorting interview questions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/","og_locale":"en_US","og_type":"article","og_title":"Go Sorting Programs: Bubble, Merge, Quick Sort","og_description":"Go sorting programs with output: bubble, selection, insertion, merge, quick and counting sort, slices.Sort, SortFunc and sorting interview questions.","og_url":"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-10-06T07:30:00+00:00","article_modified_time":"2026-11-28T19:59:37+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Sorting Programs: Bubble, Merge, Quick Sort and slices.Sort","datePublished":"2026-10-06T07:30:00+00:00","dateModified":"2026-11-28T19:59:37+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/"},"wordCount":1993,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-featured.png","keywords":["Beginner","Go Interview Questions","Go Lang","Go Programs","Go Sorting"],"articleSection":["Go Programs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/","url":"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/","name":"Go Sorting Programs: Bubble, Merge, Quick Sort","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-featured.png","datePublished":"2026-10-06T07:30:00+00:00","dateModified":"2026-11-28T19:59:37+00:00","description":"Go sorting programs with output: bubble, selection, insertion, merge, quick and counting sort, slices.Sort, SortFunc and sorting interview questions.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-sorting-programs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sorting-programs-featured.png","width":2400,"height":1260,"caption":"Go Sorting Programs featured image: bubble, merge and quick sort output"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Sorting Programs: Bubble, Merge, Quick Sort and slices.Sort"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=779"}],"version-history":[{"count":1,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/779\/revisions"}],"predecessor-version":[{"id":881,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/779\/revisions\/881"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/768"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=779"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}