-
-
Notifications
You must be signed in to change notification settings - Fork 359
Add Go language to bubble sort #157
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
Changes from 2 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 |
---|---|---|
|
@@ -82,6 +82,10 @@ | |
{ | ||
"lang": "d", | ||
"name": "D" | ||
}, | ||
{ | ||
"lang": "go", | ||
"name": "Go" | ||
} | ||
|
||
], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Submitted by Chinmaya Mahesh (chin123) | ||
|
||
package main | ||
|
||
import "fmt" | ||
|
||
func bubbleSort(array []int) { | ||
n := len(array) | ||
for i := 0; i < n-1; i++ { | ||
swapped := false | ||
for j := 0; j < n-i-1; j++ { | ||
if array[j] > array[j+1] { | ||
array[j], array[j+1] = array[j+1], array[j] | ||
swapped = true | ||
} | ||
} | ||
if !swapped { | ||
break | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
array := [10]int{1, 45, 756, 4569, 56, 3, 8, 5, -10, -4} | ||
fmt.Println("Unsorted array:") | ||
for i := 0; i < len(array); i++ { | ||
fmt.Print(array[i], " ") | ||
} | ||
fmt.Println() | ||
|
||
bubbleSort(array[:]) | ||
|
||
fmt.Println("Sorted array:") | ||
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. Similarly, why not just: fmt.Println("Sorted array: ", array) 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. Yeah, its simpler this way, I changed it. Initially it was like that because I wanted it to be like the output format of the C bubble sort. |
||
for i := 0; i < len(array); i++ { | ||
fmt.Print(array[i], " ") | ||
} | ||
fmt.Println() | ||
} |
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.
Why not just
fmt.Println("Unsorted array: ", array)
?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.
I've changed it to this format in both the cases, thanks!