Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Simple Bubble Sort with Time Complexity In Golang

Simple Bubble Sort With Time Complexity In Golang

This tutorial help to create Bubble Sort in golang, The bubble sort help to sort the elements using comparison between the current node with the immediate next node, based on condition, The element will swaps according to the requirement and goes down to last element one by one.Te the end of first phase, the last element have maximize element of the list.

The Bubble sort follow following rule:

  • Start from left hand side of the array
  • Compare first two numbers of 0 and 1 index
  • If a[0] > a[1] than swap both number position. And if a[0] than do now swap and compare next two numbers i.e. a[1] and a[2].
  • Step-3 process repeat until there are no more numbers left to compared.

>

The time complexity is Ω(n) and worst is O(n^2).

package main

import (
	"fmt"
	_ "os"
)

func main() {
	a := []int{31, 8, 6, 54, 95, 84, 71, 67}
	fmt.Printf("%v\n", a)

	len_arr := len(a) - 1
	fmt.Printf("The length of array : %v\n", len_arr)

	for i := 0; i  a[j+1] {
				tmp := a[j]
				a[j] = a[j+1]
				a[j+1] = tmp

			}
		}
		fmt.Println("Iteration=======================", i+1)

		fmt.Printf("\nYour sort Array : %v\n", a)

	}

	fmt.Printf("Your sort Array : %v", a)
}

The post Simple Bubble Sort with Time Complexity In Golang appeared first on Rest Api Example.



This post first appeared on Rest Api Example, please read the originial post: here

Share the post

Simple Bubble Sort with Time Complexity In Golang

×

Subscribe to Rest Api Example

Get updates delivered right to your inbox!

Thank you for your subscription

×