diff --git a/Searching/binary search/go/binarySearch.go b/Searching/binary search/go/binarySearch.go new file mode 100644 index 000000000..cc75c20bf --- /dev/null +++ b/Searching/binary search/go/binarySearch.go @@ -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") + } +} diff --git a/Sorting/Comb Sort/Go/combSort.go b/Sorting/Comb Sort/Go/combSort.go new file mode 100644 index 000000000..24d56e66c --- /dev/null +++ b/Sorting/Comb Sort/Go/combSort.go @@ -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 + } + } + } +} diff --git a/Sorting/Merge Sort/Go/mergeSort.go b/Sorting/Merge Sort/Go/mergeSort.go new file mode 100644 index 000000000..8ed19c543 --- /dev/null +++ b/Sorting/Merge Sort/Go/mergeSort.go @@ -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 +} diff --git a/Sorting/Pancake Sorting/Go/pancakeSort.go b/Sorting/Pancake Sorting/Go/pancakeSort.go new file mode 100644 index 000000000..6ce535520 --- /dev/null +++ b/Sorting/Pancake Sorting/Go/pancakeSort.go @@ -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] + } +}