DSAA Homework 2

Exercises

3.4

a

by the definition
32n=6432n
2n=2n+6
n’=n+6

c

by the definition

8n=648n
n=64n


3.8

c

For the all values of n > 1, c4nlogn+c5nc4nlogn+c5nlogn(c4+c5)nlogn . Therefore, by the definition, T(n) is in O(n) for n0=1 and c=c4+c5
For all n > 1, c4nlogn+c5nc4n+c5n(c4+c5)n . Therefore, by the definition, T(n) is in Ω(n) for n0=1 and c=c4+c5 .

d

For the all values of n > 29, c62n+c7n6(c6+c7)2n . Therefore, by the definition, T(n) is in O(n) for n0=29 and c=c4+c5 .
For the all values of n > 29, c62n+c7n6(c6+c7)n6 . Therefore, by the definition, T(n) is in Ω(n) for n0=29 and c=c4+c5 .


3.11

b

limnnlogn2=limnn4= , so f(n) is in Ω(g(n))

e

limnnlogn+nlogn=limnn= , so f(n) is in Ω(g(n))

h

limn2n10n2= , so f(n) is in Ω(g(n))


3.12

a

Θ(1)

b

Θ(n)

c

Θ(n2)

d

Θ(n2)

e

Θ(nlogn)

f

Θ(nlogn)

g

Θ(n2logn)


Projects

3.2

#include<cstdio>
#include<cstdlib>
#include<ctime>
const int maxn = 1e7 + 100;
int a[maxn];
int random(int n){
    return rand() % n;
}
int SeSearch(int val, int n){
    for(int i = 0; i < n; i++)
        if(a[i] == val) return i;
    return -1;
}
int BiSearch(int val, int n){
    int l = 0, r = n;
    while(r - l > 1){
        int m = (l + r) / 2;
        if(a[m] > val) r = m;
        else l = m;
    }
    return a[l] == val ? l : -1;
}
int main(){
    for(int i = 0; i < maxn; i++) a[i] = i;
    const int T = 100000;
    int tmp;
    clock_t begin, end;
    for(int n = 10, k = 1; k <= 7; n *= 10, k++){
        printf("i:%d ", k);
        begin = clock();
        for(int i = 0; i < T; i++)
            tmp = SeSearch(random(n), n);
        end = clock();
        printf("%.2lf ", (double)(end - begin) / CLOCKS_PER_SEC);

        begin = clock();
        for(int i = 0; i < T; i++)
            tmp = BiSearch(random(n), n);
        end = clock();
        printf("%.2lf\n", (double)(end - begin) / CLOCKS_PER_SEC);
    }
    return 0;
}

这里写图片描述

iSequentialBinary
10.020.01
20.020.02
30.170.02
41.570.03
55.500.03
65.300.03
75.440.03

这里写图片描述

So binary search always faster than sequential search.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值