sort.go

  1. package sort
  2. type Sorter interface {
  3. Len() int
  4. Less(i, j int) bool
  5. Swap(i, j int)
  6. }
  7. func Sort(data Sorter) {
  8. for pass := 1; pass < data.Len(); pass++ {
  9. for i := 0; i < data.Len()-pass; i++ {
  10. if data.Less(i+1, i) {
  11. data.Swap(i, i+1)
  12. }
  13. }
  14. }
  15. }
  16. func IsSorted(data Sorter) bool {
  17. n := data.Len()
  18. for i := n - 1; i > 0; i-- {
  19. if data.Less(i, i-1) {
  20. return false
  21. }
  22. }
  23. return true
  24. }
  25. // Convenience types for common cases
  26. type IntArray []int
  27. func (p IntArray) Len() int { return len(p) }
  28. func (p IntArray) Less(i, j int) bool { return p[i] < p[j] }
  29. func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  30. type StringArray []string
  31. func (p StringArray) Len() int { return len(p) }
  32. func (p StringArray) Less(i, j int) bool { return p[i] < p[j] }
  33. func (p StringArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  34. // Convenience wrappers for common cases
  35. func SortInts(a []int) { Sort(IntArray(a)) }
  36. func SortStrings(a []string) { Sort(StringArray(a)) }
  37. func IntsAreSorted(a []int) bool { return IsSorted(IntArray(a)) }
  38. func StringsAreSorted(a []string) bool { return IsSorted(StringArray(a)) }

main.go

  1. package main
  2. import (
  3. "fmt"
  4. "./sort"
  5. )
  6. func ints() {
  7. data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
  8. a := sort.IntArray(data) //conversion to type IntArray
  9. sort.Sort(a)
  10. if !sort.IsSorted(a) {
  11. panic("fails")
  12. }
  13. fmt.Printf("The sorted array is: %v\n", a)
  14. }
  15. func strings() {
  16. data := []string{"monday", "friday", "tuesday", "wednesday", "sunday", "thursday", "", "saturday"}
  17. a := sort.StringArray(data)
  18. sort.Sort(a)
  19. if !sort.IsSorted(a) {
  20. panic("fail")
  21. }
  22. fmt.Printf("The sorted array is: %v\n", a)
  23. }
  24. type day struct {
  25. num int
  26. shortName string
  27. longName string
  28. }
  29. type dayArray struct {
  30. data []*day
  31. }
  32. func (p *dayArray) Len() int { return len(p.data) }
  33. func (p *dayArray) Less(i, j int) bool { return p.data[i].num < p.data[j].num }
  34. func (p *dayArray) Swap(i, j int) { p.data[i], p.data[j] = p.data[j], p.data[i] }
  35. func days() {
  36. Sunday := day{0, "SUN", "Sunday"}
  37. Monday := day{1, "MON", "Monday"}
  38. Tuesday := day{2, "TUE", "Tuesday"}
  39. Wednesday := day{3, "WED", "Wednesday"}
  40. Thursday := day{4, "THU", "Thursday"}
  41. Friday := day{5, "FRI", "Friday"}
  42. Saturday := day{6, "SAT", "Saturday"}
  43. data := []*day{&Tuesday, &Thursday, &Wednesday, &Sunday, &Monday, &Friday, &Saturday}
  44. a := dayArray{data}
  45. sort.Sort(&a)
  46. if !sort.IsSorted(&a) {
  47. panic("fail")
  48. }
  49. for _, d := range data {
  50. fmt.Printf("%s ", d.longName)
  51. }
  52. fmt.Printf("\n")
  53. }
  54. func main() {
  55. ints()
  56. strings()
  57. days()
  58. }

output

  1. The sorted array is: [-5467984 -784 0 0 42 59 74 238 905 959 7586 7586 9845]
  2. The sorted array is: [ friday monday saturday sunday thursday tuesday wednesday]
  3. Sunday Monday Tuesday Wednesday Thursday Friday Saturday