Created
January 22, 2021 17:41
-
-
Save somersbmatthews/35ef2f82b168fd6b75d2194fdaf1ec6d to your computer and use it in GitHub Desktop.
bubble sorts #golang #go #algorithms
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
// ascending, non-decreasing | |
func BubbleSort(data []int) { | |
for i := 0; i < len(data); i++ { | |
for j := 1; j < len(data)-i; j++ { | |
if data[j] < data[j-1] { | |
data[j], data[j-1] = data[j-1], data[j] | |
} | |
} | |
} | |
} | |
// descending, non-increasing | |
for index := 0; index < len(nums)-1; index++ { | |
for innerIndex := 0; innerIndex < len(nums) - index - 1; innerIndex++ { | |
if nums[innerIndex] < nums[innerIndex+1] { | |
nums[innerIndex], nums[innerIndex+1] = nums[innerIndex+1], nums[innerIndex] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment