Skip to content

Commit

Permalink
GoLang - Bubble and selection sort added (tornado-12#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivangiRai1310 authored Oct 4, 2021
1 parent c3de003 commit fe9761b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Golang/bubbleSort Program.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import "fmt"

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

for i:=0; i<=len(arr)-1; i++{
for j:=0; j<len(arr)-1-i; j++{
if arr[j]> arr[j+1]{
arr[j], arr[j+1] = arr[j+1], arr[j]
}
}
}
return arr
}

func main(){
fmt.Println(bubbleSort([]int{9, 2, 3, 1, 0}))
fmt.Println(bubbleSort([]int{17, 3, 1, 8, 9}))
fmt.Println(bubbleSort([]int{8, 2, 7, 14, 5}))
}

/*
Bubble Sort Program
--------------------
Input array - {9, 2, 3, 1, 0}
{17, 3, 1, 8, 9}
{8, 2, 7, 14, 5}
Output Sorted array - [0 1 2 3 9]
[1 3 8 9 17]
[2 5 7 8 14]
Time Complexity - O(n2)
*/
38 changes: 38 additions & 0 deletions Golang/selectionSort Program.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import "fmt"

func selectionSort(items []int) []int{
var n = len(items)
for i := 0; i < n; i++ {
var minIdx = i
for j := i; j < n; j++ {
if items[j] < items[minIdx] {
minIdx = j
}
}
items[i], items[minIdx] = items[minIdx], items[i]
}
return items
}

func main(){
fmt.Println(selectionSort([]int{9, 2, 3, 1, 0}))
fmt.Println(selectionSort([]int{17, 3, 1, 8, 9}))
fmt.Println(selectionSort([]int{8, 2, 7, 14, 5}))
}

/*
Selection Sort Program
--------------------
Input array - {9, 2, 3, 1, 0}
{17, 3, 1, 8, 9}
{8, 2, 7, 14, 5}
Output Sorted array - [0 1 2 3 9]
[1 3 8 9 17]
[2 5 7 8 14]
Time Complexity - O(n2)
*/

0 comments on commit fe9761b

Please sign in to comment.