array-sorter/main.go

70 lines
1.5 KiB
Go

package main
import (
"fmt"
"git.bulgariu.xyz/raul/min-max-array"
"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")
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)
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
// IT WAS THIS BASTARD WHO KEPT CAUSING OUT OF BOUNDS ERRORS
// I KEPT READING THE LENGTH OF THE ORIGINAL ARRAY INSTEAD OF THE
// ARRAY BEING SLOWLY EMPTIED
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
}
}
//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:]...)
}
return orderedArr
}