GO语言练习:第一个Go语言工程--排序
1、代码
2、编译
3、运行
1、代码框架 /home/fengbo/sorter
$ tree
.
├── bin
├── pkg
├── readme.txt
└── src
├── algorithms
│ ├── bubblesort
│ │ ├── bubblesort.go
│ │ └── bubblesort_test.go
│ └── qsort
│ ├── qsort.go
│ └── qsort_test.go
└── sorter
└── sorter.go
1.1、src/algorithms/bubblesort/bubblesort.go
//bubblesort.go
package bubblesort
func BubbleSort(values []int) {
flag := true
for i := 0; i < len(values) - 1; i++ {
flag = true
for j := 0; j < len(values) - i - 1; j++ {
if values[j] > values[j + 1] {
values[j], values[j + 1] = values[j + 1], values[j]
flag = false
}
if flag == true {
break;
}
}
}
}
1.2、src/algorithms/bubblesort/bubblesort_test.go
//bubble_test.ho
package bubblesort
import "testing"
func TestBubbleSort1(t * testing.T) {
values := []int {5, 4, 3, 2, 1}
BubbleSort(values)
for i := 0; i < len(values) - 1; i++ {
if values[i] > values[i + 1] {
t.Error("ubbleSort() faild. Got at ", values[i], values[i + 1])
break
}
}
}
func TestBubbleSort2(t * testing.T) {
values := []int{5}
BubbleSort(values)
if(values[0] != 5) {
t.Error("BubbleSort() faild. Got ", values, "Excepted 5")
}
}
1.3、src/algorithms/qsort/qsort.go
//qsort.go
package qsort
func quickSort(values []int, left, right int) {
temp := values[left]
p := left
i, j := left, right
for i <= j {
for j >= p && values[j] >= temp {
j--
}
if j >= p {
values[p] = values[j]
p = j
}
if values[i] <= temp && i <= p {
i++
}
if i <= p {
values[p] = values[i]
p = i
}
}
values[p] = temp
if p - left > 1 {
quickSort(values, left, p - 1)
}
if right - p > 1 {
quickSort(values, p + 1, right)
}
}
func QuickSort(values []int) {
quickSort(values, 0 , len(values) - 1)
}
1.4、src/algorithms/qsort/qsort_test.go
//qsort_test.go
package qsort
import "testing"
func TestQuickSort1(t * testing.T) {
values := []int {5, 4, 3, 2, 1}
QuickSort(values)
var i int;
var j int = len(values) - 1
for i = 0; i < j; i++ {
if values[i] > values[i + 1] {
t.Error("QuickSort() faild")
break;
}
}
}
func TestQuickSort2(t * testing.T) {
values := []int {5}
QuickSort(values)
if values[0] != 5 {
t.Error("QuickSort() faild. Got", values);
}
}
1.5、src/sorter/sorter.go
package main
import "bufio"
import "flag"
import "fmt"
import "io"
import "os"
import "strconv"
import "time"
import "algorithms/bubblesort"
import "algorithms/qsort"
var infile * string = flag.String("i", "unsorted.dat" , "File contains values for sorting")
var outfile * string = flag.String("o", "sorted.dat" , "File to receive sorted values")
var algorithm * string = flag.String("a", "qsort" , "Sort algorithm")
func readValues(infile string)(values []int, err error){
file, err := os.Open(infile)
if err != nil{
fmt.Println("Failed to open the input file :", infile)
return
}
defer file.Close()
br := bufio.NewReader(file)
values = make([]int, 0)
for{
line, isPrefix, err1 := br.ReadLine()
if err1 != nil{
if err1 != io.EOF{
err = err1
}
break
}
if isPrefix {
fmt.Println("A too long line, seems unexpected.")
return
}
str := string(line)
value, err1 := strconv.Atoi(str)
if err1 != nil{
err = err1
return
}
values = append(values, value)
}
return
}
func writeValues(values []int, outfile string) error {
file, err := os.Create(outfile)
if err != nil {
fmt.Println("Faild to create the outfile ", outfile)
return err
}
defer file.Close()
for _, value := range values {
str := strconv.Itoa(value)
file.WriteString(str + "\n")
}
return nil
}
func main(){
flag.Parse()
if infile != nil {
fmt.Println("infile =", *infile, "outfile = ", *outfile, "algorithm = ", *algorithm)
}
values, err := readValues(*infile)
if err == nil {
t1 := time.Now()
switch *algorithm {
case "qsort" :
qsort.QuickSort(values)
case "bubblesort" :
bubblesort.BubbleSort(values)
default :
fmt.Println("Sorting algorithm", * algorithm, "is either unknown or ensupported.")
}
t2 := time.Now()
fmt.Println("The sorting process costs", t2.Sub(t1), "to complete.")
writeValues(values, * outfile)
}else {
fmt.Println(err)
}
}
2、编译
2.1、设置环境变量
[fengbo@sorter]$ pwd
/home/fengbo/sorter
[fengbo@sorter]$ export GOPATH="/home/fengbo/sorter"
2.1编译级安装
$ go build algorithms/bubblesort
$ go build algorithms/qsort
$ go install algorithms/bubblesort
$ go install algorithms/qsort
$ go build sorter
2.2、编译及安装后的目录结构
$ tree
.
├── bin
├── pkg
│ └── linux_386
│ └── algorithms
│ ├── bubblesort.a
│ └── qsort.a
├── readme.txt
├── sorter
└── src
├── algorithms
│ ├── bubblesort
│ │ ├── bubblesort.go
│ │ └── bubblesort_test.go
│ └── qsort
│ ├── qsort.go
│ └── qsort_test.go
└── sorter
└── sorter.go
3、运行
3.1、未排序的文本文件
$ cat unsorted.dat
6
345
76564
5
66
443654
757
2365
565
3.2、运行排序程序
$ ./sorter -i unsorted.dat -o sorted.dat -a qsort
infile = unsorted.dat outfile = sorted.dat algorithm = qsort
The sorting process costs 2.22us to complete.
3.3、排序后的结果文件
$ ls
sorted.dat sorter unsorted.dat
$ cat sorted.dat
5
6
66
345
565
757
2365
76564
443654
4、注释
代码来源于《Go语言编程》一书的第二章,目录结构与原书有些差异。