Convert Figma logo to code with AI

0xAX logogo-algorithms

Algorithms and data structures for golang

1,917
351
1,917
8

Top Related Projects

15,801

Algorithms and Data Structures implemented in Go for beginners, following best practices.

✅ Solutions to LeetCode by Go, 100% test coverage, runtime beats 100% / LeetCode 题解

:memo: LeetCode of algorithms with golang solution(updating).

Algorithms & Data Structures in Go

16,091

GoDS (Go Data Structures) - Sets, Lists, Stacks, Maps, Trees, Queues, and much more

Data Structures and Algorithms implementation in Go

Quick Overview

The 0xAX/go-algorithms repository is a collection of various algorithms and data structures implemented in Go. It serves as a learning resource for developers interested in understanding common algorithms and their Go implementations. The project covers a wide range of topics, including sorting, searching, graph algorithms, and more.

Pros

  • Provides clear and well-commented Go implementations of popular algorithms
  • Covers a diverse range of algorithmic topics and data structures
  • Serves as a valuable learning resource for Go developers and algorithm enthusiasts
  • Actively maintained with contributions from the community

Cons

  • Some implementations may not be optimized for production use
  • Documentation could be more comprehensive for certain algorithms
  • Not all algorithms have accompanying explanations or complexity analysis
  • Some advanced algorithms or data structures may be missing

Code Examples

Here are a few examples of algorithms implemented in this repository:

  1. Binary Search
func BinarySearch(needle int, haystack []int) bool {
	low := 0
	high := len(haystack) - 1

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

		if haystack[median] < needle {
			low = median + 1
		} else {
			high = median - 1
		}
	}

	if low == len(haystack) || haystack[low] != needle {
		return false
	}

	return true
}
  1. Bubble Sort
func BubbleSort(arr []int) {
	swapped := true
	for swapped {
		swapped = false
		for i := 0; i < len(arr)-1; i++ {
			if arr[i+1] < arr[i] {
				arr[i], arr[i+1] = arr[i+1], arr[i]
				swapped = true
			}
		}
	}
}
  1. Depth-First Search (DFS)
func DFS(g *Graph, startVertex *Vertex) {
	visited := make(map[int]bool)
	dfsUtil(g, startVertex, visited)
}

func dfsUtil(g *Graph, v *Vertex, visited map[int]bool) {
	visited[v.key] = true
	fmt.Printf("%d ", v.key)

	for _, neighbor := range g.vertices[v.key] {
		if !visited[neighbor.key] {
			dfsUtil(g, neighbor, visited)
		}
	}
}

Getting Started

To use the algorithms in your Go project:

  1. Clone the repository:

    git clone https://github.com/0xAX/go-algorithms.git
    
  2. Import the desired algorithm in your Go code:

    import "path/to/go-algorithms/sorting"
    
    func main() {
        arr := []int{64, 34, 25, 12, 22, 11, 90}
        sorting.BubbleSort(arr)
        fmt.Println(arr)
    }
    
  3. Run your Go program:

    go run your_program.go
    

Competitor Comparisons

15,801

Algorithms and Data Structures implemented in Go for beginners, following best practices.

Pros of TheAlgorithms/Go

  • More comprehensive collection of algorithms and data structures
  • Better organized with clear directory structure
  • More active community and frequent updates

Cons of TheAlgorithms/Go

  • Less focus on detailed explanations and comments
  • Some implementations may be less optimized for performance

Code Comparison

go-algorithms (Binary Search):

func BinarySearch(array []int, target int) int {
    low, high := 0, len(array)-1
    for low <= high {
        mid := (low + high) / 2
        if array[mid] == target {
            return mid
        } else if array[mid] < target {
            low = mid + 1
        } else {
            high = mid - 1
        }
    }
    return -1
}

TheAlgorithms/Go (Binary Search):

