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

Lab 2: Bubble Sort #534

wants to merge 3 commits into from

Conversation

krnicolson
Copy link
Collaborator

@krnicolson krnicolson commented Jul 4, 2019

Fixes #540

Review of colleague's PR #554

Changes proposed in this PR:

  • implement bubble() function
  • implement main function to call bubble with required input
  • test main and bubble

@codecov
Copy link

codecov bot commented Jul 4, 2019

Codecov Report

Merging #534 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #534   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         107    108    +1     
  Lines        1861   1874   +13     
=====================================
+ Hits         1861   1874   +13
Impacted Files Coverage Δ
02_bubble/nicholsk/main.go 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 40114a7...316bbb3. Read the comment docs.

sorted := false

// 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.

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) ?

@mohitnag mohitnag mentioned this pull request Jul 4, 2019
@patrickmarabeas
Copy link
Collaborator

patrickmarabeas commented Jul 4, 2019

Don't forget to be descriptive with your commit messages and PR. Currently your PR does not inform the reviewer what has been done, nor the issue being resolved. Also remember to use the appropriate tags.

@mohitnag Don't forget to update the tags as well.

@patrickmarabeas patrickmarabeas mentioned this pull request Jul 4, 2019
@krnicolson krnicolson changed the title add bubble sort main and tests WIP: add bubble sort main and tests Jul 4, 2019
@krnicolson krnicolson changed the title WIP: add bubble sort main and tests Lab 2: Bubble Sort Jul 14, 2019
@krnicolson krnicolson requested a review from samuong as a code owner July 21, 2019 06:13
Copy link
Contributor

@juliaogris juliaogris left a comment

Choose a reason for hiding this comment

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

Please fix commit messages with git rebase -i HASH.

Your bubble sort implementation is lacking a key parts to be called bubble sort IMO.

}

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.

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

// 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.


// 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

if s[i-1] > s[i] {

// if less than previous then swap elements
s[i], s[i-1] = s[i-1], s[i]
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
create a test case that fails when checking that input is left unmodified and fix with copy()

// track whether the sort is complete
sorted := false

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
		}
	}

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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lab 2 - Bubble sort
5 participants