-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #93 - Adds Selection Sort method in golang
- Loading branch information
1 parent
004cb49
commit 4c368d6
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |