Skip to content

Commit 5c8b6ef

Browse files
committed
Selection sort in Go
1 parent 1b8ef75 commit 5c8b6ef

File tree

4 files changed

+73
-15
lines changed

4 files changed

+73
-15
lines changed

Level-2/bubble-sort.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Date: 2018-09-08
2+
Date: 2023-12-28
33
44
Description:
55
Implement bubble sort.

Level-2/selection-sort.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Date: 2023-12-29
3+
4+
Description:
5+
Implement selection sort.
6+
7+
Approach:
8+
Finds index of minimum (when sorting in ascending) element from the remaining
9+
array and swap it with element at the end of sorted sub-array.
10+
While sorting in ascending order minimum element reaches at first place after
11+
first iteration of outer loop.
12+
13+
Complexity:
14+
O(n^2)
15+
*/
16+
17+
package main
18+
19+
import (
20+
"fmt"
21+
"reflect"
22+
)
23+
24+
func selectionSort(nums []int) {
25+
for i := 0; i < len(nums); i++ {
26+
minIdx := i
27+
for j := i + 1; j < len(nums); j++ {
28+
// For sorting in descending order, find maxIdx.
29+
if nums[j] < nums[minIdx] {
30+
minIdx = j
31+
}
32+
}
33+
nums[minIdx], nums[i] = nums[i], nums[minIdx]
34+
}
35+
}
36+
37+
func main() {
38+
testCases := []struct {
39+
input, expected []int
40+
}{
41+
{input: []int{5, 4, 3, 2, 1, 10, 28, 7, 6}, expected: []int{1, 2, 3, 4, 5, 6, 7, 10, 28}},
42+
{input: []int{3, 4, 5, 2, 1}, expected: []int{1, 2, 3, 4, 5}},
43+
{input: []int{3, 4, 5, 2, 1, 6}, expected: []int{1, 2, 3, 4, 5, 6}},
44+
{input: []int{}, expected: []int{}},
45+
{input: []int{1}, expected: []int{1}},
46+
{input: []int{2, 1}, expected: []int{1, 2}},
47+
}
48+
for idx, tc := range testCases {
49+
inputCopy := make([]int, len(tc.input))
50+
copy(inputCopy, tc.input)
51+
selectionSort(inputCopy)
52+
if !reflect.DeepEqual(inputCopy, tc.expected) {
53+
fmt.Printf("\nTC %d failed. Expected %v, got %v", idx, tc.expected, inputCopy)
54+
}
55+
}
56+
}

Level-2/selection_sort.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
#!/usr/bin/python
22

3-
# Date: 2018-09-08
4-
#
5-
# Description:
6-
# Implement selection sort.
7-
#
8-
# Approach:
9-
# Finds index of minimum(when sorting in ascending) element from the remaining
10-
# array and swap it with element at the end of sorted sub-array.
11-
# While sorting in ascending order minimum element reaches at first place after
12-
# first iteration of outer loop.
13-
#
14-
# Complexity:
15-
# O(n^2)
3+
"""
4+
Date: 2018-09-08
165
6+
Description:
7+
Implement selection sort.
8+
9+
Approach:
10+
Finds index of minimum(when sorting in ascending) element from the remaining
11+
array and swap it with element at the end of sorted sub-array.
12+
While sorting in ascending order minimum element reaches at first place after
13+
first iteration of outer loop.
14+
15+
Complexity:
16+
O(n^2)
17+
"""
1718

1819
def selection_sort_ascending(A):
1920
for i in range(len(A)):

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ All programs are categorized in level 1 to 4(1 being easiest)
33
## Sorting
44
- [Bubble sort (Python)](Level-2/bubble_sort.py): Implement bubble sort in Python | O(n^2) | Level 2.
55
- [Bubble sort (Go)](Level-2/bubble-sort.go): Implement bubble sort in Golang | O(n^2) | Level 2.
6-
- [Selection sort](Level-2/selection_sort.py): Implement selection sort in python | O(n^2) | Level 2.
6+
- [Selection sort (Python)](Level-2/selection_sort.py): Implement selection sort in Python | O(n^2) | Level 2.
7+
- [Selection sort (Go)](Level-2/selection-sort.go): Implement selection sort in Golang | O(n^2) | Level 2.
78
- [Selection sort](Level-2/selection_sort.c): Implement selection sort in C| O(n^2) | Level 2.
89
- [Insertion sort](Level-2/insertion_sort.py): Implement insertion sort | O(n^2) | Level 2.
910
- [Heap sort using max heap](Level-3/heap_sort_using_max_heap.c): Build a max heap and sort array in ascending order | Level 3.

0 commit comments

Comments
 (0)