func BinarySearch(arr []int, target int) int {
    left, right := 0, len(arr)-1
    for left <= right {
        mid := left + (right-left)/2
        if arr[mid] == target {
            return mid
        } else if arr[mid] < target {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    return -1
}

Both implementations are similar, but TheAlgorithms/Go uses a slightly different method to calculate the midpoint, which can help prevent integer overflow in extreme cases.

✅ Solutions to LeetCode by Go, 100% test coverage, runtime beats 100% / LeetCode 题解

Pros of LeetCode-Go

  • Comprehensive collection of LeetCode problem solutions
  • Well-organized structure with problems categorized by difficulty and topic
  • Detailed explanations and multiple solution approaches for each problem

Cons of LeetCode-Go

  • Focused solely on LeetCode problems, lacking general-purpose algorithms
  • May not cover all fundamental data structures and algorithms

Code Comparison

LeetCode-Go (Binary Search implementation):

func binarySearch(nums []int, target int) int {
    left, right := 0, len(nums)-1
    for left <= right {
        mid := left + (right-left)>>1
        if nums[mid] == target {
            return mid
        } else if nums[mid] < target {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    return -1
}

go-algorithms (Binary Search implementation):

func BinarySearch(array []int, target int) int {
    low := 0
    high := len(array) - 1
    for low <= high {
        mid := (low + high) / 2
        if array[mid] == target {
            return mid
        } else if array[mid] < target {
            low = mid + 1
        } else {
            high = mid - 1
        }
    }
    return -1
}

Both implementations are similar, with LeetCode-Go using bit shifting for calculating the midpoint, which can be slightly more efficient.

:memo: LeetCode of algorithms with golang solution(updating).

Pros of awesome-golang-algorithm

  • More comprehensive collection of algorithms and data structures
  • Better organized with categorization and difficulty levels
  • Includes solutions from multiple coding platforms (LeetCode, HackerRank, etc.)

Cons of awesome-golang-algorithm

  • Less focus on detailed explanations and comments in the code
  • May be overwhelming for beginners due to the large number of algorithms

Code Comparison

go-algorithms:

func BubbleSort(array []int) {
    for i := 0; i < len(array)-1; i++ {
        for j := 0; j < len(array)-i-1; j++ {
            if array[j] > array[j+1] {
                array[j], array[j+1] = array[j+1], array[j]
            }
        }
    }
}

awesome-golang-algorithm:

func sortArray(nums []int) []int {
    for i := 0; i < len(nums); i++ {
        for j := i + 1; j < len(nums); j++ {
            if nums[i] > nums[j] {
                nums[i], nums[j] = nums[j], nums[i]
            }
        }
    }
    return nums
}

Both repositories provide implementations of common algorithms in Go. go-algorithms focuses on providing clear, well-commented code for educational purposes, while awesome-golang-algorithm offers a wider range of algorithms and problem solutions. The code comparison shows similar implementations of bubble sort, with slight differences in variable naming and loop structure.

Algorithms & Data Structures in Go

Pros of algorithms

  • More comprehensive coverage of data structures and algorithms
  • Better organized directory structure with clear categorization
  • Includes more advanced topics like graph algorithms and dynamic programming

Cons of algorithms

  • Less actively maintained (last commit in 2015)
  • Fewer contributors and community engagement
  • Lacks detailed explanations or comments for some implementations

Code Comparison

go-algorithms (Dijkstra's algorithm):

func Dijkstra(graph *Graph, source *Vertex) {
    dist := make(map[*Vertex]int)
    for _, v := range graph.Vertices {
        dist[v] = math.MaxInt32
    }
    dist[source] = 0
    // ... (implementation continues)
}

algorithms (Dijkstra's algorithm):

func Dijkstra(g *Graph, source int) []int {
    dist := make([]int, g.V)
    for i := range dist {
        dist[i] = math.MaxInt32
    }
    dist[source] = 0
    // ... (implementation continues)
}

Both implementations follow similar logic, but algorithms uses a more concise approach with slices instead of maps for distance tracking.

16,091

GoDS (Go Data Structures) - Sets, Lists, Stacks, Maps, Trees, Queues, and much more

Pros of gods

  • More comprehensive collection of data structures and algorithms
  • Better documentation and examples for each implementation
  • Actively maintained with regular updates and contributions

Cons of gods

  • Larger codebase, potentially more complex to navigate
  • May include more advanced structures not needed for basic use cases
  • Slightly steeper learning curve for beginners

Code Comparison

gods implementation of a stack:

stack := arraystack.New()
stack.Push(1)
stack.Push(2)
value, _ := stack.Pop()

go-algorithms implementation of a stack:

stack := NewStack()
stack.Push(1)
stack.Push(2)
value := stack.Pop()

Both implementations offer similar functionality, but gods provides more extensive error handling and additional methods. The go-algorithms version is simpler but may lack some advanced features.

gods generally offers more robust and feature-rich implementations, while go-algorithms focuses on simplicity and basic functionality. The choice between the two depends on the specific needs of the project and the developer's preference for complexity versus simplicity.

Data Structures and Algorithms implementation in Go

Pros of Data-Structures-and-Algorithms

  • More comprehensive coverage of data structures and algorithms
  • Includes implementations in multiple programming languages (C++, Python, Java)
  • Better organized with separate folders for each topic

Cons of Data-Structures-and-Algorithms

  • Less focused on Go-specific implementations
  • May lack some of the Go-specific optimizations present in go-algorithms
  • Repository activity is less recent compared to go-algorithms

Code Comparison

go-algorithms (Go implementation of QuickSort):

func quickSort(arr []int, low, high int) {
    if low < high {
        pivot := partition(arr, low, high)
        quickSort(arr, low, pivot-1)
        quickSort(arr, pivot+1, high)
    }
}

Data-Structures-and-Algorithms (Python implementation of QuickSort):

def quickSort(arr, low, high):
    if low < high:
        pi = partition(arr, low, high)
        quickSort(arr, low, pi-1)
        quickSort(arr, pi+1, high)

Both repositories provide implementations of common algorithms and data structures. go-algorithms focuses specifically on Go implementations, which may be beneficial for Go developers looking for optimized solutions. Data-Structures-and-Algorithms offers a broader range of languages and topics, making it more suitable for general algorithm study and comparison across different programming languages.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

go-algorithms

go-algorithms - implementation of different algorithms and data structures with golang.

Usage

  1. Clone https://github.com/0xAX/go-algorithms.git repo, it must be in your $GOPATH.

  2. Execute go build && go install in go-algorithms

  3. Execute go build bubble_sort.go and ./bubble_sort

Algorithms

Sorting

Searching

Collections

Numerical

Contribution

  • Fork go-algorithms;
  • Make changes;
  • Send pull request;
  • Thank you.

Author

@0xAX