Moved array-sorter into its own repo
I'm real proud of this one so I'm moving it out of golang-exercises and creating a repo for it to live in
This commit is contained in:
parent
13a606ddc9
commit
d73ff9618a
|
@ -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
|
10
README.md
10
README.md
|
@ -1,3 +1,13 @@
|
|||
# array-sorter
|
||||
|
||||
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
|
||||
|
|
|
@ -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)
|
|
@ -0,0 +1,5 @@
|
|||
module array-sorter
|
||||
|
||||
go 1.21.6
|
||||
|
||||
require git.bulgariu.xyz/raul/min-max-array v0.0.0-20240131105739-41c2fcc5af70
|
|
@ -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=
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue