Created
October 15, 2018 12:52
-
-
Save sas1ni69/c971122c790b2c2894da234c1cbc693f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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