bubble sort Algorithm

Bubble sort algorithm is a simple sorting technique that works by repeatedly swapping adjacent elements if they are in the wrong order. The algorithm gets its name from the way smaller elements "bubble" to the top of the list as larger elements "sink" to the bottom, akin to the behavior of gas bubbles in a liquid. This comparison-based sorting algorithm is easy to understand and implement, making it a popular choice for teaching and learning the basics of sorting algorithms, although it is not efficient for large datasets. The bubble sort algorithm starts at the beginning of the list and compares the first two elements. If the first element is greater than the second, the algorithm swaps them. It then moves on to the next pair of adjacent elements and performs the same comparison and swap operation if necessary. This process continues until the end of the list is reached. At this point, the largest element will have "bubbled" to the correct position at the end of the list. The algorithm then repeats the entire process, excluding the last sorted element, and continues this way until all the elements are sorted. Despite its simplicity, bubble sort has a worst-case and average-case time complexity of O(n^2), making it inefficient for large datasets compared to other sorting algorithms like quicksort and merge sort.
//Package sorts a package for demonstrating sorting algorithms in Go
package sorts

func bubbleSort(arrayzor []int) []int {


	swapped := true
	for swapped {
		swapped = false
		for i := 0; i < len(arrayzor)-1; i++ {
			if arrayzor[i+1] < arrayzor[i] {
				swap(arrayzor, i, i+1)
				swapped = true
			}
		}
	}
	return arrayzor
}

func swap(arrayzor []int, i, j int) {
	tmp := arrayzor[j]
	arrayzor[j] = arrayzor[i]
	arrayzor[i] = tmp
}

LANGUAGE:

DARK MODE: