-
Notifications
You must be signed in to change notification settings - Fork 165
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
Lab 2: Bubble Sort #534
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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 { | ||
|
||
// track whether the sort is complete | ||
sorted := false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you don't need |
||
|
||
for !sorted { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typically bubble sort has to nested loops. 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++ { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why |
||
|
||
// compare element to previous | ||
if s[i-1] > s[i] { | ||
|
||
// if less than previous then swap elements | ||
s[i], s[i-1] = s[i-1], s[i] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you haven't created a copy of the input slice and you are there for modifying the input. |
||
swaps = true | ||
} | ||
|
||
} | ||
|
||
// an iteration with no swaps indicates the array is sorted | ||
if !swaps { | ||
sorted = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return or break here |
||
} | ||
} | ||
|
||
return s | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
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 TestBubbleWithRepeatedNumbers(t *testing.T) { | ||
a := assert.New(t) | ||
|
||
sorted := bubble([]int{3, 1, 2, 3, 2, 1}) | ||
expected := []int{1, 1, 2, 2, 3, 3} | ||
|
||
a.Equal(expected, sorted) | ||
} | ||
|
||
func TestBubbleWithLargeSlice(t *testing.T) { | ||
a := assert.New(t) | ||
|
||
expected := make([]int, 100) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Poor naming: this is the |
||
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) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add test cases for empty slice and already sorted. |
There was a problem hiding this comment.
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.