Skip to content

Commit

Permalink
Fixes #93 - Adds Selection Sort method in golang
Browse files Browse the repository at this point in the history
  • Loading branch information
haroon-sheikh committed Oct 1, 2017
1 parent 004cb49 commit 4c368d6
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions code/sorting/selection_sort/SelectionSort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Selection Sort in Golang
package main

import (
"fmt"
)

// SelectionSort sorts the given slice
func SelectionSort(items []int) {
var n = len(items)
for i := 0; i < n; i++ {
var minIdx = i
for j := i; j < n; j++ {
if items[j] < items[minIdx] {
minIdx = j
}
}
items[i], items[minIdx] = items[minIdx], items[i]
}
}

func main() {
randomSlice := []int{1, 6, 2, 4, 9, 0, 5, 3, 7, 8}
fmt.Println("Unsorted Slice: ", randomSlice)
SelectionSort(randomSlice)
fmt.Println("Sorted Slice: ", randomSlice)
}

0 comments on commit 4c368d6

Please sign in to comment.