Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

bubble sort

// Go impelementation of Bubble Sort
package main

import "fmt"

func bubbleSort(arr []int) {
	// Finding the length of the array
	n := len(arr)

	for i := 0; i < n; i++ {
		for j := 0; j < n-i-1; j++ {
			// if the fist lement is greater than the second one then swap
			if arr[j] > arr[j+1] {
				arr[j], arr[j+1] = arr[j+1], arr[j]
			}
		}
	}
}

func printArray(arr []int) {
	n := len(arr)

	for i := 0; i < n; i++ {
		fmt.Print(arr[i], " ")
	}
	fmt.Println()
}

func main() {
	arr := []int{24,35,8,16,64}

	bubbleSort(arr)

	printArray(arr)
}
Source by github.com #
 
PREVIOUS NEXT
Tagged: #bubble #sort
ADD COMMENT
Topic
Name
2+1 =