关于golang:go-实现排序算法

38次阅读

共计 3535 个字符,预计需要花费 9 分钟才能阅读完成。

go 十个数组排序办法

package sufa

import (
   "fmt"
   "math"
   "math/rand"
   "strconv"
   "time"
)

func Main2() {var li []int
   var r int
   var start, end int64
   //li = []int{90, 23, 433, 343, 53, -1, 56, 7, 7, 8, 6, 45, 4, 5, 66, 7}
   li = []int{}
   for i := 0; i < 10; i++ {r = rand.Intn(100)
      li = append(li, r)
   }

   //for i := len(li)-1; i >= 0; i-- {////   fmt.Println(i, li[i])
   // var t = li[0]
   // for j := 1; j < len(li); j++ {//    if li[j] < li[j-1] {//       t = li[j-1]
   //       li[j-1] = li[j]
   //       li[j] = t
   //    }
   // }
   //}
   fmt.Println(li)
   start = time.Now().UnixNano()

   //BubbleSort(li)
   //SelectSort(li)
   //InsertSort(li)
   //ShellSort(li)
   //MergeSort(li)
   //QuickSort(li)
   //CountSort(li)
   BucketSort(li)
   //Reverse(li)

   //ExChange(li, 0,1)
   end = time.Now().UnixNano()

   fmt.Println(li)
   fmt.Println(end - start)

}

// 冒泡排序
func BubbleSort(li []int) {for i := len(li) - 1; i >= 0; i-- {// fmt.Println(i, li[i])
      var t = li[0]
      for j := 1; j < len(li); j++ {if li[j] < li[j-1] {t = li[j-1]
            li[j-1] = li[j]
            li[j] = t
         }
      }
      fmt.Print(i, "\r")
   }
}
// 抉择排序
func SelectSort(li []int) {for i := 0; i < len(li); i++ {
      var minIndex int = i
      for j := i; j < len(li); j++ {if li[j] < li[minIndex] {minIndex = j}
      }
      ExChange(li, i, minIndex)
   }

}
插入排序
func InsertSort(li []int) {for i := 1; i < len(li); i++ {var this int = li[i]
      for j := i - 1; j >= 0; j-- {if li[j+1] < li[j] {ExChange(li, j, j+1)
            continue
         }
         if li[j+1] >= li[j] {li[j+1] = this
            break
         }
      }
   }
}
// 希尔排序
func ShellSort(li []int) {var l = len(li)
   var gap = l / 2
   if l <= 1 {return}
   for true {
      if gap == 1 {InsertSort(li)
         break
      }
      for i := 0; i < gap; i++ {
         var index int
         var group []int = make([]int, 0)
         var groupIdList []int = make([]int, 0)
         //group = append(group, li[i])
         //groupIdList = append(groupIdList, i)
         for j := 0; j < l; j++ {
            index = i + j*gap
            if index >= l {break}
            group = append(group, li[index])
            groupIdList = append(groupIdList, index)
         }
         InsertSort(group)

         for j := 0; j < len(groupIdList) && j < len(group); j++ {
            var ind int
            if len(groupIdList) <= 1 || len(group) <= 1 {break}
            ind = groupIdList[j]
            li[ind] = group[j]
         }
      }

      gap = gap / 2
   }

}

func MergeSort(li []int) {//merge(li, 0, len(li))

   type funcType func(li []int, left, right int)
   var f funcType
   f = func(li []int, left, right int) {var middle = (left + right) / 2
      if right-left <= 1 {InsertSort(li[left:right])
         return
      }

      f(li, left, middle)
      f(li, middle, right)

      SelectSort(li[left:middle])
      SelectSort(li[middle:right])
   }
   f(li, 0, len(li))
}
// 疾速排序
func QuickSort(li []int) {//qu(li, 0, len(li))
   if len(li) <= 1 {return}

   type funcType func(li []int, left, right int)
   var f funcType
   f = func(li []int, left, right int) {var pivot int = (right + left) / 2
      if right-left <= 1 {InsertSort(li[left:right])
         return
      }

      f(li, left, pivot)
      f(li, pivot, right)
      //fmt.Println(li[left:right], li[pivot])

      for i := pivot - 1; i >= left; i-- {if li[i] >= li[pivot] {
            for j := i; j < pivot; j++ {ExChange(li, j, j+1)
               //if j+1 == pivot {
               // pivot -= 1
               //}
            }
            pivot -= 1

         }
      }

      for i := right - 1; i > pivot; i-- {if li[i] <= li[pivot] {
            for j := i; j > pivot; j-- {ExChange(li, j, j-1)
            }
            pivot += 1

         }
      }

      SelectSort(li[left:pivot])
      SelectSort(li[pivot:right])

      //fmt.Println(li[left:right], li[pivot])

   }
   f(li, 0, len(li))

}
// 计数排序
func CountSort(li []int) {
   var (min = li[0]
      max = li[0]
      ma  []int
      res []int)
   for i := 0; i < len(li); i++ {if li[i] <= min {min = li[i]
      }
      if li[i] >= max {max = li[i]
      }
   }
   ma = make([]int, max+1)
   for i := 0; i < len(li); i++ {ma[li[i]] += 1
   }

   res = make([]int, 0)
   for i := 0; i < len(ma); i++ {if ma[i] != 0 {for j := 0; j < ma[i]; j++ {res = append(res, i)
         }
      }
   }
   for i := 0; i < len(li); i++ {li[i] = res[i]
   }

   //fmt.Println(ma, min, max, res)

}
// 桶排序
func BucketSort(li []int) {var max = li[0]
   var maxNumb = 0
   for i := 0; i < len(li); i++ {if li[i] >= max {max = li[i]
      }
   }
   var data = strconv.Itoa(max)
   maxNumb = len(data)

   for i := 0; i < maxNumb; i++ {var l = make([][]int, len(li)*2)
      var n = 0
      for j := 0; j < len(li); j++ {n = getNumb(li[j], i)
         //fmt.Println(n, li[j], i)
         l[n] = append(l[n], li[j])
      }

      var index = 0
      for j := 0; j < 10; j++ {if len(l[j]) > 0 {for k := 0; k < len(l[j]); k++ {li[index] = l[j][k]
               index++
            }
         }
      }
   }

}
// 失去数字的第几位
func getNumb(num, index int) int {return (num / int(math.Pow(10, float64(index)))) % 10
}
// 替换数组中的两个数
func ExChange(li []int, x, y int) {
   var t int
   t = li[x]
   li[x] = li[y]
   li[y] = t
}
// 逆序
func Reverse(li []int) {for i := 0; i < len(li)/2; i++ {ExChange(li, i, len(li)-i-1)
   }
}

正文完
 0