0

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 Answers 2

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

1

I am learning Go as well. I think the fact that you are using slice is what causing the unwanted behaviour (when I run the program the first function sorts the slice and the the second function uses that sorted slice). As explained here:

A slice does not store any data, it just describes a section of an underlying array.

Changing the elements of a slice modifies the corresponding elements of its underlying array.

Other slices that share the same underlying array will see those changes.

I don't think passing pointer will produce different results as slices point to the same array. What you can do is maybe receive an array instead of slice?

Hope it helps! :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.