Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab 2: Bubble Sort #534

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions 02_bubble/nicholsk/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"
"io"
"os"
)

var out io.Writer = os.Stdout

func main() {
fmt.Fprintln(out, bubble([]int{3, 2, 1, 5}))
}

func bubble(s []int) []int {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary blank line.
No need for blank lines at beginning or end of block.
Use blank lines between type, method and function definitions and within a block for logical separation.

// track whether the sort is complete
sorted := false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need sorted and swaps; swaps alone should suffice.


// make a slice copy to perform sort on
sCopy := s
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why you need a variable to make a copy over here.


for !sorted {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically bubble sort has to nested loops.
After the first iteration of the outer loop the largest element has "bubbled" up to the highest index position.
Therefore no more comparisons with the highest index position need to be executed:

	for i := len(s); i > 0; i-- {
		for j := 1; j < i; j++ {
			if s[j-1] > s[j] {
				s[j-1], s[j] = s[j], s[j-1]
			}
		}
	}

you can add in an early bail when nothing has been swapped ie the slice is already sorted:

	for i := len(s); i > 0; i-- {
		swapped := false
		for j := 1; j < i; j++ {
			if s[j-1] > s[j] {
				s[j-1], s[j] = s[j], s[j-1]
				swapped = true
			}
		}
		if !swapped {
			// If no elements were swapped, the slice is sorted so we can stop here
			return
		}
	}


// track whether any swaps took place in current iteration
swaps := false

// iterate every index in the list, starting at index 1
for i := 1; i < len(s); i++ {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why len(s) and why not len(sCopy) ?


// compare element to previous
if sCopy[i-1] > sCopy[i] {

// if less than previous then swap elements
sCopy[i], sCopy[i-1] = sCopy[i-1], sCopy[i]
swaps = true
}

}

// an iteration with no swaps indicates the array is sorted
if !swaps {
sorted = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return or break here

}
}

return sCopy
}
57 changes: 57 additions & 0 deletions 02_bubble/nicholsk/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"bytes"
"math/rand"
"sort"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
)

func TestBubbleSortMain(t *testing.T) {
var buf bytes.Buffer
out = &buf

main()

expected := strconv.Quote("[1 2 3 5]\n")
actual := strconv.Quote(buf.String())

if expected != actual {
t.Errorf("Unexpected output in main()")
}
}

func TestBubbleWithSmallSlice(t *testing.T) {
a := assert.New(t)

sorted := bubble([]int{2, 1})
expected := []int{1, 2}

a.Equal(expected, sorted)
}

func TestBubbleWithLargeSlice(t *testing.T) {
a := assert.New(t)

expected := make([]int, 100)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Poor naming: this is the input not the expectedOutput.

for i := range expected {
expected[i] = rand.Intn(1000)
}

sorted := bubble(expected)
sort.Ints(expected)

a.Equal(expected, sorted)
}

func TestBubbleWithSmallSliceIncludingNegativeInts(t *testing.T) {
a := assert.New(t)

sorted := bubble([]int{1, -2, -3})
expected := []int{-3, -2, 1}

a.Equal(expected, sorted)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test cases for empty slice and already sorted.