# 10 examples of 'bubble sort algorithm' in Go

Every line of 'bubble sort algorithm' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

## All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
``5func BubbleSort(arr []int) []int {6	if len(arr) == 0 {7		return arr8	}9	for j := len(arr); j > 0; j-- {10		for i := 1; i < j; i++ {11			if arr[i] > arr[i+1] {12				swap(arr, i, i+1)13			}14		}15	}16	return arr17}``
``22func BubbleSort(arr []int) []int {23	tmp := 024	for i := 0; i < len(arr); i++ {25		for j := 0; j < len(arr)-i-1; j++ {26			if arr[j] > arr[j+1] {27				tmp = arr[j]28				arr[j] = arr[j+1]29				arr[j+1] = tmp30			}31		}32	}33	return arr34}``
``14func bubbleSort(arrayzor []int) {1516	swapped := true;17	for swapped {18		swapped = false19		for i := 0; i < len(arrayzor) - 1; i++ {20			if arrayzor[i + 1] < arrayzor[i] {21				algoutils.Swap(arrayzor, i, i + 1)22				swapped = true23			}24		}25	}	26}``
``20func BubbleBestSort(a []dataframe.Grade) []dataframe.Grade {21	lastSwap := len(a) - 122	lastSwapTemp := len(a) - 123	for j := 0; j < len(a)-1; j++ {24		lastSwap = lastSwapTemp25		for i := 0; i < lastSwap; i++ {26			if a[i].Score > a[i+1].Score {27				a[i], a[i+1] = a[i+1], a[i]28				lastSwapTemp = i29			}30		}31		if lastSwap == lastSwapTemp {32			break33		}34	}35	return a36}``
``22func BubbleSort(arr []int) {23	n := len(arr)24	for i := 0; i < n; i++ {25		flag := false26		for j := 0; j < n - i - 1; j++ {27			if arr[j] > arr[j + 1] {28				tmp := arr[j]29				arr[j] = arr[j + 1];30				arr[j + 1] = tmp31				flag = true32			}33		}34		if (!flag) {35			break;36		}37	}38}``
``33func heapSort(s1 []int, i int, s1Len int) []int {34	done := false3536	tmp := 037	maxChild := 03839	for (i*2+1 < s1Len) && (!done) {40		if i*2+1 == s1Len-1 {41			maxChild = i*2 + 142		} else if s1[i*2+1] > s1[i*2+2] {43			maxChild = i*2 + 144		} else {45			maxChild = i*2 + 246		}4748		if s1[i] < s1[maxChild] {49			tmp = s1[i]50			s1[i] = s1[maxChild]51			s1[maxChild] = tmp52			i = maxChild53		} else {54			done = true55		}56		fmt.Printf("Sorting ...:\t%v\n", s1)57	}5859	return s160}``
``13func heapsort(array []int) {14	ep := (len(array) - 1) >> 115	fmt.Println(ep)16	for i := ep; i >= 0; i-- {17		heapt(array, i, len(array)-1)18	}1920	for i := len(array) - 1; i > 0; i-- {21		array[0], array[i] = array[i], array[0]22		heapt(array, 0, i-1)23	}24}``
``11func bubblesort(n []int) {12	for i := 0; i < len(n) - 1; i++ {13		for j := i + 1; j < len(n); j++ {14			if n[j] < n[i] {15				n[i], n[j] = n[j], n[i]16			}17		}18	}19}``
``34func bubbleSort(numbers []int) {35	n := len(numbers)36	for i := 0; i < n; i++ {37		if !sweep(numbers, i) {38			return39		}40	}41}``
``18func MergeSort(arr []int) []int {19	var alen = len(arr)20	var ns = make([]int, alen, alen)2122	var tmp = &arr23	var stmp = &ns24	for add := 1; add < alen; add *= 2 {25		fmt.Println(add)26		var start1 = 027		var start2 = 028		ik := 029		for start1 = 0; start1 < alen; start1 = start1 + add {30			start2 = start1 + add31			var end1 = start1 + add32			var end2 = start2 + add33			for start1 < end1 && start2 < end2 && start1 < alen && start2 < alen {34				if (*tmp)[start1] < (*tmp)[start2] {35					(*stmp)[ik] = (*tmp)[start1]36					start1++37				} else {38					(*stmp)[ik] = (*tmp)[start2]39					start2++40				}41				ik++42				fmt.Println(*stmp)43			}4445			for start1 < end1 && start1 < alen {46				(*stmp)[ik] = (*tmp)[start1]47				start1++48				ik++49				fmt.Println(*stmp)50			}5152			for start2 < end2 && start2 < alen {53				(*stmp)[ik] = (*tmp)[start2]54				start2++55				ik++56				fmt.Println(*stmp)57			}58		}59		tmp, stmp = stmp, tmp60		for index, _ := range *stmp {61			(*stmp)[index] = 062		}63	}64	return (*tmp)65}``