2024-02-02 09:18:21 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-02-02 10:38:24 +01:00
|
|
|
"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 {
|
2024-02-02 10:38:24 +01:00
|
|
|
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:")
|
2024-02-02 10:38:24 +01:00
|
|
|
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
|
|
|
|
}
|
2024-02-02 10:38:24 +01:00
|
|
|
|
|
|
|
var numLocation int
|
2024-02-03 14:01:11 +01:00
|
|
|
//var numValue float64
|
2024-02-02 10:38:24 +01:00
|
|
|
sliced_arr := arr
|
|
|
|
|
2024-02-03 14:21:38 +01:00
|
|
|
// 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
|
2024-02-03 14:01:11 +01:00
|
|
|
for i := 0; i < len(sliced_arr); {
|
|
|
|
// fmt.Printf("Current slice length: %v\n", sliced_arr)
|
|
|
|
// fmt.Println(orderedArr)
|
|
|
|
|
2024-02-02 10:38:24 +01:00
|
|
|
mini := minmax.Min(sliced_arr)
|
|
|
|
orderedArr = append(orderedArr, mini)
|
|
|
|
|
|
|
|
for i, v := range sliced_arr {
|
2024-02-03 14:01:11 +01:00
|
|
|
if v == mini {
|
2024-02-02 10:38:24 +01:00
|
|
|
numLocation = i
|
2024-02-03 14:01:11 +01:00
|
|
|
//numValue = v
|
|
|
|
break
|
2024-02-02 10:38:24 +01:00
|
|
|
}
|
2024-02-02 09:18:21 +01:00
|
|
|
}
|
2024-02-02 10:38:24 +01:00
|
|
|
|
2024-02-03 14:01:11 +01:00
|
|
|
//fmt.Printf("\nDEBUGGING WIZARD:\nThe smallest value is %v and its position is %v\n\n", numValue, numLocation)
|
2024-02-02 10:38:24 +01:00
|
|
|
|
2024-02-03 14:01:11 +01:00
|
|
|
sliced_arr = append(sliced_arr[:numLocation], sliced_arr[numLocation+1:]...)
|
2024-02-02 09:18:21 +01:00
|
|
|
}
|
2024-02-02 10:38:24 +01:00
|
|
|
return orderedArr
|
2024-02-02 09:18:21 +01:00
|
|
|
}
|