Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use int instead of float for the example code of log time complexity #1164

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions codes/c/chapter_computational_complexity/time_complexity.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ int expRecur(int n) {
}

/* 对数阶(循环实现) */
int logarithmic(float n) {
int logarithmic(int n) {
int count = 0;
while (n > 1) {
n = n / 2;
Expand All @@ -100,14 +100,14 @@ int logarithmic(float n) {
}

/* 对数阶(递归实现) */
int logRecur(float n) {
int logRecur(int n) {
if (n <= 1)
return 0;
return logRecur(n / 2) + 1;
}

/* 线性对数阶 */
int linearLogRecur(float n) {
int linearLogRecur(int n) {
if (n <= 1)
return 1;
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
Expand Down
12 changes: 6 additions & 6 deletions codes/cpp/chapter_computational_complexity/time_complexity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int expRecur(int n) {
}

/* 对数阶(循环实现) */
int logarithmic(float n) {
int logarithmic(int n) {
int count = 0;
while (n > 1) {
n = n / 2;
Expand All @@ -96,14 +96,14 @@ int logarithmic(float n) {
}

/* 对数阶(递归实现) */
int logRecur(float n) {
int logRecur(int n) {
if (n <= 1)
return 0;
return logRecur(n / 2) + 1;
}

/* 线性对数阶 */
int linearLogRecur(float n) {
int linearLogRecur(int n) {
if (n <= 1)
return 1;
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
Expand Down Expand Up @@ -153,12 +153,12 @@ int main() {
count = expRecur(n);
cout << "指数阶(递归实现)的操作数量 = " << count << endl;

count = logarithmic((float)n);
count = logarithmic(n);
cout << "对数阶(循环实现)的操作数量 = " << count << endl;
count = logRecur((float)n);
count = logRecur(n);
cout << "对数阶(递归实现)的操作数量 = " << count << endl;

count = linearLogRecur((float)n);
count = linearLogRecur(n);
cout << "线性对数阶(递归实现)的操作数量 = " << count << endl;

count = factorialRecur(n);
Expand Down
14 changes: 7 additions & 7 deletions codes/csharp/chapter_computational_complexity/time_complexity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class time_complexity {
void Algorithm(int n) {
int a = 1; // +0(技巧 1)
a += n; // +0(技巧 1)
// +n(技巧 2)
// +n(技巧 2)
for (int i = 0; i < 5 * n + 1; i++) {
Console.WriteLine(0);
}
Expand Down Expand Up @@ -118,7 +118,7 @@ int ExpRecur(int n) {
}

/* 对数阶(循环实现) */
int Logarithmic(float n) {
int Logarithmic(int n) {
int count = 0;
while (n > 1) {
n /= 2;
Expand All @@ -128,13 +128,13 @@ int Logarithmic(float n) {
}

/* 对数阶(递归实现) */
int LogRecur(float n) {
int LogRecur(int n) {
if (n <= 1) return 0;
return LogRecur(n / 2) + 1;
}

/* 线性对数阶 */
int LinearLogRecur(float n) {
int LinearLogRecur(int n) {
if (n <= 1) return 1;
int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);
for (int i = 0; i < n; i++) {
Expand Down Expand Up @@ -181,12 +181,12 @@ public void Test() {
count = ExpRecur(n);
Console.WriteLine("指数阶(递归实现)的操作数量 = " + count);

count = Logarithmic((float)n);
count = Logarithmic(n);
Console.WriteLine("对数阶(循环实现)的操作数量 = " + count);
count = LogRecur((float)n);
count = LogRecur(n);
Console.WriteLine("对数阶(递归实现)的操作数量 = " + count);

count = LinearLogRecur((float)n);
count = LinearLogRecur(n);
Console.WriteLine("线性对数阶(递归实现)的操作数量 = " + count);

count = FactorialRecur(n);
Expand Down
12 changes: 6 additions & 6 deletions codes/dart/chapter_computational_complexity/time_complexity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,25 @@ int expRecur(int n) {
}

/* 对数阶(循环实现) */
int logarithmic(num n) {
int logarithmic(int n) {
int count = 0;
while (n > 1) {
n = n / 2;
n = n ~/ 2;
count++;
}
return count;
}

/* 对数阶(递归实现) */
int logRecur(num n) {
int logRecur(int n) {
if (n <= 1) return 0;
return logRecur(n / 2) + 1;
return logRecur(n ~/ 2) + 1;
}

/* 线性对数阶 */
int linearLogRecur(num n) {
int linearLogRecur(int n) {
if (n <= 1) return 1;
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
int count = linearLogRecur(n ~/ 2) + linearLogRecur(n ~/ 2);
for (var i = 0; i < n; i++) {
count++;
}
Expand Down
8 changes: 4 additions & 4 deletions codes/go/chapter_computational_complexity/time_complexity.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func expRecur(n int) int {
}

/* 对数阶(循环实现)*/
func logarithmic(n float64) int {
func logarithmic(n int) int {
count := 0
for n > 1 {
n = n / 2
Expand All @@ -97,20 +97,20 @@ func logarithmic(n float64) int {
}

/* 对数阶(递归实现)*/
func logRecur(n float64) int {
func logRecur(n int) int {
if n <= 1 {
return 0
}
return logRecur(n/2) + 1
}

/* 线性对数阶 */
func linearLogRecur(n float64) int {
func linearLogRecur(n int) int {
if n <= 1 {
return 1
}
count := linearLogRecur(n/2) + linearLogRecur(n/2)
for i := 0.0; i < n; i++ {
for i := 0; i < n; i++ {
count++
}
return count
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ func TestTimeComplexity(t *testing.T) {
count = expRecur(n)
fmt.Println("指数阶(递归实现)的操作数量 =", count)

count = logarithmic(float64(n))
count = logarithmic(n)
fmt.Println("对数阶(循环实现)的操作数量 =", count)
count = logRecur(float64(n))
count = logRecur(n)
fmt.Println("对数阶(递归实现)的操作数量 =", count)

count = linearLogRecur(float64(n))
count = linearLogRecur(n)
fmt.Println("线性对数阶(递归实现)的操作数量 =", count)

count = factorialRecur(n)
Expand Down
12 changes: 6 additions & 6 deletions codes/java/chapter_computational_complexity/time_complexity.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static int expRecur(int n) {
}

/* 对数阶(循环实现) */
static int logarithmic(float n) {
static int logarithmic(int n) {
int count = 0;
while (n > 1) {
n = n / 2;
Expand All @@ -97,14 +97,14 @@ static int logarithmic(float n) {
}

/* 对数阶(递归实现) */
static int logRecur(float n) {
static int logRecur(int n) {
if (n <= 1)
return 0;
return logRecur(n / 2) + 1;
}

/* 线性对数阶 */
static int linearLogRecur(float n) {
static int linearLogRecur(int n) {
if (n <= 1)
return 1;
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
Expand Down Expand Up @@ -153,12 +153,12 @@ public static void main(String[] args) {
count = expRecur(n);
System.out.println("指数阶(递归实现)的操作数量 = " + count);

count = logarithmic((float) n);
count = logarithmic(n);
System.out.println("对数阶(循环实现)的操作数量 = " + count);
count = logRecur((float) n);
count = logRecur(n);
System.out.println("对数阶(递归实现)的操作数量 = " + count);

count = linearLogRecur((float) n);
count = linearLogRecur(n);
System.out.println("线性对数阶(递归实现)的操作数量 = " + count);

count = factorialRecur(n);
Expand Down
12 changes: 6 additions & 6 deletions codes/kotlin/chapter_computational_complexity/time_complexity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fun expRecur(n: Int): Int {
}

/* 对数阶(循环实现) */
fun logarithmic(n: Float): Int {
fun logarithmic(n: Int): Int {
var n1 = n
var count = 0
while (n1 > 1) {
Expand All @@ -98,14 +98,14 @@ fun logarithmic(n: Float): Int {
}

/* 对数阶(递归实现) */
fun logRecur(n: Float): Int {
fun logRecur(n: Int): Int {
if (n <= 1)
return 0
return logRecur(n / 2) + 1
}

/* 线性对数阶 */
fun linearLogRecur(n: Float): Int {
fun linearLogRecur(n: Int): Int {
if (n <= 1)
return 1
var count = linearLogRecur(n / 2) + linearLogRecur(n / 2)
Expand Down Expand Up @@ -153,12 +153,12 @@ fun main() {
count = expRecur(n)
println("指数阶(递归实现)的操作数量 = $count")

count = logarithmic(n.toFloat())
count = logarithmic(n)
println("对数阶(循环实现)的操作数量 = $count")
count = logRecur(n.toFloat())
count = logRecur(n)
println("对数阶(递归实现)的操作数量 = $count")

count = linearLogRecur(n.toFloat())
count = linearLogRecur(n)
println("线性对数阶(递归实现)的操作数量 = $count")

count = factorialRecur(n)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def exp_recur(n: int) -> int:
return exp_recur(n - 1) + exp_recur(n - 1) + 1


def logarithmic(n: float) -> int:
def logarithmic(n: int) -> int:
"""对数阶(循环实现)"""
count = 0
while n > 1:
Expand All @@ -86,14 +86,14 @@ def logarithmic(n: float) -> int:
return count


def log_recur(n: float) -> int:
def log_recur(n: int) -> int:
"""对数阶(递归实现)"""
if n <= 1:
return 0
return log_recur(n / 2) + 1


def linear_log_recur(n: float) -> int:
def linear_log_recur(n: int) -> int:
"""线性对数阶"""
if n <= 1:
return 1
Expand Down
24 changes: 12 additions & 12 deletions codes/rust/chapter_computational_complexity/time_complexity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,29 @@ fn exp_recur(n: i32) -> i32 {
}

/* 对数阶(循环实现) */
fn logarithmic(mut n: f32) -> i32 {
fn logarithmic(mut n: i32) -> i32 {
let mut count = 0;
while n > 1.0 {
n = n / 2.0;
while n > 1 {
n = n / 2;
count += 1;
}
count
}

/* 对数阶(递归实现) */
fn log_recur(n: f32) -> i32 {
if n <= 1.0 {
fn log_recur(n: i32) -> i32 {
if n <= 1 {
return 0;
}
log_recur(n / 2.0) + 1
log_recur(n / 2) + 1
}

/* 线性对数阶 */
fn linear_log_recur(n: f32) -> i32 {
if n <= 1.0 {
fn linear_log_recur(n: i32) -> i32 {
if n <= 1 {
return 1;
}
let mut count = linear_log_recur(n / 2.0) + linear_log_recur(n / 2.0);
let mut count = linear_log_recur(n / 2) + linear_log_recur(n / 2);
for _ in 0..n as i32 {
count += 1;
}
Expand Down Expand Up @@ -157,12 +157,12 @@ fn main() {
count = exp_recur(n);
println!("指数阶(递归实现)的操作数量 = {}", count);

count = logarithmic(n as f32);
count = logarithmic(n);
println!("对数阶(循环实现)的操作数量 = {}", count);
count = log_recur(n as f32);
count = log_recur(n);
println!("对数阶(递归实现)的操作数量 = {}", count);

count = linear_log_recur(n as f32);
count = linear_log_recur(n);
println!("线性对数阶(递归实现)的操作数量 = {}", count);

count = factorial_recur(n);
Expand Down
Loading
Loading