Created
June 6, 2020 16:40
-
-
Save andresabello/b0fa98e186d18615b2292f5767eb1933 to your computer and use it in GitHub Desktop.
Bubble sort algorithm
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 | |
import ( | |
"fmt" | |
) | |
func main() { | |
unordered := []int{99, 6, 5, 88, 3, 9, 7, 11} | |
ordered := bubbleSort(unordered) | |
fmt.Println(unordered) | |
fmt.Println(ordered) | |
} | |
func bubbleSort(numbers []int) []int { | |
length := len(numbers) | |
for i := 0; i < length; i++ { | |
for j := 0; j < length; j++ { | |
if j+1 <= length-1 && numbers[j] > numbers[j+1] { | |
temp := numbers[j] | |
numbers[j] = numbers[j+1] | |
numbers[j+1] = temp | |
} | |
} | |
} | |
return numbers | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment