Skip to content

Commit 3499ab6

Browse files
seong954tegonSchiele
authored andcommitted
golang about 04_quicksort (egonSchiele#43)
* recursion_Golang * go_fmt * selection_sort_Golang * rm-golang * selection_sort_Golang * selection_sort_Golang * selection_sort_Golang * dynamic_golang * golang 05_hash_tables 02_check_voter.go * golang 05_hash_tables 01_price_of_groceries * add 01_loop_sum in 04_quicksort * 02_recursive_sum in 04_quicksort * 03_recursive_count in 04_quicksort * 04_recursive_max in 04_quicksort * 05_quicksort in 04_quicksort
1 parent cdec985 commit 3499ab6

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

04_quicksort/golang/01_loop_sum.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func sum(arr []int) int {
6+
total := 0
7+
for _, num := range arr {
8+
total += num
9+
}
10+
return total
11+
}
12+
13+
func main() {
14+
fmt.Println(sum([]int{1, 2, 3, 4}))
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func sum(arr []int) int {
6+
if len(arr) == 0 {
7+
return 0
8+
}
9+
return arr[0] + sum(arr[1:])
10+
}
11+
12+
func main() {
13+
fmt.Println(sum([]int{1, 2, 3, 4}))
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func count(list []int) int {
6+
if len(list) == 0 {
7+
return 0
8+
}
9+
return 1 + count(list[1:])
10+
}
11+
12+
func main() {
13+
fmt.Println(count([]int{0, 1, 2, 3, 4, 5}))
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func max(list []int) int {
6+
if len(list) == 2 {
7+
if list[0] > list[1] {
8+
return list[0]
9+
}
10+
return list[1]
11+
}
12+
13+
subMax := max(list[1:])
14+
if list[0] > subMax {
15+
return list[0]
16+
}
17+
return subMax
18+
}
19+
20+
func main() {
21+
fmt.Println(max([]int{1, 5, 10, 25, 16, 1}))
22+
}

04_quicksort/golang/05_quicksort.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func quicksort(list []int) []int {
6+
if len(list) < 2 {
7+
return list
8+
} else {
9+
pivot := list[0]
10+
11+
var less = []int{}
12+
var greater = []int{}
13+
for _, num := range list[1:] {
14+
if pivot > num {
15+
less = append(less, num)
16+
} else {
17+
greater = append(greater, num)
18+
}
19+
}
20+
21+
less = append(quicksort(less), pivot)
22+
greater = quicksort(greater)
23+
return append(less, greater...)
24+
}
25+
}
26+
27+
func main() {
28+
fmt.Println(quicksort([]int{10, 5, 2, 3}))
29+
}

0 commit comments

Comments
 (0)