Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mergeSort, combSort, pancakeSort in Golang #1330

Merged
merged 5 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Searching/binary search/go/binarySearch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main
import "fmt"

// Binary Search
func binarySearch(item int, items []int) bool {

low := 0
high := len(items) - 1

for low <= {
median := (low + high) / 2

if items[median] < item {
low = median + 1
} else{
high = median - 1
}
}

if low == len(items) || items[low] != item {
return false
}

return true
}

// Main
func main(){
items := []int{1,2, 9, 20, 31, 45, 63, 70, 100}
flag := binarySearch(63, items)
if flag {
fmt.Println("\nFound!\n")
} else {
fmt.Println("\nNot Found!\n")
}
}
49 changes: 49 additions & 0 deletions Sorting/Comb Sort/Go/combSort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"fmt"
"math/rand"
)

func main() {

fmt.Println("\nComb Sort:")
slice := generateSlice(15)
fmt.Println("\nUnsorted:", slice)
combSort(slice)
fmt.Println("\nSorted:", slice, "\n")
}

// Generates a slice of size with random numbers
func generateSlice(size int) []int {

slice := make([]int, size, size)
for i := 0; i < size; i++ {
slice[i] = rand.Intn(999) - rand.Intn(999)
}
return slice
}

// Comb Sort
func combSort(items []int) {
var (
n = len(items)
gap = len(items)
shrink = 1.3
swapped = true
)

for swapped {
swapped = false
gap = int(float64(gap) / shrink)
if gap < 1 {
gap = 1
}
for i := 0; i+gap < n; i++ {
if items[i] > items[i+gap] {
items[i+gap], items[i] = items[i], items[i+gap]
swapped = true
}
}
}
}
76 changes: 76 additions & 0 deletions Sorting/Merge Sort/Go/mergeSort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"fmt"
"math/rand"
)

func main() {

fmt.Println("\nMerge Sort:")
slice := (15)
fmt.Println("\nUnsorted:", slice)
fmt.Println("\nSorted:", mergeSort(slice), "\n")
}

// Generates a slice of size with random numbers
func generateSlice(size int) []int {

slice := make([]int, size, size)
for i := 0; i < size; i++ {
slice[i] = rand.Intn(999) - rand.Intn(999)
}
return slice
}

// Merge Sort
func mergeSort(items []int) []int {
var num = len(items)

if num == 1 {
return items
}

middle := int(num / 2)
var (
left = make([]int, middle)
right = make([]int, num-middle)
)
for i := 0; i < num; i++ {
if i < middle {
left[i] = items[i]
} else {
right[i-middle] = items[i]
}
}

return merge(mergeSort(left), mergeSort(right))
}

// Merge
func merge(left, right []int) (result []int) {
result = make([]int, len(left) + len(right))

i := 0
for len(left) > 0 && len(right) > 0 {
if left[0] < right[0] {
result[i] = left[0]
left = left[1:]
} else {
result[i] = right[0]
right = right[1:]
}
i++
}

for j := 0; j < len(left); j++ {
result[i] = left[j]
i++
}
for j := 0; j < len(right); j++ {
result[i] = right[j]
i++
}

return
}
48 changes: 48 additions & 0 deletions Sorting/Pancake Sorting/Go/pancakeSort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"
"math/rand"
)

func main() {

fmt.Println("\nPancake Sort:")
slice := generateSlice(15)
fmt.Println("\nUnsorted:", slice)
pancakeSort(slice)
fmt.Println("\nSorted:", slice, "\n")
}

// Generates a slice of size with random numbers
func generateSlice(size int) []int {

slice := make([]int, size, size)
for i := 0; i < size; i++ {
slice[i] = rand.Intn(999) - rand.Intn(999)
}
return slice
}

// Pancake Sort
func pancakeSort(items []int) {
for uns := len(items) - 1; uns > 0; uns-- {
// find largest in unsorted range
lx, lg := 0, items[0]
for i := 1; i <= uns; i++ {
if items[i] > lg {
lx, lg = i, items[i]
}
}
// move to final position in two flips
flip(lx, items)
flip(uns, items)
}
}

// Flip
func flip(r int, items []int) {
for l := 0; l < r; l, r = l+1, r-1 {
items[l], items[r] = items[r], items[l]
}
}