普通网友 2017-11-05 14:37
浏览 31
已采纳

在Golang中使用指针遇到麻烦

I am new to golang, here I am using BubleSort and InsertionSort and generating a rando slice for the functions. Can I use pointers some how to hand both functions the unsorted slice? because when I run the program the first function sorts the slice and the the second function uses that sorted slice. I know there are different ways to give both functions the unsorted slice, but I want to see how can I use pointers to do this. Thank you.

package main

import (
"fmt"

"math/rand"

"time"
)

func main() {
slice := generateSlice(4)
fmt.Println(BubleSort(slice))
fmt.Println(InsertionSort(slice))
}

func generateSlice(size int) []int {
slice := make([]int, size, size)
rand.Seed(time.Now().UnixNano())
for i := 0; i < size; i++ {
    slice[i] = rand.Intn(10)
}
return slice
}

func BubleSort(slice []int) []int {
fmt.Println("unsorted Buble", slice)
for i := 1; i <= len(slice); {
    for j := 0; j < len(slice)-i; {
        if slice[j] > slice[j+1] {
            slice[j], slice[j+1] = slice[j+1], slice[j]
        }
        j++
    }
    i++
}
return slice
}

func InsertionSort(slice []int) []int {
fmt.Println("unsorted Insertion", slice)
for i := 1; i <= len(slice)-1; {
    // Check j and j-1 and swap the smaller number to left in each 
itteartion to reach the first 2 elements of the slice
    for j := i; j >= 1; {
        if slice[j] < slice[j-1] {
            slice[j], slice[j-1] = slice[j-1], slice[j]
        }
        j--
    }
    i++
}
return slice
}
  • 写回答

2条回答 默认 最新

  • doupingtang9627 2017-11-05 15:38
    关注

    Andy is correct

    As an alternative, you can copy the slice before sorting it, as in here (on Play):

    package main
    
    import (
        "fmt"
    
        "math/rand"
    
        "time"
    )
    
    func main() {
        slice := generateSlice(4)
        fmt.Println(BubleSort(copySlice(slice)))
        fmt.Println(InsertionSort(copySlice(slice)))
    }
    
    func copySlice(src []int) []int {
        dest := make([]int, len(src))
        copy(dest, src)
        return dest
    }
    func generateSlice(size int) []int {
        slice := make([]int, size, size)
        rand.Seed(time.Now().UnixNano())
        for i := 0; i < size; i++ {
            slice[i] = rand.Intn(10)
        }
        return slice
    }
    
    func BubleSort(slice []int) []int {
        fmt.Println("unsorted Buble", slice)
        for i := 1; i <= len(slice); {
            for j := 0; j < len(slice)-i; {
                if slice[j] > slice[j+1] {
                    slice[j], slice[j+1] = slice[j+1], slice[j]
                }
                j++
            }
            i++
        }
        return slice
    }
    
    func InsertionSort(slice []int) []int {
        fmt.Println("unsorted Insertion", slice)
        for i := 1; i <= len(slice)-1; {
            // Check j and j-1 and swap the smaller number to left in each itteartion to reach the first 2 elements of the slice
            for j := i; j >= 1; {
                if slice[j] < slice[j-1] {
                    slice[j], slice[j-1] = slice[j-1], slice[j]
                }
                j--
            }
            i++
        }
        return slice
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口