Go for bubbling sorting and quick sorting

Source: Internet
Author: User
Tags array to string

Project structure

Bubble sort algorithm, source file Bubblesort.go

package bubblesort// 冒泡排序func BubbleSort(values []int)  {    for i := 0; i < len(values) - 1; i ++ {        flag := true        for j := 0; j < len(values) - i - 1; i ++ {            if values[j] > values[j + 1] {                values[j], values[j + 1] = values[j + 1], values[j]                flag = false            }            if flag == true {                break            }        }    }}

Quick Sort algorithm, source file 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)}

Master File Main.go

Package Mainimport ("Flag" "OS" "Bufio" "io" "StrConv" "Time" "Sorter/algorithms/qsort" "sorter/a Lgorithms/bubblesort "FMT") var infile *string = flag. String ("I", "Unsorted.dat", "File contains values of sorting") var outfile *string = flag. String ("O", "Sorted.dat", "File to receive sorted values") var algorithm *string = flag. String ("A", "Qsort", "Sort Algorithm")//Read value from file func readvalues (infile string) (values []int, err Error) {file, err: = OS. Open (infile) if err! = Nil {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 {println ("A too long            Line, seems unexpected. ") return} str: = string (line)//convert character array to string value, ERR1:= StrConv.    Atoi (str) if err1! = Nil {err = err1 return} values = Append (values, value) return}//writes the value to the file in Func writevalues (values []int, outfile String) error {file, err: = OS. Create (outfile) if err! = Nil {println ("Failed to create the ouput file", outfile) return err} D Efer file. Close () for _, Value: = range values {str: = StrConv. Itoa (value) file. WriteString (str + "\ n")} return Nil}func main () {flag.    Parse () if infile! = nil {println ("infile =", *infile, "outfile=", *outfile, "algorithm =", *algorithm)} Values, err: = Readvalues (*infile) If Err = = Nil {fmt. Println ("Read values:%v", values) if Err = = Nil {T1: = time. Now () switch *algorithm {case "qsort": qsort. QuickSort (values) case "Bubblesort": Bubblesort. Bubblesort (values) default:printlN ("Sorting algorithm", *algorithm, "is either unknown or unsupported.") } t2: = time. Now () println ("The Sorting process conts", T2.            Sub (T1), "to complete.") Writevalues (values, *outfile)}}else{println (Err)}}

Sort results

Go for bubbling sorting and quick sorting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.