From 4263dac181736e937c735d380a15bd7e75b0fc30 Mon Sep 17 00:00:00 2001 From: Aadish Jain <121042282+mapcrafter2048@users.noreply.github.com> Date: Sat, 16 Nov 2024 21:39:37 +0530 Subject: [PATCH] Add time and space complexity information to various algorithms (#744) * Add time and space complexity information to various algorithms * Added the implementation of Kosaraju algorithm * Added some style issues in articulationpoints.go * Formatted code with gofmt -s to meet style requirements --------- Co-authored-by: Rak Laptudirm --- cache/lru.go | 7 ++ checksum/crc8.go | 2 + checksum/luhn.go | 2 + cipher/caesar/caesar.go | 4 + .../diffiehellman/diffiehellmankeyexchange.go | 4 + cipher/polybius/polybius.go | 4 + cipher/railfence/railfence.go | 6 + cipher/rot13/rot13.go | 4 + cipher/rsa/rsa.go | 2 + cipher/rsa/rsa2.go | 2 + cipher/transposition/transposition.go | 2 + cipher/xor/xor.go | 4 + compression/huffmancoding.go | 2 + compression/rlecoding.go | 3 + conversion/base64.go | 2 + conversion/binarytodecimal.go | 2 + conversion/decimaltobinary.go | 2 + conversion/inttoroman.go | 6 + conversion/rgbhex.go | 2 + conversion/romantoint.go | 2 + dynamic/abbreviation.go | 2 + dynamic/binomialcoefficient.go | 5 + dynamic/catalan.go | 4 +- dynamic/coinchange.go | 6 + dynamic/editdistance.go | 2 + dynamic/fibonacci.go | 4 + dynamic/knapsack.go | 3 + dynamic/longestcommonsubsequence.go | 3 + dynamic/longestincreasingsubsequence.go | 6 + dynamic/longestpalindromicsubsequence.go | 2 + dynamic/matrixmultiplication.go | 2 + dynamic/rodcutting.go | 2 + dynamic/subsetsum.go | 3 +- dynamic/traprainwater.go | 2 + dynamic/uniquepaths.go | 2 + graph/articulationpoints.go | 107 ++++++++---------- graph/bellmanford.go | 2 + graph/breadthfirstsearch.go | 4 +- graph/coloring/backtracking.go | 2 + graph/coloring/bfs.go | 2 + graph/coloring/bipartite.go | 16 ++- graph/coloring/greedy.go | 2 + graph/cycle.go | 2 + graph/depthfirstsearch.go | 6 + graph/dijkstra.go | 7 ++ graph/floydwarshall.go | 2 + graph/lowestcommonancestor.go | 2 + graph/topological.go | 7 ++ hashing/sha256/sha256.go | 2 + math/aliquotsum.go | 2 + math/armstrong/isarmstrong.go | 2 + math/binary/abs.go | 5 + math/binary/arithmeticmean.go | 2 + math/binary/bitcounter.go | 2 + math/binary/checkisnumberpoweroftwo.go | 2 + math/binary/fast_inverse_sqrt.go | 2 + math/binary/logarithm.go | 2 + math/binary/rbc.go | 2 + math/binary/reversebits.go | 2 + math/binary/sqrt.go | 2 + math/binary/xorsearch.go | 2 + math/binomialcoefficient.go | 2 + math/catalan/catalannumber.go | 2 + math/factorial/factorial.go | 2 + math/fibonacci/fibonacci.go | 2 + math/gcd/extended.go | 2 + math/gcd/extendedgcd.go | 5 + math/gcd/gcd.go | 3 + math/gcd/gcditerative.go | 3 + math/geometry/distance.go | 2 + math/krishnamurthy.go | 2 + math/liouville.go | 2 + math/matrix/add.go | 5 + math/matrix/checkequal.go | 3 + math/matrix/copy.go | 6 + math/matrix/determinant.go | 2 + math/matrix/multiply.go | 5 + math/matrix/strassenmatrixmultiply.go | 2 + math/max/bitwisemax.go | 2 + math/median.go | 2 + math/mobius.go | 2 + math/mode.go | 2 + math/modular/exponentiation.go | 2 + math/modular/inverse.go | 2 + math/moserdebruijnsequence/sequence.go | 2 + math/pascal/pascaltriangle.go | 2 + math/perfectnumber.go | 2 + math/permutation/heaps.go | 5 + math/permutation/next_permutation.go | 2 + math/pi/montecarlopi.go | 2 + math/pi/spigotpi.go | 2 + math/pollard.go | 2 + math/power/powvialogarithm.go | 2 + math/prime/millerrabintest.go | 3 +- math/prime/primecheck.go | 2 + math/prime/primefactorization.go | 5 + math/prime/sieve2.go | 2 + math/prime/twin.go | 2 + math/pronicnumber.go | 6 +- other/nested/nestedbrackets.go | 3 + other/password/generator.go | 3 + search/jump.go | 2 + sort/bogosort.go | 3 +- sort/combSort.go | 3 + sort/countingsort.go | 3 + sort/exchangesort.go | 3 + sort/heapsort.go | 6 + sort/insertionsort.go | 6 + sort/mergesort.go | 6 + sort/patiencesort.go | 3 + sort/pigeonholesort.go | 2 + sort/quicksort.go | 3 + sort/radixsort.go | 3 + sort/simplesort.go | 3 + 114 files changed, 385 insertions(+), 72 deletions(-) diff --git a/cache/lru.go b/cache/lru.go index 8c27cfc1f..b9578a9fe 100644 --- a/cache/lru.go +++ b/cache/lru.go @@ -1,3 +1,10 @@ +// lru.go +// description : Least Recently Used (LRU) cache +// details : A Least Recently Used (LRU) cache is a type of cache algorithm used to manage memory within a computer. The LRU algorithm is designed to remove the least recently used items first when the cache reaches its limit. +// time complexity : O(1) +// space complexity : O(n) +// ref : https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU) + package cache import ( diff --git a/checksum/crc8.go b/checksum/crc8.go index fc73a9a40..29c6214b5 100644 --- a/checksum/crc8.go +++ b/checksum/crc8.go @@ -3,6 +3,8 @@ // details: // A cyclic redundancy check (CRC) is an error-detecting code commonly used in digital networks // and storage devices to detect accidental changes to raw data. +// time complexity: O(n) +// space complexity: O(1) // See more [CRC](https://en.wikipedia.org/wiki/Cyclic_redundancy_check) // author(s) [red_byte](https://github.com/i-redbyte) // see crc8_test.go diff --git a/checksum/luhn.go b/checksum/luhn.go index e9f7dd6b1..b28236d95 100644 --- a/checksum/luhn.go +++ b/checksum/luhn.go @@ -1,6 +1,8 @@ // lunh.go // description: Luhn algorithm // details: is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, etc [Lunh](https://en.wikipedia.org/wiki/Luhn_algorithm) +// time complexity: O(n) +// space complexity: O(1) // author(s) [red_byte](https://github.com/i-redbyte) // see lunh_test.go diff --git a/cipher/caesar/caesar.go b/cipher/caesar/caesar.go index 8c1cb47f7..f2f945e71 100644 --- a/cipher/caesar/caesar.go +++ b/cipher/caesar/caesar.go @@ -1,4 +1,8 @@ // Package caesar is the shift cipher +// description: Caesar cipher +// details : Caesar cipher is a type of substitution cipher in which each letter in the plaintext is shifted a certain number of places down the alphabet. +// time complexity: O(n) +// space complexity: O(n) // ref: https://en.wikipedia.org/wiki/Caesar_cipher package caesar diff --git a/cipher/diffiehellman/diffiehellmankeyexchange.go b/cipher/diffiehellman/diffiehellmankeyexchange.go index 2621e3cca..8a779efd4 100644 --- a/cipher/diffiehellman/diffiehellmankeyexchange.go +++ b/cipher/diffiehellman/diffiehellmankeyexchange.go @@ -1,4 +1,8 @@ // Package diffiehellman implements Diffie-Hellman Key Exchange Algorithm +// description: Diffie-Hellman key exchange +// details : Diffie-Hellman key exchange is a method of securely exchanging cryptographic keys over a public channel by combining private keys of two parties to generate a shared secret key. +// time complexity: O(log(n)) +// space complexity: O(1) // for more information watch : https://www.youtube.com/watch?v=NmM9HA2MQGI package diffiehellman diff --git a/cipher/polybius/polybius.go b/cipher/polybius/polybius.go index 7b57ab344..e022dabae 100644 --- a/cipher/polybius/polybius.go +++ b/cipher/polybius/polybius.go @@ -1,4 +1,8 @@ // Package polybius is encrypting method with polybius square +// description: Polybius square +// details : The Polybius algorithm is a simple algorithm that is used to encode a message by converting each letter to a pair of numbers. +// time complexity: O(n) +// space complexity: O(n) // ref: https://en.wikipedia.org/wiki/Polybius_square#Hybrid_Polybius_Playfair_Cipher package polybius diff --git a/cipher/railfence/railfence.go b/cipher/railfence/railfence.go index f1ea11928..aeeaed3fa 100644 --- a/cipher/railfence/railfence.go +++ b/cipher/railfence/railfence.go @@ -1,3 +1,9 @@ +// railfence.go +// description: Rail Fence Cipher +// details: The rail fence cipher is a an encryption algorithm that uses a rail fence pattern to encode a message. it is a type of transposition cipher that rearranges the characters of the plaintext to form the ciphertext. +// time complexity: O(n) +// space complexity: O(n) +// ref: https://en.wikipedia.org/wiki/Rail_fence_cipher package railfence import ( diff --git a/cipher/rot13/rot13.go b/cipher/rot13/rot13.go index 9b822332b..d50d97c53 100644 --- a/cipher/rot13/rot13.go +++ b/cipher/rot13/rot13.go @@ -1,4 +1,8 @@ // Package rot13 is a simple letter substitution cipher that replaces a letter with the 13th letter after it in the alphabet. +// description: ROT13 +// details: ROT13 is a simple letter substitution cipher that replaces a letter with the 13th letter after it in the alphabet. it is a special case of the Caesar cipher +// time complexity: O(n) +// space complexity: O(n) // ref: https://en.wikipedia.org/wiki/ROT13 package rot13 diff --git a/cipher/rsa/rsa.go b/cipher/rsa/rsa.go index 44cf767b3..1eb506e2c 100644 --- a/cipher/rsa/rsa.go +++ b/cipher/rsa/rsa.go @@ -6,6 +6,8 @@ // thus both the Encrypt and Decrypt are not a production // ready implementation. The OpenSSL implementation of RSA // also adds a padding which is not present in this algorithm. +// time complexity: O(n) +// space complexity: O(n) // author(s) [Taj](https://github.com/tjgurwara99) // see rsa_test.go diff --git a/cipher/rsa/rsa2.go b/cipher/rsa/rsa2.go index 2eceddf10..18b6e1391 100644 --- a/cipher/rsa/rsa2.go +++ b/cipher/rsa/rsa2.go @@ -2,6 +2,8 @@ rsa2.go description: RSA encryption and decryption including key generation details: [RSA wiki](https://en.wikipedia.org/wiki/RSA_(cryptosystem)) +time complexity: O(n) +space complexity: O(1) author(s): [ddaniel27](https://github.com/ddaniel27) */ package rsa diff --git a/cipher/transposition/transposition.go b/cipher/transposition/transposition.go index 6365ffa4a..7a18293ea 100644 --- a/cipher/transposition/transposition.go +++ b/cipher/transposition/transposition.go @@ -2,6 +2,8 @@ // description: Transposition cipher // details: // Implementation "Transposition cipher" is a method of encryption by which the positions held by units of plaintext (which are commonly characters or groups of characters) are shifted according to a regular system, so that the ciphertext constitutes a permutation of the plaintext [Transposition cipher](https://en.wikipedia.org/wiki/Transposition_cipher) +// time complexity: O(n) +// space complexity: O(n) // author(s) [red_byte](https://github.com/i-redbyte) // see transposition_test.go diff --git a/cipher/xor/xor.go b/cipher/xor/xor.go index 0296c97ce..61181b1aa 100644 --- a/cipher/xor/xor.go +++ b/cipher/xor/xor.go @@ -1,4 +1,8 @@ // Package xor is an encryption algorithm that operates the exclusive disjunction(XOR) +// description: XOR encryption +// details: The XOR encryption is an algorithm that operates the exclusive disjunction(XOR) on each character of the plaintext with a given key +// time complexity: O(n) +// space complexity: O(n) // ref: https://en.wikipedia.org/wiki/XOR_cipher package xor diff --git a/compression/huffmancoding.go b/compression/huffmancoding.go index f6515872e..ecdaa1fb2 100644 --- a/compression/huffmancoding.go +++ b/compression/huffmancoding.go @@ -3,6 +3,8 @@ // details: // We implement the linear-time 2-queue method described here https://en.wikipedia.org/wiki/Huffman_coding. // It assumes that the list of symbol-frequencies is sorted. +// time complexity: O(n) +// space complexity: O(n) // author(s) [pedromsrocha](https://github.com/pedromsrocha) // see also huffmancoding_test.go diff --git a/compression/rlecoding.go b/compression/rlecoding.go index c08d3e4bd..9aa9399bb 100644 --- a/compression/rlecoding.go +++ b/compression/rlecoding.go @@ -3,6 +3,9 @@ rlecoding.go description: run length encoding and decoding details: Run-length encoding (RLE) is a simple form of data compression in which runs of data are stored as a single data value and count, rather than as the original run. This is useful when the data contains many repeated values. For example, the data "WWWWWWWWWWWWBWWWWWWWWWWWWBBB" can be compressed to "12W1B12W3B". The algorithm is simple and can be implemented in a few lines of code. +time complexity: O(n) +space complexity: O(n) +ref: https://en.wikipedia.org/wiki/Run-length_encoding author(s) [ddaniel27](https://github.com/ddaniel27) */ package compression diff --git a/conversion/base64.go b/conversion/base64.go index 8828f6a7a..bf3a383c5 100644 --- a/conversion/base64.go +++ b/conversion/base64.go @@ -1,6 +1,8 @@ // base64.go // description: The base64 encoding algorithm as defined in the RFC4648 standard. // author: [Paul Leydier] (https://github.com/paul-leydier) +// time complexity: O(n) +// space complexity: O(n) // ref: https://datatracker.ietf.org/doc/html/rfc4648#section-4 // ref: https://en.wikipedia.org/wiki/Base64 // see base64_test.go diff --git a/conversion/binarytodecimal.go b/conversion/binarytodecimal.go index 6181d3bc2..aa308e6a8 100644 --- a/conversion/binarytodecimal.go +++ b/conversion/binarytodecimal.go @@ -9,6 +9,8 @@ Date: 19-Oct-2021 // https://en.wikipedia.org/wiki/Decimal // Function receives a Binary Number as string and returns the Decimal number as integer. // Supported Binary number range is 0 to 2^(31-1). +// time complexity: O(n) +// space complexity: O(1) package conversion diff --git a/conversion/decimaltobinary.go b/conversion/decimaltobinary.go index e74765708..54153b0d2 100644 --- a/conversion/decimaltobinary.go +++ b/conversion/decimaltobinary.go @@ -8,6 +8,8 @@ Date: 14-Oct-2021 // https://en.wikipedia.org/wiki/Binary_number // Function receives a integer as a Decimal number and returns the Binary number. // Supported integer value range is 0 to 2^(31 -1). +// time complexity: O(log(n)) +// space complexity: O(1) package conversion diff --git a/conversion/inttoroman.go b/conversion/inttoroman.go index c1764db1f..1b9c39a0e 100644 --- a/conversion/inttoroman.go +++ b/conversion/inttoroman.go @@ -1,3 +1,9 @@ +// inttoroman.go +// description: Convert an integer to a roman numeral +// details: This program converts an integer to a roman numeral. The program uses a lookup array to convert the integer to a roman numeral. +// time complexity: O(1) +// space complexity: O(1) + package conversion import ( diff --git a/conversion/rgbhex.go b/conversion/rgbhex.go index d050edc8d..3d7265d2c 100644 --- a/conversion/rgbhex.go +++ b/conversion/rgbhex.go @@ -1,5 +1,7 @@ // rgbhex.go // description: convert hex input to red, green and blue and vice versa +// time complexity: O(1) +// space complexity: O(1) // author(s) [darmiel](https://github.com/darmiel) // see rgbhex_test.go diff --git a/conversion/romantoint.go b/conversion/romantoint.go index dda0000f3..629952320 100644 --- a/conversion/romantoint.go +++ b/conversion/romantoint.go @@ -3,6 +3,8 @@ // Function receives a string as a roman number and outputs an integer // Maximum output will be 3999 // Only standard form is supported +// time complexity: O(n) +// space complexity: O(1) package conversion diff --git a/dynamic/abbreviation.go b/dynamic/abbreviation.go index ebfe92171..e5b7ad6e9 100644 --- a/dynamic/abbreviation.go +++ b/dynamic/abbreviation.go @@ -11,6 +11,8 @@ // Given a = "ABcde" and b = "ABCD" // We can capitalize "c" and "d" in a to get "ABCde" then delete all the lowercase letters (which is only "e") in a to get "ABCD" which equals b. // Author: [duongoku](https://github.com/duongoku) +// Time Complexity: O(n*m) where n is the length of a and m is the length of b +// Space Complexity: O(n*m) where n is the length of a and m is the length of b // See abbreviation_test.go for test cases package dynamic diff --git a/dynamic/binomialcoefficient.go b/dynamic/binomialcoefficient.go index b26dbcbbf..fed664fd5 100644 --- a/dynamic/binomialcoefficient.go +++ b/dynamic/binomialcoefficient.go @@ -1,3 +1,8 @@ +// binomialcoefficient.go +// description: Implementation of the binomial coefficient using dynamic programming +// details: The binomial coefficient C(n, k) is the number of ways to choose a subset of k elements from a set of n elements. The binomial coefficient is calculated using the formula C(n, k) = C(n-1, k-1) + C(n-1, k) with base cases C(n, 0) = C(n, n) = 1. +// time complexity: O(n*k) where n is the number of elements and k is the number of elements to choose +// space complexity: O(n*k) where n is the number of elements and k is the number of elements to choose package dynamic import "github.com/TheAlgorithms/Go/math/min" diff --git a/dynamic/catalan.go b/dynamic/catalan.go index 7e0076fba..2f53c3edb 100644 --- a/dynamic/catalan.go +++ b/dynamic/catalan.go @@ -1,5 +1,7 @@ //The Catalan numbers are a sequence of positive integers that appear in many counting -//problems in combinatorics. +// problems in combinatorics. +// time complexity: O(n²) +// space complexity: O(n) //reference: https://brilliant.org/wiki/catalan-numbers/ package dynamic diff --git a/dynamic/coinchange.go b/dynamic/coinchange.go index 1098ea4e1..2ba6064ab 100644 --- a/dynamic/coinchange.go +++ b/dynamic/coinchange.go @@ -1,3 +1,9 @@ +// coinchange.go +// description: Implementation of the coin change problem using dynamic programming +// details: The coin change problem is a problem that asks for the number of ways to make change for a given amount of money using a given set of coins. The problem can be solved using dynamic programming. +// time complexity: O(n*m) where n is the number of coins and m is the amount of money +// space complexity: O(m) where m is the amount of money + package dynamic // CoinChange finds the number of possible combinations of coins diff --git a/dynamic/editdistance.go b/dynamic/editdistance.go index 92abe9583..6d4778a3e 100644 --- a/dynamic/editdistance.go +++ b/dynamic/editdistance.go @@ -1,4 +1,6 @@ // EDIT DISTANCE PROBLEM +// time complexity: O(m * n) where m and n are lengths of the strings, first and second respectively. +// space complexity: O(m * n) where m and n are lengths of the strings, first and second respectively. // https://www.geeksforgeeks.org/edit-distance-dp-5/ // https://leetcode.com/problems/edit-distance/ diff --git a/dynamic/fibonacci.go b/dynamic/fibonacci.go index 288b82b1c..6025b59f0 100644 --- a/dynamic/fibonacci.go +++ b/dynamic/fibonacci.go @@ -1,3 +1,7 @@ +// fibonacci.go +// description: Implementation of the Fibonacci sequence using dynamic programming +// time complexity: O(n) +// space complexity: O(1) package dynamic // https://www.geeksforgeeks.org/program-for-nth-fibonacci-number/ diff --git a/dynamic/knapsack.go b/dynamic/knapsack.go index b07bf6b40..6b637ca54 100644 --- a/dynamic/knapsack.go +++ b/dynamic/knapsack.go @@ -2,6 +2,9 @@ package dynamic // Knapsack Problem // https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/ +// https://en.wikipedia.org/wiki/Knapsack_problem +// time complexity: O(n*maxWeight) +// space complexity: O(n*maxWeight) import ( "math" diff --git a/dynamic/longestcommonsubsequence.go b/dynamic/longestcommonsubsequence.go index 739aeb8c1..087d986e0 100644 --- a/dynamic/longestcommonsubsequence.go +++ b/dynamic/longestcommonsubsequence.go @@ -1,6 +1,9 @@ // LONGEST COMMON SUBSEQUENCE // DP - 4 // https://www.geeksforgeeks.org/longest-common-subsequence-dp-4/ +// https://leetcode.com/problems/longest-common-subsequence/ +// time complexity: O(m*n) where m and n are lengths of the strings +// space complexity: O(m*n) where m and n are lengths of the strings package dynamic diff --git a/dynamic/longestincreasingsubsequence.go b/dynamic/longestincreasingsubsequence.go index cce099510..a1e70e53f 100644 --- a/dynamic/longestincreasingsubsequence.go +++ b/dynamic/longestincreasingsubsequence.go @@ -1,3 +1,9 @@ +// longestincreasingsubsequence.go +// description: Implementation of the Longest Increasing Subsequence using dynamic programming +// reference: https://en.wikipedia.org/wiki/Longest_increasing_subsequence +// time complexity: O(n^2) +// space complexity: O(n) + package dynamic import ( diff --git a/dynamic/longestpalindromicsubsequence.go b/dynamic/longestpalindromicsubsequence.go index bc5df07b6..700fb8f87 100644 --- a/dynamic/longestpalindromicsubsequence.go +++ b/dynamic/longestpalindromicsubsequence.go @@ -1,4 +1,6 @@ // longest palindromic subsequence +// time complexity: O(n^2) +// space complexity: O(n^2) // http://www.geeksforgeeks.org/dynamic-programming-set-12-longest-palindromic-subsequence/ package dynamic diff --git a/dynamic/matrixmultiplication.go b/dynamic/matrixmultiplication.go index 373936e62..10fccdbda 100644 --- a/dynamic/matrixmultiplication.go +++ b/dynamic/matrixmultiplication.go @@ -1,6 +1,8 @@ // matrix chain multiplication problem // https://en.wikipedia.org/wiki/Matrix_chain_multiplication // www.geeksforgeeks.org/dynamic_programming-set-8-matrix-chain-multiplication/ +// time complexity: O(n^3) +// space complexity: O(n^2) package dynamic diff --git a/dynamic/rodcutting.go b/dynamic/rodcutting.go index ce721e18b..4d37f4ecc 100644 --- a/dynamic/rodcutting.go +++ b/dynamic/rodcutting.go @@ -1,6 +1,8 @@ // Solution to Rod cutting problem // https://en.wikipedia.org/wiki/Cutting_stock_problem // http://www.geeksforgeeks.org/dynamic-programming-set-13-cutting-a-rod/ +// time complexity: O(n^2) +// space complexity: O(n) package dynamic diff --git a/dynamic/subsetsum.go b/dynamic/subsetsum.go index 1f7da69b8..db04c32de 100644 --- a/dynamic/subsetsum.go +++ b/dynamic/subsetsum.go @@ -1,7 +1,8 @@ //Given a set of non-negative integers, and a (positive) value sum, //determine if there is a subset of the given set with sum //equal to given sum. -//Complexity: O(n*sum) +// time complexity: O(n*sum) +// space complexity: O(n*sum) //references: https://www.geeksforgeeks.org/subset-sum-problem-dp-25/ package dynamic diff --git a/dynamic/traprainwater.go b/dynamic/traprainwater.go index 4f87e1cd7..3b1c6a966 100644 --- a/dynamic/traprainwater.go +++ b/dynamic/traprainwater.go @@ -5,6 +5,8 @@ // It uses dynamic programming to precompute the maximum height of bars to the left and right of each position. // Then, it iterates through the array to calculate the amount of trapped rainwater at each position based on the minimum of the left and right maximum heights. // Finally, it sums up the trapped rainwater for all positions and returns the total amount. +// time complexity: O(n) +// space complexity: O(n) // author(s) [TruongNhanNguyen (SOZEL)](https://github.com/TruongNhanNguyen) package dynamic diff --git a/dynamic/uniquepaths.go b/dynamic/uniquepaths.go index 3dc96a8e7..978a81c7e 100644 --- a/dynamic/uniquepaths.go +++ b/dynamic/uniquepaths.go @@ -1,4 +1,6 @@ // See https://leetcode.com/problems/unique-paths/ +// time complexity: O(m*n) where m and n are the dimensions of the grid +// space complexity: O(m*n) where m and n are the dimensions of the grid // author: Rares Mateizer (https://github.com/rares985) package dynamic diff --git a/graph/articulationpoints.go b/graph/articulationpoints.go index 618107890..26b3a6b78 100644 --- a/graph/articulationpoints.go +++ b/graph/articulationpoints.go @@ -1,56 +1,48 @@ +// Package graph provides algorithms to analyze graph structures. package graph import "github.com/TheAlgorithms/Go/math/min" +// apHelper stores auxiliary data used to identify articulation points in a graph. type apHelper struct { - is_ap []bool - visited []bool - child_cnt []int - discovery_time []int - earliest_discovery []int + isAP []bool + visited []bool + childCount []int + discoveryTime []int + earliestDiscovery []int } -// ArticulationPoint is a function to identify articulation points in a graph. -// The function takes the graph as an argument and returns a boolean slice -// which indicates whether a vertex is an articulation point or not. +// ArticulationPoint identifies articulation points in a graph. It returns a boolean slice +// where each element indicates whether a vertex is an articulation point. // Worst Case Time Complexity: O(|V| + |E|) // Auxiliary Space: O(|V|) -// reference: https://en.wikipedia.org/wiki/Biconnected_component and https://cptalks.quora.com/Cut-Vertex-Articulation-point +// Reference: https://en.wikipedia.org/wiki/Biconnected_component and https://cptalks.quora.com/Cut-Vertex-Articulation-point func ArticulationPoint(graph *Graph) []bool { - // time variable to keep track of the time of discovery_time of a vertex + // Time variable to keep track of the discovery time of a vertex time := 0 - //initialize all the variables + // Initialize apHelper instance with the required data structures apHelperInstance := &apHelper{ - is_ap: make([]bool, graph.vertices), - visited: make([]bool, graph.vertices), - child_cnt: make([]int, graph.vertices), - // integer slice to store the discovery time of a vertex as we traverse - // the graph in a depth first manner - discovery_time: make([]int, graph.vertices), - // integer slice to store the earliest discovered vertex reachable from a vertex - earliest_discovery: make([]int, graph.vertices), + isAP: make([]bool, graph.vertices), + visited: make([]bool, graph.vertices), + childCount: make([]int, graph.vertices), + discoveryTime: make([]int, graph.vertices), + earliestDiscovery: make([]int, graph.vertices), } - articulationPointHelper( - apHelperInstance, - 0, - -1, - &time, - graph, - ) - if apHelperInstance.child_cnt[0] == 1 { - // if the root has only one child, it is not an articulation point - apHelperInstance.is_ap[0] = false + // Start traversal from the root (0) + articulationPointHelper(apHelperInstance, 0, -1, &time, graph) + + // Check if the root has only one child, making it non-articulate + if apHelperInstance.childCount[0] == 1 { + apHelperInstance.isAP[0] = false } - return apHelperInstance.is_ap + return apHelperInstance.isAP } -// articulationPointHelper is a recursive function to traverse the graph -// and mark articulation points. Based on the depth first search transversal -// of the graph, however modified to keep track and update the -// `child_cnt`, `discovery_time` and `earliest_discovery` slices defined above +// articulationPointHelper recursively traverses the graph using DFS and marks articulation points. +// It updates `childCount`, `discoveryTime`, and `earliestDiscovery` slices for the given vertex. func articulationPointHelper( apHelperInstance *apHelper, vertex int, @@ -60,41 +52,38 @@ func articulationPointHelper( ) { apHelperInstance.visited[vertex] = true - // Mark the time of discovery of a vertex - // set the earliest discovery time to the discovered time - // increment the time - apHelperInstance.discovery_time[vertex] = *time - apHelperInstance.earliest_discovery[vertex] = apHelperInstance.discovery_time[vertex] + // Set discovery and earliest discovery times for the vertex + apHelperInstance.discoveryTime[vertex] = *time + apHelperInstance.earliestDiscovery[vertex] = *time *time++ - for next_vertex := range graph.edges[vertex] { - if next_vertex == parent { + for nextVertex := range graph.edges[vertex] { + if nextVertex == parent { continue } - if apHelperInstance.visited[next_vertex] { - apHelperInstance.earliest_discovery[vertex] = min.Int( - apHelperInstance.earliest_discovery[vertex], - apHelperInstance.discovery_time[next_vertex], + if apHelperInstance.visited[nextVertex] { + // Update the earliest discovery time to the smallest reachable discovery time + apHelperInstance.earliestDiscovery[vertex] = min.Int( + apHelperInstance.earliestDiscovery[vertex], + apHelperInstance.discoveryTime[nextVertex], ) continue } - apHelperInstance.child_cnt[vertex]++ - articulationPointHelper( - apHelperInstance, - next_vertex, - vertex, - time, - graph, - ) - apHelperInstance.earliest_discovery[vertex] = min.Int( - apHelperInstance.earliest_discovery[vertex], - apHelperInstance.earliest_discovery[next_vertex], + // Increment child count and perform recursive traversal for DFS + apHelperInstance.childCount[vertex]++ + articulationPointHelper(apHelperInstance, nextVertex, vertex, time, graph) + + // Update the earliest discovery time post DFS + apHelperInstance.earliestDiscovery[vertex] = min.Int( + apHelperInstance.earliestDiscovery[vertex], + apHelperInstance.earliestDiscovery[nextVertex], ) - if apHelperInstance.earliest_discovery[next_vertex] >= apHelperInstance.discovery_time[vertex] { - apHelperInstance.is_ap[vertex] = true - } + // Mark vertex as articulation point if condition meets + if apHelperInstance.earliestDiscovery[nextVertex] >= apHelperInstance.discoveryTime[vertex] { + apHelperInstance.isAP[vertex] = true + } } } diff --git a/graph/bellmanford.go b/graph/bellmanford.go index f130a4e06..d1db29c5c 100644 --- a/graph/bellmanford.go +++ b/graph/bellmanford.go @@ -3,6 +3,8 @@ // It is slower than Dijkstra but capable of handling negative edge weights. // https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm // Implementation is based on the book 'Introduction to Algorithms' (CLRS) +// time complexity: O(V*E) where V is the number of vertices and E is the number of edges in the graph +// space complexity: O(V) where V is the number of vertices in the graph package graph diff --git a/graph/breadthfirstsearch.go b/graph/breadthfirstsearch.go index 7325c06a3..bac2f04cc 100644 --- a/graph/breadthfirstsearch.go +++ b/graph/breadthfirstsearch.go @@ -3,8 +3,8 @@ package graph // BreadthFirstSearch is an algorithm for traversing and searching graph data structures. // It starts at an arbitrary node of a graph, and explores all of the neighbor nodes // at the present depth prior to moving on to the nodes at the next depth level. -// Worst-case performance O(|V|+|E|)=O(b^{d})}O(|V|+|E|)=O(b^{d}) -// Worst-case space complexity O(|V|)=O(b^{d})}O(|V|)=O(b^{d}) +// Worst-case performance O(|V|+|E|)=O(b^{d})}O(|V|+|E|)=O(b^{d}) where |V| is the number of vertices and |E| is the number of edges in the graph and b is the branching factor of the graph (the average number of successors of a node). d is the depth of the goal node. +// Worst-case space complexity O(|V|)=O(b^{d})}O(|V|)=O(b^{d}) where |V| is the number of vertices and |E| is the number of edges in the graph and b is the branching factor of the graph (the average number of successors of a node). d is the depth of the goal node. // reference: https://en.wikipedia.org/wiki/Breadth-first_search func BreadthFirstSearch(start, end, nodes int, edges [][]int) (isConnected bool, distance int) { queue := make([]int, 0) diff --git a/graph/coloring/backtracking.go b/graph/coloring/backtracking.go index 1ae657d45..885b79e28 100644 --- a/graph/coloring/backtracking.go +++ b/graph/coloring/backtracking.go @@ -1,4 +1,6 @@ // This file contains the graph coloring implementation using backtracking +// time complexity: O(V^V) where V is the number of vertices in the graph +// space complexity: O(V) where V is the number of vertices in the graph // Author(s): [Shivam](https://github.com/Shivam010) package coloring diff --git a/graph/coloring/bfs.go b/graph/coloring/bfs.go index d73b0ed31..22f0bc899 100644 --- a/graph/coloring/bfs.go +++ b/graph/coloring/bfs.go @@ -1,4 +1,6 @@ // This file contains the graph coloring implementation using BFS +// time complexity: O(V+E) where V is the number of vertices and E is the number of edges in the graph +// space complexity: O(V) where V is the number of vertices in the graph // Author(s): [Shivam](https://github.com/Shivam010) package coloring diff --git a/graph/coloring/bipartite.go b/graph/coloring/bipartite.go index f27c7dc54..1f0e3c62c 100644 --- a/graph/coloring/bipartite.go +++ b/graph/coloring/bipartite.go @@ -1,7 +1,13 @@ package coloring +// Bipartite.go +// description: Implementation of the Bipartite graph coloring algorithm +// details: A bipartite graph is a graph whose vertices can be divided into two disjoint sets U and V such that every edge connects a vertex in U to one in V. The Bipartite graph coloring algorithm is used to determine if a graph is bipartite or not. +// time complexity: O(V+E) where V is the number of vertices and E is the number of edges in the graph +// space complexity: O(V) where V is the number of vertices in the graph + func (g *Graph) TryBipartiteColoring() map[int]Color { - // 0 is uncolored, 1/2 is colors + // 0 is uncolored, 1/2 are colors colors := make(map[int]Color) visited := make(map[int]bool) @@ -10,8 +16,8 @@ func (g *Graph) TryBipartiteColoring() map[int]Color { visited[i] = false } - var color_node func(int) - color_node = func(s int) { + var colorNode func(int) + colorNode = func(s int) { visited[s] = true coloring := []Color{0, 2, 1} @@ -20,7 +26,7 @@ func (g *Graph) TryBipartiteColoring() map[int]Color { colors[n] = coloring[colors[s]] } if !visited[n] { - color_node(n) + colorNode(n) } } } @@ -28,7 +34,7 @@ func (g *Graph) TryBipartiteColoring() map[int]Color { for i := range g.edges { if colors[i] == 0 { colors[i] = 1 - color_node(i) + colorNode(i) } } diff --git a/graph/coloring/greedy.go b/graph/coloring/greedy.go index 907395d96..07ca6af1b 100644 --- a/graph/coloring/greedy.go +++ b/graph/coloring/greedy.go @@ -1,4 +1,6 @@ // This file contains the graph coloring implementation using Greedy Approach. +// time complexity: O(V^2) where V is the number of vertices in the graph +// space complexity: O(V) where V is the number of vertices in the graph // Author(s): [Shivam](https://github.com/Shivam010) package coloring diff --git a/graph/cycle.go b/graph/cycle.go index cf45d1854..c55cd6bed 100644 --- a/graph/cycle.go +++ b/graph/cycle.go @@ -1,5 +1,7 @@ // cycle.go // this file handle algorithm that related to cycle in graph +// time complexity: O(V+E) where V is the number of vertices and E is the number of edges in the graph +// space complexity: O(V) where V is the number of vertices in the graph // reference: https://en.wikipedia.org/wiki/Cycle_(graph_theory) // [kiarash hajian](https://github.com/kiarash8112) diff --git a/graph/depthfirstsearch.go b/graph/depthfirstsearch.go index c035c79f5..76bed3106 100644 --- a/graph/depthfirstsearch.go +++ b/graph/depthfirstsearch.go @@ -1,3 +1,9 @@ +// depthfirstsearch.go +// description: this file contains the implementation of the depth first search algorithm +// details: Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node and explores as far as possible along each branch before backtracking. +// time complexity: O(n) +// space complexity: O(n) + package graph func GetIdx(target int, nodes []int) int { diff --git a/graph/dijkstra.go b/graph/dijkstra.go index 685cf02e8..ad7a8a778 100644 --- a/graph/dijkstra.go +++ b/graph/dijkstra.go @@ -1,3 +1,10 @@ +// dijkstra.go +// description: this file contains the implementation of the Dijkstra algorithm +// details: Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph, which may represent, for example, road networks. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later. The algorithm exists in many variants; Dijkstra's original variant found the shortest path between two nodes, but a more common variant fixes a single node as the "source" node and finds shortest paths from the source to all other nodes in the graph, producing a shortest-path tree. +// time complexity: O((V+E) log V) where V is the number of vertices and E is the number of edges in the graph +// space complexity: O(V) where V is the number of vertices in the graph +// reference: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm + package graph import "github.com/TheAlgorithms/Go/sort" diff --git a/graph/floydwarshall.go b/graph/floydwarshall.go index 33b97a2c1..9544e587b 100644 --- a/graph/floydwarshall.go +++ b/graph/floydwarshall.go @@ -1,4 +1,6 @@ // Floyd-Warshall algorithm +// time complexity: O(V^3) where V is the number of vertices in the graph +// space complexity: O(V^2) where V is the number of vertices in the graph // https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm package graph diff --git a/graph/lowestcommonancestor.go b/graph/lowestcommonancestor.go index 8ce575187..891bac50d 100644 --- a/graph/lowestcommonancestor.go +++ b/graph/lowestcommonancestor.go @@ -3,6 +3,8 @@ // detail: // Let `T` be a tree. The LCA of `u` and `v` in T is the shared ancestor of `u` and `v` // that is located farthest from the root. +// time complexity: O(n log n) where n is the number of vertices in the tree +// space complexity: O(n log n) where n is the number of vertices in the tree // references: [cp-algorithms](https://cp-algorithms.com/graph/lca_binary_lifting.html) // author(s) [Dat](https://github.com/datbeohbbh) // see lowestcommonancestor_test.go for a test implementation. diff --git a/graph/topological.go b/graph/topological.go index 9e99470b7..8cd1856e0 100644 --- a/graph/topological.go +++ b/graph/topological.go @@ -1,3 +1,10 @@ +// topological.go +// description: Topological sort +// details: Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge u v, vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG. +// time complexity: O(V+E) where V is the number of vertices and E is the number of edges in the graph +// space complexity: O(V) where V is the number of vertices in the graph +// reference: https://en.wikipedia.org/wiki/Topological_sorting + package graph // Topological assumes that graph given is valid and that its diff --git a/hashing/sha256/sha256.go b/hashing/sha256/sha256.go index 9061f3801..0dd38a557 100644 --- a/hashing/sha256/sha256.go +++ b/hashing/sha256/sha256.go @@ -1,5 +1,7 @@ // sha256.go // description: The sha256 cryptographic hash function as defined in the RFC6234 standard. +// time complexity: O(n) +// space complexity: O(n) // author: [Paul Leydier] (https://github.com/paul-leydier) // ref: https://datatracker.ietf.org/doc/html/rfc6234 // ref: https://en.wikipedia.org/wiki/SHA-2 diff --git a/math/aliquotsum.go b/math/aliquotsum.go index 6e3af6cc0..cc6bd326a 100644 --- a/math/aliquotsum.go +++ b/math/aliquotsum.go @@ -5,6 +5,8 @@ // is the sum of all proper divisors of n, // that is, all divisors of n other than n itself // wikipedia: https://en.wikipedia.org/wiki/Aliquot_sum +// time complexity: O(n) +// space complexity: O(1) // author: Akshay Dubey (https://github.com/itsAkshayDubey) // see aliquotsum_test.go diff --git a/math/armstrong/isarmstrong.go b/math/armstrong/isarmstrong.go index 68055dee8..851b9612a 100644 --- a/math/armstrong/isarmstrong.go +++ b/math/armstrong/isarmstrong.go @@ -1,6 +1,8 @@ // isarmstrong.go // description: Checks if the given number is armstrong number or not // details: An Armstrong number is a n-digit number that is equal to the sum of each of its digits taken to the nth power. +// time complexity: O(log(n)) +// space complexity: O(1) // ref: https://mathlair.allfunandgames.ca/armstrong.php // author: Kavitha J diff --git a/math/binary/abs.go b/math/binary/abs.go index 9762c7c8a..5c95fa5e8 100644 --- a/math/binary/abs.go +++ b/math/binary/abs.go @@ -1,3 +1,8 @@ +// abs.go +// description: returns absolute value using binary operation +// time complexity: O(1) +// space complexity: O(1) + package binary // Abs returns absolute value using binary operation diff --git a/math/binary/arithmeticmean.go b/math/binary/arithmeticmean.go index 9fa56560e..f7812b6be 100644 --- a/math/binary/arithmeticmean.go +++ b/math/binary/arithmeticmean.go @@ -2,6 +2,8 @@ // description: Arithmetic mean // details: // The most common type of average is the arithmetic mean. If n numbers are given, each number denoted by ai (where i = 1,2, ..., n), the arithmetic mean is the sum of the as divided by n or - [Arithmetic mean](https://en.wikipedia.org/wiki/Average#Arithmetic_mean) +// time complexity: O(1) +// space complexity: O(1) // author(s) [red_byte](https://github.com/i-redbyte) // see arithmeticmean_test.go diff --git a/math/binary/bitcounter.go b/math/binary/bitcounter.go index 33e24256d..baf8dcd7b 100644 --- a/math/binary/bitcounter.go +++ b/math/binary/bitcounter.go @@ -2,6 +2,8 @@ // description: Counts the number of set bits in a number // details: // For unsigned integer number N, return the number of bits set to 1 - [Bit numbering](https://en.wikipedia.org/wiki/Bit_numbering) +// time complexity: O(log(n)) +// space complexity: O(1) // author(s) [red_byte](https://github.com/i-redbyte) // see bitcounter_test.go diff --git a/math/binary/checkisnumberpoweroftwo.go b/math/binary/checkisnumberpoweroftwo.go index 29c023e5a..d836396f5 100644 --- a/math/binary/checkisnumberpoweroftwo.go +++ b/math/binary/checkisnumberpoweroftwo.go @@ -2,6 +2,8 @@ // description: Is the number a power of two // details: // Checks if a number is a power of two- [Power of two](https://en.wikipedia.org/wiki/Power_of_two) +// time complexity: O(1) +// space complexity: O(1) // author(s) [red_byte](https://github.com/i-redbyte) // see checkisnumberpoweroftwo_test.go diff --git a/math/binary/fast_inverse_sqrt.go b/math/binary/fast_inverse_sqrt.go index 24ee41a82..c8689b451 100644 --- a/math/binary/fast_inverse_sqrt.go +++ b/math/binary/fast_inverse_sqrt.go @@ -1,4 +1,6 @@ // Calculating the inverse square root +// time complexity: O(1) +// space complexity: O(1) // [See more](https://en.wikipedia.org/wiki/Fast_inverse_square_root) package binary diff --git a/math/binary/logarithm.go b/math/binary/logarithm.go index 205b0235b..3d73ac428 100644 --- a/math/binary/logarithm.go +++ b/math/binary/logarithm.go @@ -1,4 +1,6 @@ // author(s) [red_byte](https://github.com/i-redbyte) +// time complexity: O(1) +// space complexity: O(1) // see logarithm_test.go package binary diff --git a/math/binary/rbc.go b/math/binary/rbc.go index c0a6e78f1..b6ef45f68 100644 --- a/math/binary/rbc.go +++ b/math/binary/rbc.go @@ -2,6 +2,8 @@ // description: Reflected binary code (RBC) // details: // The reflected binary code (RBC), also known just as reflected binary (RB) or Gray code after Frank Gray, is an ordering of the binary numeral system such that two successive values differ in only one bit (binary digit). - [RBC](https://en.wikipedia.org/wiki/Gray_code) +// time complexity: O(n) +// space complexity: O(n) // author(s) [red_byte](https://github.com/i-redbyte) // see rbc_test.go diff --git a/math/binary/reversebits.go b/math/binary/reversebits.go index f4367cc94..751fbcfd5 100644 --- a/math/binary/reversebits.go +++ b/math/binary/reversebits.go @@ -2,6 +2,8 @@ // description: Reverse bits // details: // Reverse the bits of an integer number +// time complexity: O(log(n)) +// space complexity: O(1) // author(s) [red_byte](https://github.com/i-redbyte) // see reversebits_test.go diff --git a/math/binary/sqrt.go b/math/binary/sqrt.go index 520338eb6..8c967bf91 100644 --- a/math/binary/sqrt.go +++ b/math/binary/sqrt.go @@ -3,6 +3,8 @@ // details: // Calculating the square root using binary operations and a magic number 0x5f3759df [See more](https://en.wikipedia.org/wiki/Fast_inverse_square_root) // author(s) [red_byte](https://github.com/i-redbyte) +// time complexity: O(1) +// space complexity: O(1) // see sqrt_test.go package binary diff --git a/math/binary/xorsearch.go b/math/binary/xorsearch.go index b16b7846b..9e914da83 100644 --- a/math/binary/xorsearch.go +++ b/math/binary/xorsearch.go @@ -2,6 +2,8 @@ // description: Find a missing number in a sequence // details: // Given an array A containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. - [xor](https://en.wikipedia.org/wiki/Exclusive_or) +// time complexity: O(n) +// space complexity: O(1) // author(s) [red_byte](https://github.com/i-redbyte) // see xorsearch_test.go diff --git a/math/binomialcoefficient.go b/math/binomialcoefficient.go index d3c123a6f..614f4b7db 100644 --- a/math/binomialcoefficient.go +++ b/math/binomialcoefficient.go @@ -4,6 +4,8 @@ // a binomial coefficient C(n,k) gives number ways // in which k objects can be chosen from n objects. // wikipedia: https://en.wikipedia.org/wiki/Binomial_coefficient +// time complexity: O(k) or O(n-k) whichever is smaller (O(n) in worst case) +// space complexity: O(1) // author: Akshay Dubey (https://github.com/itsAkshayDubey) // see binomialcoefficient_test.go diff --git a/math/catalan/catalannumber.go b/math/catalan/catalannumber.go index 9ea8acb49..5f81cb9f4 100644 --- a/math/catalan/catalannumber.go +++ b/math/catalan/catalannumber.go @@ -2,6 +2,8 @@ // description: Returns the Catalan number // details: // In combinatorial mathematics, the Catalan numbers are a sequence of natural numbers that occur in various counting problems, often involving recursively defined objects. - [Catalan number](https://en.wikipedia.org/wiki/Catalan_number) +// time complexity: O(n) +// space complexity: O(1) // The input is the number of the Catalan number n, at the output we get the value of the number // author(s) [red_byte](https://github.com/i-redbyte) // see catalannumber_test.go diff --git a/math/factorial/factorial.go b/math/factorial/factorial.go index 7472da8de..6703e6c75 100644 --- a/math/factorial/factorial.go +++ b/math/factorial/factorial.go @@ -2,6 +2,8 @@ // description: Calculating factorial // details: // The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n - [Factorial](https://en.wikipedia.org/wiki/Factorial) +// time complexity: O(n) +// space complexity: O(1) // author(s) [red_byte](https://github.com/i-redbyte) // see factorial_test.go diff --git a/math/fibonacci/fibonacci.go b/math/fibonacci/fibonacci.go index 92e69ae08..c2fd1b2e2 100644 --- a/math/fibonacci/fibonacci.go +++ b/math/fibonacci/fibonacci.go @@ -2,6 +2,8 @@ // description: Get the nth Fibonacci Number // details: // In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. [Fibonacci number](https://en.wikipedia.org/wiki/Fibonacci_number) +// time complexity: O(log n) +// space complexity: O(1) // author(s) [red_byte](https://github.com/i-redbyte) // see fibonacci_test.go diff --git a/math/gcd/extended.go b/math/gcd/extended.go index eea32e9e9..229630141 100644 --- a/math/gcd/extended.go +++ b/math/gcd/extended.go @@ -3,6 +3,8 @@ // details: // A simple implementation of Extended GCD algorithm, that returns GCD, a and b // which solves ax + by = gcd(a, b) +// time complexity: O(log(min(a, b))) where a and b are the two numbers +// space complexity: O(log(min(a, b))) where a and b are the two numbers // author(s) [Taj](https://github.com/tjgurwara99) // see extended_test.go diff --git a/math/gcd/extendedgcd.go b/math/gcd/extendedgcd.go index 004a91ead..0f5a750dc 100644 --- a/math/gcd/extendedgcd.go +++ b/math/gcd/extendedgcd.go @@ -1,3 +1,8 @@ +// extendedgcd.go +// description: Implementation of Extended GCD Algorithm +// time complexity: O(log(min(a, b))) where a and b are the two numbers +// space complexity: O(log(min(a, b))) where a and b are the two numbers + package gcd // ExtendedRecursive finds and returns gcd(a, b), x, y satisfying a*x + b*y = gcd(a, b). diff --git a/math/gcd/gcd.go b/math/gcd/gcd.go index e76e8396f..2045d0901 100644 --- a/math/gcd/gcd.go +++ b/math/gcd/gcd.go @@ -1,3 +1,6 @@ +// time complexity: O(log(min(a, b))) where a and b are the two numbers +// space complexity: O(1) + package gcd // Recursive finds and returns the greatest common divisor of a given integer. diff --git a/math/gcd/gcditerative.go b/math/gcd/gcditerative.go index e87dfce6f..96c217cfa 100644 --- a/math/gcd/gcditerative.go +++ b/math/gcd/gcditerative.go @@ -1,3 +1,6 @@ +// time complexity: O(log(min(a, b))) where a and b are the two numbers +// space complexity: O(1) + package gcd // Iterative Faster iterative version of GcdRecursive without holding up too much of the stack diff --git a/math/geometry/distance.go b/math/geometry/distance.go index 5b1c3c82e..0baf64562 100644 --- a/math/geometry/distance.go +++ b/math/geometry/distance.go @@ -1,5 +1,7 @@ // distance.go // Find Euclidean distance between two points +// time complexity: O(n) where n is the number of dimensions +// space complexity: O(1) // author(s) [Chetan Patil](https://github.com/Chetan07j) // Package geometry contains geometric algorithms diff --git a/math/krishnamurthy.go b/math/krishnamurthy.go index 641dfbf92..63bb6dbf6 100644 --- a/math/krishnamurthy.go +++ b/math/krishnamurthy.go @@ -2,6 +2,8 @@ // description: A program which contains the function that returns true if a given number is Krishnamurthy number or not. // details: A number is a Krishnamurthy number if the sum of all the factorials of the digits is equal to the number. // Ex: 1! = 1, 145 = 1! + 4! + 5! +// time complexity: O(log n) +// space complexity: O(1) // author(s): [GooMonk](https://github.com/GooMonk) // see krishnamurthy_test.go package math diff --git a/math/liouville.go b/math/liouville.go index 908a4deac..55d2c95f4 100644 --- a/math/liouville.go +++ b/math/liouville.go @@ -6,6 +6,8 @@ // λ(n) = +1 if n is a positive integer with an even number of prime factors. // λ(n) = −1 if n is a positive integer with an odd number of prime factors. // wikipedia: https://en.wikipedia.org/wiki/Liouville_function +// time complexity: O(log n) +// space complexity: O(1) // author: Akshay Dubey (https://github.com/itsAkshayDubey) // see liouville_test.go diff --git a/math/matrix/add.go b/math/matrix/add.go index 34fdf98be..d5d79efdd 100644 --- a/math/matrix/add.go +++ b/math/matrix/add.go @@ -1,3 +1,8 @@ +// add.go +// description: Add two matrices +// time complexity: O(n^2) +// space complexity: O(n^2) + package matrix import ( diff --git a/math/matrix/checkequal.go b/math/matrix/checkequal.go index c6bde8597..005c657af 100644 --- a/math/matrix/checkequal.go +++ b/math/matrix/checkequal.go @@ -3,6 +3,9 @@ package matrix // CheckEqual checks if the current matrix is equal to another matrix (m2). // Two matrices are considered equal if they have the same dimensions and // all their elements are equal. +// time complexity: O(n*m) where n and m are the dimensions of the matrix +// space complexity: O(1) + func (m1 Matrix[T]) CheckEqual(m2 Matrix[T]) bool { if !m1.MatchDimensions(m2) { return false diff --git a/math/matrix/copy.go b/math/matrix/copy.go index 2b9949750..11afb9237 100644 --- a/math/matrix/copy.go +++ b/math/matrix/copy.go @@ -1,3 +1,9 @@ +// copy.go +// description: Copy a matrix +// details: This function creates a new matrix with the same dimensions as the original matrix and copies all the elements from the original matrix to the new matrix. +// time complexity: O(n*m) where n and m are the dimensions of the matrix +// space complexity: O(n*m) where n and m are the dimensions of the matrix + package matrix import "sync" diff --git a/math/matrix/determinant.go b/math/matrix/determinant.go index d2570f8f8..371b1fa0a 100644 --- a/math/matrix/determinant.go +++ b/math/matrix/determinant.go @@ -2,6 +2,8 @@ // description: This method finds the determinant of a matrix. // details: For a theoretical explanation as for what the determinant // represents, see the [Wikipedia Article](https://en.wikipedia.org/wiki/Determinant) +// time complexity: O(n!) where n is the number of rows and columns in the matrix. +// space complexity: O(n^2) where n is the number of rows and columns in the matrix. // author [Carter907](https://github.com/Carter907) // see determinant_test.go diff --git a/math/matrix/multiply.go b/math/matrix/multiply.go index 718a1b068..f59213da6 100644 --- a/math/matrix/multiply.go +++ b/math/matrix/multiply.go @@ -1,3 +1,8 @@ +// multiply.go +// description: Implementation of matrix multiplication +// time complexity: O(n^3) where n is the number of rows in the first matrix +// space complexity: O(n^2) where n is the number of rows in the first matrix + package matrix import ( diff --git a/math/matrix/strassenmatrixmultiply.go b/math/matrix/strassenmatrixmultiply.go index 86fb47809..9b5f01fdf 100644 --- a/math/matrix/strassenmatrixmultiply.go +++ b/math/matrix/strassenmatrixmultiply.go @@ -4,6 +4,8 @@ // This program takes two matrices as input and performs matrix multiplication // using the Strassen algorithm, which is an optimized divide-and-conquer // approach. It allows for efficient multiplication of large matrices. +// time complexity: O(n^2.81) +// space complexity: O(n^2) // author(s): Mohit Raghav(https://github.com/mohit07raghav19) // See strassenmatrixmultiply_test.go for test cases package matrix diff --git a/math/max/bitwisemax.go b/math/max/bitwisemax.go index b6c3a4623..a54f94654 100644 --- a/math/max/bitwisemax.go +++ b/math/max/bitwisemax.go @@ -3,6 +3,8 @@ // details: // implementation of finding the maximum of two numbers using only binary operations without using conditions // author(s) [red_byte](https://github.com/i-redbyte) +// time complexity: O(1) +// space complexity: O(1) // see bitwiseMax_test.go package max diff --git a/math/median.go b/math/median.go index d8cd02796..1b9dac834 100644 --- a/math/median.go +++ b/math/median.go @@ -1,5 +1,7 @@ // author(s) [jo3zeph](https://github.com/jo3zeph) // description: Find the median from a set of values +// time complexity: O(n log n) +// space complexity: O(1) // see median_test.go package math diff --git a/math/mobius.go b/math/mobius.go index 76b50da14..0f9fac639 100644 --- a/math/mobius.go +++ b/math/mobius.go @@ -7,6 +7,8 @@ // μ(n) = −1 if n is a square-free positive integer with an odd number of prime factors. // μ(n) = 0 if n has a squared prime factor. // wikipedia: https://en.wikipedia.org/wiki/M%C3%B6bius_function +// time complexity: O(n) +// space complexity: O(1) // author: Akshay Dubey (https://github.com/itsAkshayDubey) // see mobius_test.go diff --git a/math/mode.go b/math/mode.go index 164084bb6..87f233304 100644 --- a/math/mode.go +++ b/math/mode.go @@ -1,5 +1,7 @@ // mode.go // author(s): [CalvinNJK] (https://github.com/CalvinNJK) +// time complexity: O(n) +// space complexity: O(n) // description: Finding Mode Value In an Array // see mode.go diff --git a/math/modular/exponentiation.go b/math/modular/exponentiation.go index f71a8d3ff..85a397e4f 100644 --- a/math/modular/exponentiation.go +++ b/math/modular/exponentiation.go @@ -2,6 +2,8 @@ // description: Implementation of Modular Exponentiation Algorithm // details: // A simple implementation of Modular Exponentiation - [Modular Exponenetation wiki](https://en.wikipedia.org/wiki/Modular_exponentiation) +// time complexity: O(log(n)) where n is the exponent +// space complexity: O(1) // author(s) [Taj](https://github.com/tjgurwara99) // see exponentiation_test.go diff --git a/math/modular/inverse.go b/math/modular/inverse.go index 819ffda8d..f20d70b11 100644 --- a/math/modular/inverse.go +++ b/math/modular/inverse.go @@ -2,6 +2,8 @@ // description: Implementation of Modular Inverse Algorithm // details: // A simple implementation of Modular Inverse - [Modular Inverse wiki](https://en.wikipedia.org/wiki/Modular_multiplicative_inverse) +// time complexity: O(log(min(a, b))) where a and b are the two numbers +// space complexity: O(1) // author(s) [Taj](https://github.com/tjgurwara99) // see inverse_test.go diff --git a/math/moserdebruijnsequence/sequence.go b/math/moserdebruijnsequence/sequence.go index dd846b5a1..09d2c7776 100644 --- a/math/moserdebruijnsequence/sequence.go +++ b/math/moserdebruijnsequence/sequence.go @@ -1,5 +1,7 @@ // The Moser-de Bruijn sequence is the sequence obtained by // adding up the distinct powers of the number 4 (For example 1, 4, 16, 64, etc). +// time complexity: O(n) +// space complexity: O(n) // You can get more details on https://en.wikipedia.org/wiki/Moser%E2%80%93de_Bruijn_sequence. package moserdebruijnsequence diff --git a/math/pascal/pascaltriangle.go b/math/pascal/pascaltriangle.go index 12f227fd0..742b735d7 100644 --- a/math/pascal/pascaltriangle.go +++ b/math/pascal/pascaltriangle.go @@ -16,6 +16,8 @@ //1 10 45 120 210 252 210 120 45 10 1 //... // author(s) [red_byte](https://github.com/i-redbyte) +// time complexity: O(n^2) +// space complexity: O(n^2) // see pascaltriangle_test.go package pascal diff --git a/math/perfectnumber.go b/math/perfectnumber.go index e21c89b64..37be71a2b 100644 --- a/math/perfectnumber.go +++ b/math/perfectnumber.go @@ -7,6 +7,8 @@ // A number is called perfect, if it is a sum of its proper divisors, // cf. https://en.wikipedia.org/wiki/Perfect_number, // https://mathworld.wolfram.com/PerfectNumber.html +// time complexity: O(sqrt(n)) +// space complexity: O(1) // https://oeis.org/A000396 // author(s) [Piotr Idzik](https://github.com/vil02) // see perfectnumber_test.go diff --git a/math/permutation/heaps.go b/math/permutation/heaps.go index f3d79f464..5f4ebaed3 100644 --- a/math/permutation/heaps.go +++ b/math/permutation/heaps.go @@ -1,3 +1,8 @@ +// heaps.go +// description: Implementation of Heap's Algorithm for generating all permutations of n objects +// time complexity: O(n!) +// space complexity: O(n) + package permutation import ( diff --git a/math/permutation/next_permutation.go b/math/permutation/next_permutation.go index 62841bb3d..6aff11296 100644 --- a/math/permutation/next_permutation.go +++ b/math/permutation/next_permutation.go @@ -1,6 +1,8 @@ // A practice to find lexicographically next greater permutation of the given array of integers. // If there does not exist any greater permutation, then print the lexicographically smallest permutation of the given array. // The implementation below, finds the next permutation in linear time and constant memory and returns in place +// time complexity: O(n) +// space complexity: O(1) // Useful reference: https://www.geeksforgeeks.org/next-permutation/ package permutation diff --git a/math/pi/montecarlopi.go b/math/pi/montecarlopi.go index 7bbadf01c..a2e140f08 100644 --- a/math/pi/montecarlopi.go +++ b/math/pi/montecarlopi.go @@ -2,6 +2,8 @@ // description: Calculating pi by the Monte Carlo method // details: // implementations of Monte Carlo Algorithm for the calculating of Pi - [Monte Carlo method](https://en.wikipedia.org/wiki/Monte_Carlo_method) +// time complexity: O(n) +// space complexity: O(1) // author(s): [red_byte](https://github.com/i-redbyte), [Paul Leydier] (https://github.com/paul-leydier) // see montecarlopi_test.go diff --git a/math/pi/spigotpi.go b/math/pi/spigotpi.go index 6274176e4..349602a32 100644 --- a/math/pi/spigotpi.go +++ b/math/pi/spigotpi.go @@ -2,6 +2,8 @@ // description: A Spigot Algorithm for the Digits of Pi // details: // implementation of Spigot Algorithm for the Digits of Pi - [Spigot algorithm](https://en.wikipedia.org/wiki/Spigot_algorithm) +// time complexity: O(n) +// space complexity: O(n) // author(s) [red_byte](https://github.com/i-redbyte) // see spigotpi_test.go diff --git a/math/pollard.go b/math/pollard.go index 434a0f24f..a2cc42df1 100644 --- a/math/pollard.go +++ b/math/pollard.go @@ -2,6 +2,8 @@ // description: Pollard's rho algorithm // details: // implementation of Pollard's rho algorithm for integer factorization-[Pollard's rho algorithm](https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm) +// time complexity: O(n^(1/4)) +// space complexity: O(1) // author(s) [red_byte](https://github.com/i-redbyte) // see pollard_test.go diff --git a/math/power/powvialogarithm.go b/math/power/powvialogarithm.go index df1de157f..c2a395e3b 100644 --- a/math/power/powvialogarithm.go +++ b/math/power/powvialogarithm.go @@ -2,6 +2,8 @@ // description: Powers in terms of logarithms // details: // implementation of exponentiation using exponent and logarithm, without using loops - [Powers via logarithms wiki](https://en.wikipedia.org/wiki/Exponentiation#Powers_via_logarithms) +// time complexity: O(1) +// space complexity: O(1) // author(s) [red_byte](https://github.com/i-redbyte) // see powvialogarithm_test.go diff --git a/math/prime/millerrabintest.go b/math/prime/millerrabintest.go index 1f3a2ef72..5357bc45c 100644 --- a/math/prime/millerrabintest.go +++ b/math/prime/millerrabintest.go @@ -2,7 +2,8 @@ // One of the implementations is deterministic and the other is probabilistic. // The Miller-Rabin test is one of the simplest and fastest known primality // tests and is widely used. -// +// time complexity: O(k * log(n)^3) +// space complexity: O(1) // Authors: // [Taj](https://github.com/tjgurwara99) // [Rak](https://github.com/raklaptudirm) diff --git a/math/prime/primecheck.go b/math/prime/primecheck.go index 3e2a97280..7671c9cd0 100644 --- a/math/prime/primecheck.go +++ b/math/prime/primecheck.go @@ -3,6 +3,8 @@ package prime // A primality test is an algorithm for determining whether an input number is prime. Among other // fields of mathematics, it is used for cryptography. Unlike integer factorization, primality // tests do not generally give prime factors, only stating whether the input number is prime or not. +// time complexity: O(sqrt(n)) +// space complexity: O(1) // Source - Wikipedia https://en.wikipedia.org/wiki/Primality_test // TrialDivision tests whether a number is prime by trying to divide it by the numbers less than it. diff --git a/math/prime/primefactorization.go b/math/prime/primefactorization.go index c25522871..43479e439 100644 --- a/math/prime/primefactorization.go +++ b/math/prime/primefactorization.go @@ -1,3 +1,8 @@ +// primefactorization.go +// description: Prime factorization of a number +// time complexity: O(sqrt(n)) +// space complexity: O(sqrt(n)) + package prime // Factorize is a function that computes the exponents diff --git a/math/prime/sieve2.go b/math/prime/sieve2.go index 2e2e7b116..881ca20f5 100644 --- a/math/prime/sieve2.go +++ b/math/prime/sieve2.go @@ -1,5 +1,7 @@ /* sieve2.go - Sieve of Eratosthenes * Algorithm to generate prime numbers up to a limit + * time complexity: O(n log log n) + * space complexity: O(n) * Author: ddaniel27 */ package prime diff --git a/math/prime/twin.go b/math/prime/twin.go index 1326746e4..dfd861510 100644 --- a/math/prime/twin.go +++ b/math/prime/twin.go @@ -4,6 +4,8 @@ // For any integer n, twin prime is (n + 2) // if and only if both n and (n + 2) both are prime // wikipedia: https://en.wikipedia.org/wiki/Twin_prime +// time complexity: O(log n) +// space complexity: O(1) // author: Akshay Dubey (https://github.com/itsAkshayDubey) // see twin_test.go diff --git a/math/pronicnumber.go b/math/pronicnumber.go index 90850d28e..47d25d17c 100644 --- a/math/pronicnumber.go +++ b/math/pronicnumber.go @@ -4,18 +4,20 @@ // Pronic number: For any integer n, if there exists integer m // such that n = m * (m + 1) then n is called a pronic number. // wikipedia: https://en.wikipedia.org/wiki/Pronic_number +// time complexity: O(1) +// space complexity: O(1) // author: Akshay Dubey (https://github.com/itsAkshayDubey) // see pronicnumber_test.go package math -import stdMath "math" +import "math" // PronicNumber returns true if argument passed to the function is pronic and false otherwise. func PronicNumber(n int) bool { if n < 0 || n%2 == 1 { return false } - x := int(stdMath.Sqrt(float64(n))) + x := int(math.Sqrt(float64(n))) return n == x*(x+1) } diff --git a/other/nested/nestedbrackets.go b/other/nested/nestedbrackets.go index ec912938c..7f220a332 100644 --- a/other/nested/nestedbrackets.go +++ b/other/nested/nestedbrackets.go @@ -17,6 +17,9 @@ package nested // **Note** Providing characters other then brackets would return false, // despite brackets sequence in the string. Make sure to filter // input before usage. +// time complexity: O(n) +// space complexity: O(n) + func IsBalanced(input string) bool { if len(input) == 0 { return true diff --git a/other/password/generator.go b/other/password/generator.go index 46237364f..71bfdbd65 100644 --- a/other/password/generator.go +++ b/other/password/generator.go @@ -3,6 +3,9 @@ // This length is not fixed if you generate multiple passwords for the same range // Package password contains functions to help generate random passwords +// time complexity: O(n) +// space complexity: O(n) + package password import ( diff --git a/search/jump.go b/search/jump.go index 163dea55d..169620d9a 100644 --- a/search/jump.go +++ b/search/jump.go @@ -5,6 +5,8 @@ // before performing a linear search // reference: https://en.wikipedia.org/wiki/Jump_search // see jump_test.go for a test implementation, test function TestJump +// time complexity: O(sqrt(n)) +// space complexity: O(1) package search diff --git a/sort/bogosort.go b/sort/bogosort.go index a37d18ff8..165397155 100644 --- a/sort/bogosort.go +++ b/sort/bogosort.go @@ -1,7 +1,8 @@ // This is a pure Go implementation of the bogosort algorithm, // also known as permutation sort, stupid sort, slowsort, shotgun sort, or monkey sort. // Bogosort generates random permutations until it guesses the correct one. - +// worst-case time complexity: O((n+1)!) +// best-case time complexity: O(n) // More info on: https://en.wikipedia.org/wiki/Bogosort package sort diff --git a/sort/combSort.go b/sort/combSort.go index 76e523e34..15c3e8bab 100644 --- a/sort/combSort.go +++ b/sort/combSort.go @@ -1,4 +1,7 @@ // Implementation of comb sort algorithm, an improvement of bubble sort +// average time complexity: O(n^2 / 2^p) where p is the number of increments +// worst time complexity: O(n^2) +// space complexity: O(1) // Reference: https://www.geeksforgeeks.org/comb-sort/ package sort diff --git a/sort/countingsort.go b/sort/countingsort.go index d55a13798..55fbac7c0 100644 --- a/sort/countingsort.go +++ b/sort/countingsort.go @@ -1,6 +1,9 @@ // countingsort.go // description: Implementation of counting sort algorithm // details: A simple counting sort algorithm implementation +// worst-case time complexity: O(n + k) where n is the number of elements in the input array and k is the range of the input +// average-case time complexity: O(n + k) where n is the number of elements in the input array and k is the range of the input +// space complexity: O(n + k) // author [Phil](https://github.com/pschik) // see sort_test.go for a test implementation, test function TestQuickSort diff --git a/sort/exchangesort.go b/sort/exchangesort.go index cbf6b9b00..43ada90df 100644 --- a/sort/exchangesort.go +++ b/sort/exchangesort.go @@ -1,4 +1,7 @@ // Implementation of exchange sort algorithm, a variant of bubble sort +// average time complexity: O(n^2) +// worst time complexity: O(n^2) +// space complexity: O(1) // Reference: https://en.wikipedia.org/wiki/Sorting_algorithm#Exchange_sort package sort diff --git a/sort/heapsort.go b/sort/heapsort.go index 741b4e4e8..f4e581306 100644 --- a/sort/heapsort.go +++ b/sort/heapsort.go @@ -1,3 +1,9 @@ +// heapsort.go +// description: Implementation of heap sort algorithm +// worst-case time complexity: O(n log n) +// average-case time complexity: O(n log n) +// space complexity: O(1) + package sort import "github.com/TheAlgorithms/Go/constraints" diff --git a/sort/insertionsort.go b/sort/insertionsort.go index 13e45eeb9..55ac3130f 100644 --- a/sort/insertionsort.go +++ b/sort/insertionsort.go @@ -1,3 +1,9 @@ +// insertionsort.go +// description: Implementation of insertion sort algorithm +// worst-case time complexity: O(n^2) +// average-case time complexity: O(n^2) +// space complexity: O(1) + package sort import "github.com/TheAlgorithms/Go/constraints" diff --git a/sort/mergesort.go b/sort/mergesort.go index 9a875b428..550b13eea 100644 --- a/sort/mergesort.go +++ b/sort/mergesort.go @@ -1,3 +1,9 @@ +// mergesort.go +// description: Implementation of merge sort algorithm +// worst-case time complexity: O(n log n) +// average-case time complexity: O(n log n) +// space complexity: O(n) + package sort import ( diff --git a/sort/patiencesort.go b/sort/patiencesort.go index 746c84931..f1b2b46f2 100644 --- a/sort/patiencesort.go +++ b/sort/patiencesort.go @@ -4,6 +4,9 @@ // GeeksForGeeks article : https://www.geeksforgeeks.org/patience-sorting/ // Wikipedia article: https://en.wikipedia.org/wiki/Patience_sorting // authors [guuzaa](https://github.com/guuzaa) +// worst-case time complexity: O(n log n) +// average time complexity: O(n log n) +// space complexity: O(n) // see patiencesort.go package sort diff --git a/sort/pigeonholesort.go b/sort/pigeonholesort.go index 12941cdc9..5e735c8fd 100644 --- a/sort/pigeonholesort.go +++ b/sort/pigeonholesort.go @@ -1,5 +1,7 @@ // Pigeonhole algorithm's working at wikipedia. // https://en.wikipedia.org/wiki/Pigeonhole_sort +// time complexity: O(n + N) where n is the number of elements in the array and N is the range of input +// space complexity: O(N) package sort diff --git a/sort/quicksort.go b/sort/quicksort.go index 576db395a..8b05fe786 100644 --- a/sort/quicksort.go +++ b/sort/quicksort.go @@ -2,6 +2,9 @@ // description: Implementation of in-place quicksort algorithm // details: // A simple in-place quicksort algorithm implementation. [Wikipedia](https://en.wikipedia.org/wiki/Quicksort) +// worst time complexity: O(n^2) +// average time complexity: O(n log n) +// space complexity: O(log n) // author(s) [Taj](https://github.com/tjgurwara99) // see sort_test.go for a test implementation, test function TestQuickSort. diff --git a/sort/radixsort.go b/sort/radixsort.go index 4dbd21d64..28ddee8bf 100644 --- a/sort/radixsort.go +++ b/sort/radixsort.go @@ -2,6 +2,9 @@ // description: Implementation of in-place radixsort algorithm // details: // A simple in-place quicksort algorithm implementation. [Wikipedia](https://en.wikipedia.org/wiki/Radix_sort) +// worst time complexity: O(n * k) where n is the number of elements in the input array and k is the number of digits in the largest number +// average time complexity: O(n * k) where n is the number of elements in the input array and k is the number of digits in the largest number +// space complexity: O(n) package sort diff --git a/sort/simplesort.go b/sort/simplesort.go index ec54c1db0..fc60f33de 100644 --- a/sort/simplesort.go +++ b/sort/simplesort.go @@ -5,6 +5,9 @@ // An improved version is included with slight changes to make the sort slightly more efficient // reference: https://arxiv.org/abs/2110.01111v1 // see sort_test.go for a test implementation, test function TestSimple and TestImprovedSimple +// worst-case time complexity: O(n^2) +// average-case time complexity: O(n^2) +// space complexity: O(1) package sort