【Algo】复杂度分析 -- 最好, 最坏, 平均, 均摊 时间复杂度

Backto Algo Index

上一篇说到了 Big-O 分析法, 但是那只适合分析复杂度随着 n n n 增大的趋势, 实际生产中, 我们还需要知道更细节的指标, 比如

  • 最好情况时间复杂度 best case time complexity
  • 最坏情况时间复杂度 worst case time complexity
  • 平均情况时间复杂度 average case time complexity
  • 均摊时间复杂度 amortized time complexity

Best, Worst & Average

先上代码

// n 表示数组 array 的长度
int find(int[] array, int n, int x) {
   
   int i = 0;
   int pos = -1;
   for(; i < n; ++i) {
   
     if(array[i] == x) {
   
       pos = i;
       break;
     }
   }
   return pos;
}

很简单的, 在一个数组中 find 一个数是否存在, 逐一分析,

  • Best case : x = array[0], 一击即中, 复杂度 O ( 1 ) O(1) O(1)
  • Worst case : x = array[n-1], 或者 x not in array, 那么需要找 n n n 次, 复杂度 O ( n ) O(n) O(n)
  • Average case : 平均就必要有意思了, 需要看你怎么分配权重, 或者说概率
    • 均值分布, 一共有 n + 1 n+1 n+1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ICM-42670-P芯片中的算法(Algorithm)例程代码是一段用于演示ICM-42670-P芯片在实际应用中如何使用的示例代码。该代码主要包括以下几个模块: 1. 初始化模块 该模块主要用于初始化ICM-42670-P芯片,包括设置SPI或I2C接口、配置滤波器和测量范围等参数。具体代码如下: ``` void icm42670p_algo_init(void) { /* Initialize ICM-42670-P */ icm42670p_init(); /* Set accelerometer range to +/- 8g */ icm42670p_set_acc_range(ICM42670_ACC_RANGE_8G); /* Set gyroscope range to +/- 2000dps */ icm42670p_set_gyro_range(ICM42670_GYRO_RANGE_2000DPS); /* Enable low pass filter for accelerometer and gyroscope */ icm42670p_enable_acc_lpf(ICM42670_ACC_LPF_21HZ); icm42670p_enable_gyro_lpf(ICM42670_GYRO_LPF_21HZ); } ``` 该函数首先调用了icm42670p_init()函数,初始化ICM-42670-P芯片,然后设置了加速度计和陀螺仪的测量范围,以及启用低通滤波器。 2. 数据读取模块 该模块用于读取ICM-42670-P芯片中加速度计和陀螺仪的原始数据,并进行单位转换。具体代码如下: ``` void icm42670p_algo_read_data(float *ax, float *ay, float *az, float *gx, float *gy, float *gz) { int16_t raw_ax, raw_ay, raw_az, raw_gx, raw_gy, raw_gz; /* Read accelerometer and gyroscope data */ raw_ax = icm42670p_read_acc_x(); raw_ay = icm42670p_read_acc_y(); raw_az = icm42670p_read_acc_z(); raw_gx = icm42670p_read_gyro_x(); raw_gy = icm42670p_read_gyro_y(); raw_gz = icm42670p_read_gyro_z(); /* Convert raw data to physical units */ *ax = raw_ax * ICM42670_ACC_SENSITIVITY / 1000.0; *ay = raw_ay * ICM42670_ACC_SENSITIVITY / 1000.0; *az = raw_az * ICM42670_ACC_SENSITIVITY / 1000.0; *gx = raw_gx * ICM42670_GYRO_SENSITIVITY / 1000.0; *gy = raw_gy * ICM42670_GYRO_SENSITIVITY / 1000.0; *gz = raw_gz * ICM42670_GYRO_SENSITIVITY / 1000.0; } ``` 该函数首先调用icm42670p_read_acc_x()等函数读取加速度计和陀螺仪的原始数据,然后将其转换为物理单位(重力加速度和弧度/秒)。 3. 姿态解算模块 该模块主要用于计算物体在X、Y、Z三个轴上的倾斜角度。具体代码如下: ``` void icm42670p_algo_calc_angles(float ax, float ay, float az, float gx, float gy, float gz, float *roll, float *pitch, float *yaw) { float roll_rad, pitch_rad, yaw_rad; /* Calculate roll, pitch and yaw angles */ roll_rad = atan2(ay, az); pitch_rad = atan2(-ax, sqrt(ay*ay + az*az)); yaw_rad = atan2(gx, sqrt(gy*gy + gz*gz)); /* Convert angles to degrees */ *roll = roll_rad * 180 / PI; *pitch = pitch_rad * 180 / PI; *yaw = yaw_rad * 180 / PI; } ``` 该函数使用了三个公式,将原始数据转换为倾斜角度,并进行单位转换。 4. 主函数模块 主函数模块中调用了上述三个模块,实现了ICM-42670-P芯片的数据读取和姿态解算。具体代码如下: ``` int main(void) { float ax, ay, az, gx, gy, gz; float roll, pitch, yaw; /* Initialize algorithm */ icm42670p_algo_init(); while (1) { /* Read data from ICM-42670-P */ icm42670p_algo_read_data(&ax, &ay, &az, &gx, &gy, &gz); /* Calculate roll, pitch and yaw angles */ icm42670p_algo_calc_angles(ax, ay, az, gx, gy, gz, &roll, &pitch, &yaw); /* Output results */ printf("roll=%.2f, pitch=%.2f, yaw=%.2f\n", roll, pitch, yaw); } return 0; } ``` 该函数使用了一个while循环,不断读取ICM-42670-P芯片中的数据,并计算出倾斜角度,最后将结果输出到终端或其他外设上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值