diff --git a/README.md b/README.md index c3b9afae..cdc8f4ff 100644 --- a/README.md +++ b/README.md @@ -1,68 +1,75 @@ -# :abacus: Algorithms and Data Structures +

+

🧮Algorithms and Data Structures

+

This repository provides several classic algorithms and data structures in **Computer Science**, as well as some extra problems that are frequently encountered in programming challenges. -In order to achieve greater coverage and encourage more people to contribute to the project, the algorithms are available in the following languages: **C**, **C++**, **Java**, **Python**, **Go**, **Ruby**, **Javascript**, **Swift**, **Rust**, **Scala** and **Kotlin**. +In order to achieve greater coverage and encourage more people to contribute to the project, the algorithms are available in the following languages: **C**, **C++**, **Java**, **Python**, **Go**, **Ruby**, **Javascript**, **Swift**, **Rust**, **Scala**, **Kotlin** and **Elixir**. You can quickly access an [online compiler](https://onecompiler.com/) by clicking on the symbols corresponding to the desired programming languages in the top rows of each table. - + + @@ -121,6 +128,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -179,6 +191,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -237,6 +254,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -295,6 +317,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -353,6 +380,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -411,6 +443,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -469,6 +506,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -527,6 +569,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -585,6 +632,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -643,6 +695,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -701,6 +758,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -759,6 +821,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -817,6 +884,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -875,6 +947,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -933,6 +1010,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -991,6 +1073,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1049,6 +1136,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1107,6 +1199,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1165,9 +1262,14 @@ In order to achieve greater coverage and encourage more people to contribute to + - + + @@ -1281,9 +1388,14 @@ In order to achieve greater coverage and encourage more people to contribute to + - + + @@ -1397,6 +1514,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1455,6 +1577,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1513,6 +1640,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1571,6 +1703,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1629,66 +1766,76 @@ In order to achieve greater coverage and encourage more people to contribute to +
AlgorithmsAlgorithms - + - + - + - + - + - + - + - + - + - + - + + + + +
Dijkstra's Algorithm + + + +
Floyd–Warshall Algorithm + + + +
Binary Search + + + +
Graph Search + + + +
Linear Search (Iterative) + + + +
Linear Search (Recursive) + + + +
Linear Search (Sentinel) + + + +
Interpolation Search + + + +
Travelling Salesman + + + +
Hamiltonian Cycle + + + +
Connected Components + + + +
Exponentiation (Iterative) + + + +
Exponentiation (Recursive) + + + +
Factorial (Iterative) + + + +
Factorial (Recursive) + + + +
Fibonacci (Iterative) + + + +
Fibonacci (Recursive) + + + +
Fibonacci (Memoization) + + + +
Max (Recursive) + + + +
Min and Max (Iterative)Min and Max (Iterative) @@ -1223,6 +1325,11 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
Min and Max (Recursive) + + + +
Min and Max (D&C)Min and Max (D&C) @@ -1339,6 +1451,11 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
Knight's Tour + + + +
Tower of Hanoi + + + +
Genetic Algorithm + + + +
Huffman's Algorithm + + + +
LZ77 Algorithm + + + +
- + + @@ -1747,6 +1894,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1805,6 +1957,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1863,6 +2020,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1921,6 +2083,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -1979,6 +2146,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2037,6 +2209,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2095,6 +2272,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2153,6 +2335,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2211,6 +2398,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2269,9 +2461,14 @@ In order to achieve greater coverage and encourage more people to contribute to + - + + @@ -2385,6 +2587,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2443,9 +2650,14 @@ In order to achieve greater coverage and encourage more people to contribute to + - + + @@ -2559,6 +2776,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2617,66 +2839,76 @@ In order to achieve greater coverage and encourage more people to contribute to +
Data StructuresData Structures - + - + - + - + - + - + - + - + - + - + - + + + + +
Binary Tree + + + +
Binary Search Tree + + + +
Double-ended Queue + + + +
Queue + + + +
Dynamic Queue + + + +
Graph + + + +
Circular Linked List + + + +
Singly Linked List + + + +
Doubly Linked List + + + +
Unordered Linked List + + + +
Sorted Linked ListSorted Linked List @@ -2327,6 +2524,11 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
Skip List + + + +
Stack + + + +
Dynamic StackDynamic Stack @@ -2501,6 +2713,11 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
Ring Buffer + + + +
Hash Table + + + +
- + - + @@ -2735,6 +2967,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2793,6 +3030,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2851,6 +3093,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2909,6 +3156,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -2967,6 +3219,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3025,6 +3282,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3083,6 +3345,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3141,6 +3408,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3199,6 +3471,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3257,6 +3534,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3315,6 +3597,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3373,6 +3660,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3431,6 +3723,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3489,6 +3786,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3547,69 +3849,79 @@ In order to achieve greater coverage and encourage more people to contribute to +
Sorting AlgorithmsSorting Algorithms - + - + - + - + - + - + - + - + - + - + - + + + + + +
Bogosort + + + +
Bubble Sort + + + +
Bucket Sort + + + +
Cocktail Sort + + + +
Comb Sort + + + +
Counting Sort + + + +
Gnome Sort + + + +
Heapsort + + + +
Insertion Sort + + + +
Merge Sort + + + +
Quicksort + + + +
Radix Sort + + + +
Selection Sort + + + +
Shell Sort + + + +
Timsort + + + +
- + - + - + + - + + @@ -3781,9 +4103,14 @@ In order to achieve greater coverage and encourage more people to contribute to + - + + @@ -3897,6 +4229,11 @@ In order to achieve greater coverage and encourage more people to contribute to + @@ -3955,9 +4292,14 @@ In order to achieve greater coverage and encourage more people to contribute to + - + + - + +
ExtraExtra - + - + - + - + - + - + - + - + - + - + - + + + + + +
Queue using StacksQueue using Stacks @@ -3665,9 +3977,14 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
Two-Sum ProblemTwo-Sum Problem @@ -3723,6 +4040,11 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
Palindrome + + + +
IsogramIsogram @@ -3839,6 +4166,11 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
Leibniz Formula for Pi + + + +
Maze-Solving Algorithm + + + +
Rotten OrangesRotten Oranges @@ -4013,9 +4355,14 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
Find Distinct SubsetsFind Distinct Subsets @@ -4071,6 +4418,11 @@ In order to achieve greater coverage and encourage more people to contribute to + + + +
@@ -4080,3 +4432,7 @@ In order to achieve greater coverage and encourage more people to contribute to Feel free to contribute to the project, all contributions are welcome. :grin: If you have questions about how to contribute, take a look at the [CONTRIBUTING](CONTRIBUTING.md) file. + +

+[🔼 Back to top](https://github.com/TestingGround00/algorithms-and-data-structures/#algorithms-and-data-structures) + diff --git a/logos/elixir.svg b/logos/elixir.svg new file mode 100644 index 00000000..44d8f7d7 --- /dev/null +++ b/logos/elixir.svg @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + file_type_elixir + + + + + + + + \ No newline at end of file diff --git a/src/elixir/CalculatePi.ex b/src/elixir/CalculatePi.ex new file mode 100644 index 00000000..b4b6c9f6 --- /dev/null +++ b/src/elixir/CalculatePi.ex @@ -0,0 +1,31 @@ +# Module to calculate an approximation of Pi +defmodule PiApproximation do + + # Function to calculate pi given number of terms + def calculate_pi(terms) do + + # Initialize pi accumulator + pi = 0.0 + + # Reduce over 1..terms range + # Accumulator is a tuple with pi, denominator, and operation + Enum.reduce(1..terms, {pi, 1.0, 1.0}, fn _, {acc_pi, acc_denom, acc_op} -> + + # Calculate new pi value + new_pi = acc_pi + acc_op * (4.0 / acc_denom) + + # Return tuple with updated values + {new_pi, acc_denom + 2.0, -acc_op} + end) |> elem(0) + end + +end + +# Number of terms to approximate +terms = 100_000 + +# Call approximation function +result = PiApproximation.calculate_pi(terms) + +# Print result +IO.puts(result) \ No newline at end of file diff --git a/src/elixir/FactorialIterative.ex b/src/elixir/FactorialIterative.ex new file mode 100644 index 00000000..c509bde0 --- /dev/null +++ b/src/elixir/FactorialIterative.ex @@ -0,0 +1,26 @@ +defmodule Factorial do + # Define a module called Factorial. + + def factorial(n) when n >= 0 do + # Calculate the factorial using Enum.reduce. + Enum.reduce(1..n, 1, &(&1 * &2)) + end + + defp format_factorial(i, result) do + # Format the output string. + "#{i}! = #{result}" + end + + def main do + # Entry point of the program. + nums = [0, 1, 2, 3, 4, 5] + + Enum.each(nums, fn i -> + # Iterate through the list of numbers. + result = factorial(i) + IO.puts(format_factorial(i, result)) + end) + end +end + +Factorial.main() # Execute the program. diff --git a/src/elixir/FactorialRecursive.ex b/src/elixir/FactorialRecursive.ex new file mode 100644 index 00000000..2ac02996 --- /dev/null +++ b/src/elixir/FactorialRecursive.ex @@ -0,0 +1,23 @@ +# Define Math module for math utilities +defmodule Math do + + # Recursive factorial function + def factorial(0), do: 1 # Base case + + def factorial(n) when n > 0 do + # Recursive case + n * factorial(n-1) + end + +end + +# Array of numbers to get factorials for +nums = [0, 1, 2, 3, 4, 5] + +# Loop through the nums array +for i <- nums do + + # Calculate and print factorial + IO.puts "#{i}! = #{Math.factorial(i)}" + +end \ No newline at end of file diff --git a/src/elixir/FibonacciIterative.ex b/src/elixir/FibonacciIterative.ex new file mode 100644 index 00000000..295ae7b5 --- /dev/null +++ b/src/elixir/FibonacciIterative.ex @@ -0,0 +1,24 @@ +# Define a Fibonacci module +defmodule Fib do + + # Function to calculate nth Fibonacci number + def fibonacci(n) when n >= 0 do + + # Initialize starting values + last = 0 + curr = 1 + + # Use Enum.reduce as a loop from 0 to n-1 + Enum.reduce(0..(n-1), {last, curr}, fn _, {last, curr} -> + + # Update last and curr, returning as a tuple + {curr, curr + last} + end) + + # Return the last value after reducing + |> elem(0) + end +end + +# Print 10th Fibonacci number +IO.puts(Fib.fibonacci(10)) \ No newline at end of file diff --git a/src/elixir/FibonacciRecursive.ex b/src/elixir/FibonacciRecursive.ex new file mode 100644 index 00000000..9ca4b823 --- /dev/null +++ b/src/elixir/FibonacciRecursive.ex @@ -0,0 +1,14 @@ +# Define a Fibonacci module +defmodule Fibonacci do + # Base case: If n is 0 or 1, return n. + def fibonacci(n) when n <= 1, do: n + + # Recursive case: Calculate the Fibonacci number by summing the results + # of the previous two Fibonacci numbers. + def fibonacci(n) do + fibonacci(n - 1) + fibonacci(n - 2) + end +end + +# Print the Fibonacci number for n = 10. +IO.puts("Fibonacci(n): #{Fibonacci.fibonacci(10)}") \ No newline at end of file diff --git a/src/elixir/MaxRecursive.ex b/src/elixir/MaxRecursive.ex new file mode 100644 index 00000000..75a942cd --- /dev/null +++ b/src/elixir/MaxRecursive.ex @@ -0,0 +1,19 @@ +defmodule MaxRecursive do + # Base case: When n is 1, return the first element of the list (head). + def max_recursive(nums, 1), do: hd(nums) + + # Recursive case: Calculate the maximum recursively. + def max_recursive(nums, n) do + aux = max_recursive(nums, n - 1) + if aux > Enum.at(nums, n - 1) do + aux + else + Enum.at(nums, n - 1) + end + end +end + +# Example usage +nums = [1, 2, 3, 4, 32, 6, 7, 8, 9, 10] +IO.inspect "nums = #{inspect(nums)}" +IO.inspect "Max = #{MaxRecursive.max_recursive(nums, length(nums))}"