Created
October 20, 2023 01:26
-
-
Save EmmanuelOga/0460fcbc411981f2135ea4a7942f468e to your computer and use it in GitHub Desktop.
Simple recursive algorithm and its cost.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
var cost int | |
func f2(s, e int) int { | |
cost += 1 | |
if e <= s { | |
return 1 | |
} | |
mid := s + ((e - s) / 2) | |
return f2(s, mid) + f2(mid+1, e) | |
} | |
func main() { | |
for i := 1; i < 1000; i++ { | |
cost = 0 | |
f2(0, i) | |
fmt.Printf("%d\t%d\t\n", i, cost) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment