algorithms

package module
v0.0.0-...-9e17f61 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 8, 2017 License: MIT Imports: 1 Imported by: 0

README

algorithms-with-go Build Status Go Report Card GoDoc

This is a collection of different algorithms written in Go Lang. The purpose of this package is to define basic algorithms in a concise, but readable form. However, no (pre)mature optimizations should be expected here and code should never be used in production.

Contents

Iterative
  • coprime
  • maxsubarray
  • isprime
Recursive
  • factorial
  • fibonacci
  • gcd
  • binary
  • linear
Sort
  • bubble
  • insertion
  • selection

License

MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Coprime

func Coprime(a, b int) bool

Coprime test whether `a` and `b` are relatively prime integers.

Example
package main

import (
	"fmt"

	algorithms "github.com/yefremov/algorithms-with-go"
)

func main() {
	fmt.Println(algorithms.Coprime(1, 1))
	fmt.Println(algorithms.Coprime(2, 2))
	fmt.Println(algorithms.Coprime(13, 27))
	fmt.Println(algorithms.Coprime(20536, 7826))
}
Output:

true
false
true
false

func Fac

func Fac(n int) int

Fac gets factorial value of `n`.

Example
package main

import (
	"fmt"

	algorithms "github.com/yefremov/algorithms-with-go"
)

func main() {
	for i := 0; i < 6; i++ {
		fmt.Println(algorithms.Fac(i))
	}
}
Output:

1
1
2
6
24
120

func Fib

func Fib(n int) int

Fib gets fibonacci value of `n`.

Example
package main

import (
	"fmt"

	algorithms "github.com/yefremov/algorithms-with-go"
)

func main() {
	for i := 0; i < 6; i++ {
		fmt.Println(algorithms.Fib(i))
	}
}
Output:

1
1
2
3
5
8

func Gcd

func Gcd(a, b int) int

Gcd gets greatest common divisor of `a` and `b`.

Example
package main

import (
	"fmt"

	algorithms "github.com/yefremov/algorithms-with-go"
)

func main() {
	fmt.Println(algorithms.Gcd(468, 24))
	fmt.Println(algorithms.Gcd(135, 19))
	fmt.Println(algorithms.Gcd(19, 2))
	fmt.Println(algorithms.Gcd(20536, 7826))
}
Output:

12
1
1
2

func MaxSub

func MaxSub(array []int) int

MaxSub Finds the maximum sum of the elements of a subarray in a given `array`.

Example
package main

import (
	"fmt"

	algorithms "github.com/yefremov/algorithms-with-go"
)

func main() {
	fmt.Println(algorithms.MaxSub([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4}))
}
Output:

6

func SearchBinary

func SearchBinary(haystack []int, needle int) bool

SearchBinary searches for a `needle` in `haystack`.

Example
package main

import (
	"fmt"

	algorithms "github.com/yefremov/algorithms-with-go"
)

func main() {
	fmt.Println(algorithms.SearchBinary([]int{1, 2, 3}, 2))
	fmt.Println(algorithms.SearchBinary([]int{1, 2, 3}, 4))
}
Output:

true
false

func SearchLinear

func SearchLinear(haystack []int, needle int) bool

SearchLinear searches for a `needle` in a `haystack`.

Example
package main

import (
	"fmt"

	algorithms "github.com/yefremov/algorithms-with-go"
)

func main() {
	fmt.Println(algorithms.SearchLinear([]int{1, 2, 3}, 2))
	fmt.Println(algorithms.SearchLinear([]int{1, 2, 3}, 4))
}
Output:

true
false

func SortBubble

func SortBubble(numbers []int) []int

SortBubble sorts numbers array in ascending order.

Example
package main

import (
	"fmt"

	algorithms "github.com/yefremov/algorithms-with-go"
)

func main() {
	fmt.Println(algorithms.SortBubble([]int{3, 3, 7, 1}))
}
Output:

[1 3 3 7]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL