1 项目结构

image.png

2 使用方式

  1. // USAGE: sorter i <in> o <out> a <quicksort|bubblesort>
  2. $ ./sorter i in.dat o out.dat a bubblesort

3 代码

(1) sorter.go

  1. package main
  2. import (
  3. "bufio"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "os"
  8. "strconv"
  9. "time"
  10. )
  11. import "sorter/algorithm"
  12. var infile *string = flag.String("i", "infile", "File contains values for sorting")
  13. var outfile *string = flag.String("o", "outfile", "File to receive sorted values")
  14. var algorithmName *string = flag.String("a", "qsort", "Sort algorithm")
  15. func main() {
  16. flag.Parse()
  17. if infile != nil {
  18. fmt.Println("infile =", *infile, "outfile =", *outfile, "algorithm =", *algorithmName)
  19. }
  20. values, err := readValues(*infile)
  21. if err != nil {
  22. fmt.Println(err)
  23. return
  24. }
  25. fmt.Println("Read Values:", values)
  26. t1 := time.Now()
  27. switch *algorithmName {
  28. case "quicksort":
  29. algorithm.QuickSort(values)
  30. case "bubblesort":
  31. algorithm.BubbleSort(values)
  32. }
  33. t2 := time.Now()
  34. fmt.Println("排序程序耗时:", t2.Sub(t1))
  35. writeValues(values, *outfile)
  36. }
  37. func readValues(infile string) (values []int, err error) {
  38. file, err := os.Open(infile)
  39. if err != nil {
  40. fmt.Println("打开infile失败:", infile)
  41. return
  42. }
  43. defer file.Close()
  44. br := bufio.NewReader(file)
  45. values = make([]int, 0)
  46. for {
  47. line, isPrefix, err1 := br.ReadLine()
  48. if err1 != nil {
  49. if err1 != io.EOF {
  50. err = err1
  51. }
  52. break
  53. }
  54. if isPrefix {
  55. fmt.Println("A too long line, seems unexpected.")
  56. return
  57. }
  58. str := string(line) // 把字节数组转为字符串
  59. value, err1 := strconv.Atoi(str) // 字符串 转 整数
  60. if err1 != nil {
  61. err = err1
  62. return
  63. }
  64. values = append(values, value)
  65. }
  66. return
  67. }
  68. func writeValues(values []int, outfile string) error {
  69. file, err := os.Create(outfile)
  70. if err != nil {
  71. fmt.Println("Failed to create outfile", outfile)
  72. return err
  73. }
  74. defer file.Close()
  75. for _, value := range values {
  76. str := strconv.Itoa(value)
  77. file.WriteString(str + "\n")
  78. }
  79. return nil
  80. }

(2) bubblesort.go

  1. package algorithm
  2. func BubbleSort(values []int) {
  3. flag := true
  4. for i := 0; i < len(values)-1; i++ {
  5. flag = true
  6. for j := 0; j < len(values)-i-1; j++ {
  7. if values[j] > values[j+1] {
  8. values[j], values[j+1] = values[j+1], values[j]
  9. flag = false
  10. }
  11. }
  12. if flag {
  13. break
  14. }
  15. }
  16. }

(3) quicksort.go

  1. package algorithm
  2. func quickSort(values []int, left, right int) {
  3. temp := values[left]
  4. p := left
  5. i, j := left, right
  6. for i <= j {
  7. for j >= p && values[j] >= temp {
  8. j--
  9. }
  10. if j >= p {
  11. values[p] = values[j]
  12. p = j
  13. }
  14. if values[i] <= temp && i <= p {
  15. i++
  16. }
  17. if i <= p {
  18. values[p] = values[i]
  19. p = i
  20. }
  21. }
  22. values[p] = temp
  23. if p - left > 1 {
  24. quickSort(values, left, p-1)
  25. }
  26. if right - p > 1 {
  27. quickSort(values, p+1, right)
  28. }
  29. }
  30. func QuickSort(values []int) {
  31. quickSort(values, 0, len(values)-1)
  32. }

(4) in.dat

  1. 123
  2. 3064
  3. 3
  4. 64
  5. 490

4 运行结果

image.png