Skip to content

Instantly share code, notes, and snippets.

@sas1ni69
Created October 15, 2018 12:52
Show Gist options
  • Save sas1ni69/c971122c790b2c2894da234c1cbc693f to your computer and use it in GitHub Desktop.
Save sas1ni69/c971122c790b2c2894da234c1cbc693f to your computer and use it in GitHub Desktop.
package main
func BubbleSort(array []int) []int {
if len(array) <= 1 {
return array
}
for i := 0; i <= len(array)-1; i++ {
for j := len(array) - 1; j > i; j-- {
if array[j] < array[j-1] {
temp := array[j]
array[j] = array[j-1]
array[j-1] = temp
}
}
}
return array
}
func OptimizedBubbleSort(array []int) []int {
if len(array) <= 1 {
return array
}
for i := 0; i <= len(array)-1; i++ {
swapped := false
for j := len(array) - 1; j > i; j-- {
if array[j] < array[j-1] {
temp := array[j]
array[j] = array[j-1]
array[j-1] = temp
swapped = true
}
}
if swapped == false {
break
}
}
return array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment