Last active
October 13, 2020 14:06
-
-
Save Bablzz/088ee22185f46f5eb1454edbaa31ba0e to your computer and use it in GitHub Desktop.
Bubble sort
This file contains 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 | |
// worst n^2 | |
// best n | |
import ( | |
"fmt" | |
) | |
func main() { | |
arr := []int{2,5,4,-8,9,1} | |
for i, _:= range arr { | |
for j := 0; j < len(arr); j++ { | |
if arr[i] < arr[j] { | |
swap := arr[i] | |
arr[i] = arr[j] | |
arr[j] = swap | |
} | |
} | |
} | |
fmt.Println(arr) | |
} | |
// => [-8 1 2 4 5 9] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment