7
\$\begingroup\$

I am just trying to use Go so I will be thankful for all remarks and corrections:

package main

import "fmt"

func selectionSort(a []int) []int {

    for i := 0; i < len(a); i++ {

        min := i

        for j:= i + 1; j < len(a); j++ {
            if a[j] < a[min] {
                min = j;
            }
        }

        var tmp int = a[i]
        a[i] = a[min]
        a[min] = tmp
    }

    return a
}

func main() {

    values := []int { 5, 8, 4, 1, 7,  2, 3, 6 }
    result := selectionSort(values)
    fmt.Println(result)
}
\$\endgroup\$
2
\$\begingroup\$

As @coderodde said, get rid of that return and try to use algoutils package. That being said (there's no much about it), I'd GO with this version:

package main
// try using algoutils so that you can make use of Swap later
import (
    "fmt"
    "algoutils"
)

func selectionSort(array []int) {  // get rid of that ugly type declarator

    for i := 0; i < len(array) - 1; i++ {
        min := i
        for j := i + 1; j < len(array) - 1; j++ {
            if array[j] < array[min] {
                min = j
            }
        }
        algoutils.Swap(array, i, min)
    }
}

func main() {

    array := []int{5, 8, 4, 1, 7,  2, 3, 6}
    fmt.Println("Unsorted array: ", array)
    selectionSort(array)
    fmt.Println("Sorted array: ", array)
}

As a side-note, variable names should be as clear as possible:

func selectionSort(a []int) -> here it's clear a it's an array, but wouldn't be simpler if you'd just call it array []int ?

\$\endgroup\$
  • 3
    \$\begingroup\$ DON'T use algoutils. Use a tuple assignment. \$\endgroup\$ – peterSO Mar 13 '16 at 17:37
2
\$\begingroup\$

You sort the arrays in-place (as it should be), so you don't really need to return the input array back from the sort function. I would remove the return type declarator ([]int after the argument list), and remove the return statement.

\$\endgroup\$
5
\$\begingroup\$

On Stack Overflow duplicate questions are closed.

For your question, a search of Code Review Stack Exchange for "[go] selection sort" finds, amongst others,

Selection sort algorithm with increasing/decreasing sort options

which has several detailed answers that address your question. For example, the code for my answer, simplified for ascending order only, is

// selectionSort sorts the array a into ascending order.
// The selection sort algorithm is an in-place comparison sort
// with O(n*n) time complexity.
func selectionSort(a []int) {
    for i, x := range a[:len(a)-1] {
        k := i
        for j, y := range a[i+1 : len(a)] {
            if x > y {
                // out of order
                k = i + 1 + j
                x = y
            }
        }
        a[k], a[i] = a[i], x
    }
}

My code is idomatic Go and it's more than twice as fast as your code when running a Go benchmark which sorts 1024 random integers.

BenchmarkDemas           300       4894852 ns/op
BenchmarkPeterSO        1000       2339806 ns/op
\$\endgroup\$
  • 1
    \$\begingroup\$ This is the correct answer. \$\endgroup\$ – OneOfOne Mar 28 '16 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.