golang about 04_quicksort (#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
This commit is contained in:
committed by
Aditya Bhargava
parent
cdec985db1
commit
3499ab656a
15
04_quicksort/golang/01_loop_sum.go
Normal file
15
04_quicksort/golang/01_loop_sum.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func sum(arr []int) int {
|
||||||
|
total := 0
|
||||||
|
for _, num := range arr {
|
||||||
|
total += num
|
||||||
|
}
|
||||||
|
return total
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(sum([]int{1, 2, 3, 4}))
|
||||||
|
}
|
||||||
14
04_quicksort/golang/02_Recursive_sum.go
Normal file
14
04_quicksort/golang/02_Recursive_sum.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func sum(arr []int) int {
|
||||||
|
if len(arr) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return arr[0] + sum(arr[1:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(sum([]int{1, 2, 3, 4}))
|
||||||
|
}
|
||||||
14
04_quicksort/golang/03_recursive_count.go
Normal file
14
04_quicksort/golang/03_recursive_count.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func count(list []int) int {
|
||||||
|
if len(list) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return 1 + count(list[1:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(count([]int{0, 1, 2, 3, 4, 5}))
|
||||||
|
}
|
||||||
22
04_quicksort/golang/04_recursive_max.go
Normal file
22
04_quicksort/golang/04_recursive_max.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func max(list []int) int {
|
||||||
|
if len(list) == 2 {
|
||||||
|
if list[0] > list[1] {
|
||||||
|
return list[0]
|
||||||
|
}
|
||||||
|
return list[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
subMax := max(list[1:])
|
||||||
|
if list[0] > subMax {
|
||||||
|
return list[0]
|
||||||
|
}
|
||||||
|
return subMax
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(max([]int{1, 5, 10, 25, 16, 1}))
|
||||||
|
}
|
||||||
29
04_quicksort/golang/05_quicksort.go
Normal file
29
04_quicksort/golang/05_quicksort.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func quicksort(list []int) []int {
|
||||||
|
if len(list) < 2 {
|
||||||
|
return list
|
||||||
|
} else {
|
||||||
|
pivot := list[0]
|
||||||
|
|
||||||
|
var less = []int{}
|
||||||
|
var greater = []int{}
|
||||||
|
for _, num := range list[1:] {
|
||||||
|
if pivot > num {
|
||||||
|
less = append(less, num)
|
||||||
|
} else {
|
||||||
|
greater = append(greater, num)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
less = append(quicksort(less), pivot)
|
||||||
|
greater = quicksort(greater)
|
||||||
|
return append(less, greater...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(quicksort([]int{10, 5, 2, 3}))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user