diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dcc4674 --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +# ---> Go +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib +*.txt + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work diff --git a/README.md b/README.md index b7396db..d2ee797 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,13 @@ # array-sorter -Fun tool I created to sort arrays in ascending order \ No newline at end of file +Fun tool I created to sort arrays in ascending order + +## Usage +``` +git clone https://git.bulgariu.xyz/raul/array-sorter +cd array-sorter +./gen-numbers.sh +``` + +gen-numbers.sh generates 300 random numbers and feeds them to array-sorter as input, you can modify +the script to increase the amount of numbers to sort diff --git a/gen-numbers.sh b/gen-numbers.sh new file mode 100755 index 0000000..8f71604 --- /dev/null +++ b/gen-numbers.sh @@ -0,0 +1,13 @@ +#!/bin/bash +FILE="./array-sorter" + +echo {1..300} > /tmp/numbers.txt +cat /tmp/numbers.txt | tr ' ' "\\n" | shuf | tr "\\n" ' ' > /tmp/random.txt +if test -f ./array-sorter; then + echo "$FILE exists, running" + +else + echo "$FILE doesn't exist, building" + go build -o $FILE . +fi +./array-sorter $(cat /tmp/random.txt) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0399acc --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module array-sorter + +go 1.21.6 + +require git.bulgariu.xyz/raul/min-max-array v0.0.0-20240131105739-41c2fcc5af70 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..73bf989 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +git.bulgariu.xyz/raul/min-max-array v0.0.0-20240131105739-41c2fcc5af70 h1:q/uQwkakAZnaus16KHG6UkBFZzNaMbjY3SCZjh0Pytk= +git.bulgariu.xyz/raul/min-max-array v0.0.0-20240131105739-41c2fcc5af70/go.mod h1:PdMyknNgNZYKg03bYhSc4aaSA/P3yScGPfxC4z+ju0c= diff --git a/main.go b/main.go new file mode 100644 index 0000000..4f857a7 --- /dev/null +++ b/main.go @@ -0,0 +1,69 @@ +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 +}