Bubble sort is a simple sorting algorithm that sequentially goes through an array n times. Each time the algorithm runs through the array, it looks at the first element and then the second element, if the first element is larger than the second element then it swaps them, it then proceeds through the entire list performing this action.
Time Complexity
The time complexity for this algorithm is O(n^2) where n is the number of items being sorted. It is not recommended to use for large input sets.
Best case time complexity is O(n) when the list is already sorted.
Implementation
package main
import (
"fmt"
)
var arrToBeSorted [10]int = [10]int{1,3,2,5,4,6,7,9,8,10}
func BubbleSort(input [10]int) {
for i := 0; i < len(input)-1; i++ {
if input[i] > input[i+1] {
input[i], input[i+1] = input[i+1], input[i]
}
}
fmt.Println(input)
}
func main() {
fmt.Println("Before")
fmt.Println(arrToBeSorted)
fmt.Println("After")
BubbleSort(arrToBeSorted)
}
Result:
Other Topics:
How to build NGINX RTMP module, Setup Live Streaming with NGINX RTMP module, Publishing Stream with Open Broadcaster Software (OBS), Create Adaptive Streaming with NGINX RTMP, Implementing Filtergraph in Streaming with NGINX RTMP, How to Implement Running Text in Streaming with NGINX RTMP, How to build OpenSceneGraph with Code::Blocks, How to build OpenSceneGraph with Visual Studio 2010, Building Geometry Model, How to run OpenSceneGraph with Netbean IDE 8.2 C++, Rendering Basic Shapes, Using OSG Node to Load 3D Object Model, Rendering 3D Simulator with OpenSceneGraph, How to compile wxWidgets with Visual Studio 2010, How to Setup Debugging in Golang with Visual Code
No comments:
Post a Comment