Go language Implementation 7 Big sort algorithm

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport (     FMT      "Math/rand"      "Time"     //"OS"     //"os/signal") const (    num      = 100000   & Nbsp;rangenum = 100000) func main () {    randseed: = Rand. New (Rand. Newsource (time. Now (). Unix () + time. Now (). Unixnano ()))     var buf []int    for I: = 0; i < num; i++ {        buf = append (buf, RANDSEED.INTN (rangenum))     }    t: = time. Now ()     //bubble sort     //Maopao (buf)     //Select sort     //Xuanze (BUF)     //Insert sort     //Charu (buf)     //Hill sort     //xier ( BUF)     //Quick Sort     //kuaisu (buf)     //merge sort     //guibing (BUF)     //heap sort     duipai (buf)     //FMT. Println (BUF)     fmt. Println (time. Since (t))     //wait to exit     //c: = Make (chan os. Signal, 1)     //Signal. Notify (c, OS. Interrupt, OS. Kill)     //<-c    //fmt. Println ("Receive ctrl-c")}//bubble sort func Maopao (buf []int) {    times: = 0    for I: = 0; I &lt ; Len (BUF)-1; i++ {        flag: = false        for J: = 1; j < Len (BUF)- I J + + {            if buf[j-1] > Buf[j] {       & nbsp;        times++                &NBSP;TMP: = buf[j-1]                buf[j-1] = buf[ j]                buf[j] = tmp        &NBSP;&NBsp;       flag = true            }   & nbsp;    }        if!flag {             break        }    }    fmt. Println ("Maopao times:", times)}//Select sort func xuanze (buf []int) {    times: = 0    for I: = 0 ; I < Len (buf)-1; i++ {        min: = i        for J: = i; j < Len (BUF); J + + {            times++            if Buf[min] > buf[j] {                min = j             }        }         if min! = i {            tmp: = buf[i]            buf[i] = buf[ min]            buf[min] = tmp        }     }    fmt.  Println ("Xuanze times:", times)}//Insert sort func Charu (buf []int) {    times: = 0    for I: = 1; I < Len (BUF); i++ {        for J: = i; j > 0; j--{           & Nbsp;if Buf[j] < buf[j-1] {                times++                 tmp: = buf[j-1]     & nbsp          buf[j-1] = buf[j]                buf[j] = tmp            } else {               break             }        }    }    fmt. Println ("Charu times:", Times)}//Hill sort func xier (buf []int) {    times: = 0    tmp: = 0&nbsp ;   length: = Len (buf)     incre: = length    //fmt. Println ("BUF:", buf)     for {        incre/= 2     & nbsp  for k: = 0; K < Incre; k++ {//based on increments divided into sub-sequences             for I: = k + incre; i < length; i + = Incre {                for J: = i; j > k; J-= Incre { &nbsp ;                  //fmt. Println ("J:", J, "Data:", Buf[j], "J-incre:", J-incre, "Data:", Buf[j-incre]) &Nbsp;                   times++                    if buf[j] < Buf[j-incre] { & nbsp                      tmp = buf[ j-incre]                        buf[j-incre] = buf[j]                   & nbsp;    buf[j] = tmp                     } else {                   & nbsp;    break                     }           &NBSP;&NBSP;&NBsp  }                //fmt. Println ("Middle:", buf)             }            //FMT. Println ("outer:", buf)         }        //fmt. PRINTLN ("outer outer:", buf, "Incre:", incre)         if incre = = 1 {    &n bsp;       break        }    }    //FMT. Println ("After:", buf)     fmt. Println ("Xier times:", times)}//Quick sort func kuaisu (buf []int) {    kuai (buf, 0, Len (buf)-1)}func Kuai (A []in T, l, R int) {    if l >= R {        return    } &nbsp ;  i, J, key: = L, R, A[l]//Select the first number is key    for i < j {         For i < J && A[j] > key {//right-to-left find the first value less than key             j--&nbs p;       }        if i < J {       & nbsp;    a[i] = a[j]            i++         }        for i < J && A[i] < key {//left to right find the first value greater than key  & nbsp          i++        }         if i < J {            a[j] = a[i]   &NBSP;&NBSP;&N Bsp      j--        }    }    //i = = J     a[i] = key    kuai (A, L, i-1)     kuai (A, i+1, R)}//merge sort func guibing (buf []int] {    tmp: = Make ([]int, Len (buf))     merge_sort (buf, 0, Len (buf)-1, TMP)}func merge_sort (A []int, first, last int, tmp []int) {&NB sp;   if First < last {        middle: = (first + last)/2    & nbsp;   merge_sort (A, first, middle, TMP)      //left half sequence          Merge_sort (A, middle+1, last, TMP)      //right half order         mergearray (A, First, middle, last, TMP)//merge left and right parts     }}func Mergearray (A []int, first, middle, end int, tmp []int] {     //FMT. Printf ("Mergearray A:%v, first:%v, Middle:%v, End:%V, tmp:%v\n",    //    a, First, MI Ddle, END, TMP)     i, M, J, N, K: = First, middle, middle+1, end, 0    for i <= m &AMP;&A mp J <= N {        if a[i] <= a[j] {           &N bsp;tmp[K] = a[i]            k++             i++        } else {            tmp[k] = a[j]            k++             j++        }    }    for i <= m {         tmp[k] = a[i]        k++         i++    }    for j <= N {        tmp[k] = a[j]         k++        j++    }    for II: = 0; II < K; ii++ {        a[first+ii] = tmp[ii]    }    //fmt. Printf ("Sort:buf:%v\n", a)}//heap sort func Duipai (bUF []int) {    temp, N: = 0, Len (buf)     for I: = (n-1)/2; I >= 0; i--{        minheapfixdown (buf, I, N)     }    for i: = n-1; i > 0; i--{        temp = buf[0]        buf[0] = buf[i]         buf[i] = temp        minheapfixdown (buf, 0, i)       }}func Minheapfixdown (a []int, I, n int) {    j, temp: = 2*i+1, 0    for J < n {        if j+1 < n && a[j+1] < A[j] {       &nbsp ;    j++        }        if a[i] <= a[j] {            break        }        temp = a[i]        a[i] = a[j]        a[j] = temp        i = j         j = 2*i + 1    }}

 

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.