Compare commits
No commits in common. "4c2c6ef429c002cf04e56f24ef5991f16ce9959a" and "6ea704fe5b581c3cd0854ab1b3c1e53b2e578731" have entirely different histories.
4c2c6ef429
...
6ea704fe5b
@ -1,62 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
inBytes, err := os.ReadFile(os.Args[1])
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
inLines := strings.Split(strings.TrimSpace(string(inBytes)), "\n")
|
||||
inInts := make([]int64, len(inLines))
|
||||
|
||||
for i, line := range inLines {
|
||||
v, err := strconv.ParseInt(line, 0, 64)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
inInts[i] = v
|
||||
}
|
||||
|
||||
if err := insertionSort(inInts); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
for _, i := range inInts {
|
||||
fmt.Printf("%v\n", i)
|
||||
}
|
||||
}
|
||||
|
||||
func insertionSort(inInts []int64) error {
|
||||
if len(inInts) < 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for j := 1; j < len(inInts); j += 1 {
|
||||
key := inInts[j]
|
||||
fmt.Fprintf(os.Stderr, "%+v (save %v from %v)\n", inInts, key, j)
|
||||
|
||||
i := j - 1
|
||||
|
||||
for ; i > -1 && inInts[i] > key; i -= 1 {
|
||||
inInts[i+1] = inInts[i]
|
||||
|
||||
fmt.Fprintf(os.Stderr, "%+v (j=%v i=%v)\n", inInts, j, i)
|
||||
}
|
||||
|
||||
inInts[i+1] = key
|
||||
fmt.Fprintf(os.Stderr, "%+v (insert %v at %v)\n", inInts, key, i+1)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def main() -> int:
|
||||
in_ints: list[int] = []
|
||||
with open(sys.argv[1]) as infile:
|
||||
for line in infile:
|
||||
in_ints.append(int(line.strip()))
|
||||
|
||||
insertion_sort(in_ints)
|
||||
|
||||
for i in in_ints:
|
||||
print(i)
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def insertion_sort(in_ints: list[int]) -> None:
|
||||
for i in range(len(in_ints) - 1):
|
||||
j = i + 1
|
||||
key = in_ints[j]
|
||||
|
||||
print(f"{in_ints} (save {key} from {j})", file=sys.stderr)
|
||||
|
||||
while i > -1 and in_ints[i] > key:
|
||||
in_ints[i + 1] = in_ints[i]
|
||||
|
||||
print(f"{in_ints} (j={j} i={i})", file=sys.stderr)
|
||||
|
||||
i -= 1
|
||||
|
||||
in_ints[i + 1] = key
|
||||
print(f"{in_ints} (insert {key} at {i+1})", file=sys.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
@ -1,10 +0,0 @@
|
||||
11
|
||||
2
|
||||
17
|
||||
-1
|
||||
34
|
||||
8
|
||||
999
|
||||
2
|
||||
-56
|
||||
3
|
Loading…
Reference in New Issue
Block a user