golang-exercises/array-sorter/main.go

68 lines
1.4 KiB
Go
Raw Normal View History

2024-02-02 09:18:21 +01:00
package main
import (
"fmt"
"git.bulgariu.xyz/raul/min-max-array"
2024-02-02 09:18:21 +01:00
"os"
"strconv"
)
var numArray = []float64{}
var numToAdd int
var err error
func main() {
if len(os.Args) < 2 {
fmt.Println("Tool to automatically sort an array from the smallest number to the largest")
fmt.Println("Usage: ./array-sorter 20 5 10")
2024-02-02 09:18:21 +01:00
os.Exit(1)
}
for i := 1; i < (len(os.Args)); i++ {
numToAdd, err = strconv.Atoi(os.Args[i])
numArray = append(numArray, float64(numToAdd))
}
if err != nil {
panic(err)
}
fmt.Println("RESULTS:")
fmt.Printf("%v\n", numArray)
2024-02-02 09:18:21 +01:00
fmt.Printf("\nSORTED RESULTS:\n")
sortedArray := sorter(numArray)
fmt.Printf("%v\n", sortedArray)
}
func sorter(arr []float64) (orderedArr []float64) {
if len(arr) == 0 {
return nil
}
var numLocation int
//var numValue float64
sliced_arr := arr
// TODO: get the actual sorting working
for i := 0; i < len(sliced_arr); {
// fmt.Printf("Current slice length: %v\n", sliced_arr)
// fmt.Println(orderedArr)
mini := minmax.Min(sliced_arr)
orderedArr = append(orderedArr, mini)
for i, v := range sliced_arr {
if v == mini {
numLocation = i
//numValue = v
break
}
2024-02-02 09:18:21 +01:00
}
//fmt.Printf("\nDEBUGGING WIZARD:\nThe smallest value is %v and its position is %v\n\n", numValue, numLocation)
sliced_arr = append(sliced_arr[:numLocation], sliced_arr[numLocation+1:]...)
2024-02-02 09:18:21 +01:00
}
return orderedArr
2024-02-02 09:18:21 +01:00
}