Go Example 1: file read and write and sort (fast bubbling)

Source: Internet
Author: User
Tags sort


Package main import ("Algorithm/bubblesort" "Algorithm/qsort" "Bufio" "Flag" "FMT" "IO" "OS" "StrConv" "Time") 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) fmt. Print (str, "") 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 ("Failed to create the output file", 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 {fmt. PRINTLN (Err)} else {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 unsupported.") } t2: = time. Now () Fmt. Println ("The sorting process costs", T2.

		Sub (T1), "to complete.") Writevalues (values, *outfile)}}
Package Bubblesort

func bubblesort (values []int) {for
	I: = 0, I < len (values)-1; i++ {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]}
	}}

Package Qsort

func quickSort (values []int, left, right int) {
	Temp: = Values[left]
	p: = left
	I, j: = Le FT, right-

	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 & lt;= p {
			values[p] = values[i]
			p = i
		}
	}

	values[p] = temp  //provkey
	if p-left > 1 { C32/>quicksort (values, left, p-1)
	}
	if right-p > 1 {
		quickSort (values, p+1, right)
	}
}

func QuickSort (values []int) {
	QuickSort (values, 0, Len (values)-1)
}





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.