cpp-algo-cases/chapter_computational_compl.../time_complexity.cpp

169 lines
3.8 KiB
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: time_complexity.cpp
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int constant(int n) {
int count = 0;
int size = 100000;
for (int i = 0; i < size; i++)
count++;
return count;
}
/* <20><><EFBFBD>Խ<EFBFBD> */
int linear(int n) {
int count = 0;
for (int i = 0; i < n; i++)
count++;
return count;
}
/* <20><><EFBFBD>Խף<D4BD><D7A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>飩 */
int arrayTraversal(vector<int> &nums) {
int count = 0;
// ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȳ<EFBFBD><C8B3><EFBFBD><EFBFBD><EFBFBD>
for (int num : nums) {
count++;
}
return count;
}
/* ƽ<><C6BD><EFBFBD><EFBFBD> */
int quadratic(int n) {
int count = 0;
// ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȳ<EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD>ϵ
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
count++;
}
}
return count;
}
/* ƽ<><C6BD><EFBFBD>ף<EFBFBD>ð<EFBFBD><C3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int bubbleSort(vector<int> &nums) {
int count = 0; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ [0, i]
for (int i = nums.size() - 1; i > 0; i--) {
// <20><>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [0, i] <20>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҷ<EFBFBD>
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// <20><><EFBFBD><EFBFBD> nums[j] <20><> nums[j + 1]
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
count += 3; // Ԫ<>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3 <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>
}
}
}
return count;
}
/* ָ<><D6B8><EFBFBD>ף<EFBFBD>ѭ<EFBFBD><D1AD>ʵ<EFBFBD>֣<EFBFBD> */
int exponential(int n) {
int count = 0, base = 1;
// ϸ<><CFB8>ÿ<EFBFBD><C3BF>һ<EFBFBD><D2BB>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>γ<EFBFBD><CEB3><EFBFBD><EFBFBD><EFBFBD> 1, 2, 4, 8, ..., 2^(n-1)
for (int i = 0; i < n; i++) {
for (int j = 0; j < base; j++) {
count++;
}
base *= 2;
}
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count;
}
/* ָ<><D6B8><EFBFBD>ף<EFBFBD><D7A3>ݹ<EFBFBD>ʵ<EFBFBD>֣<EFBFBD> */
int expRecur(int n) {
if (n == 1)
return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
/* <20><><EFBFBD><EFBFBD><EFBFBD>ף<EFBFBD>ѭ<EFBFBD><D1AD>ʵ<EFBFBD>֣<EFBFBD> */
int logarithmic(float n) {
int count = 0;
while (n > 1) {
n = n / 2;
count++;
}
return count;
}
/* <20><><EFBFBD><EFBFBD><EFBFBD>ף<EFBFBD><D7A3>ݹ<EFBFBD>ʵ<EFBFBD>֣<EFBFBD> */
int logRecur(float n) {
if (n <= 1)
return 0;
return logRecur(n / 2) + 1;
}
/* <20><><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD> */
int linearLogRecur(float n) {
if (n <= 1)
return 1;
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
for (int i = 0; i < n; i++) {
count++;
}
return count;
}
/* <20>׳˽ף<CBBD><D7A3>ݹ<EFBFBD>ʵ<EFBFBD>֣<EFBFBD> */
int factorialRecur(int n) {
if (n == 0)
return 1;
int count = 0;
// <20><> 1 <20><><EFBFBD><EFBFBD><EFBFBD>ѳ<EFBFBD> n <20><>
for (int i = 0; i < n; i++) {
count += factorialRecur(n - 1);
}
return count;
}
/* Driver Code */
int main() {
// <20><><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD> n <20><><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>¸<EFBFBD><C2B8>ָ<EFBFBD><D6B8>ӶȵIJ<C8B5><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E4BBAF><EFBFBD><EFBFBD>
int n = 8;
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD>С n = " << n << endl;
int count = constant(n);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׵IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << count << endl;
count = linear(n);
cout << "<EFBFBD><EFBFBD><EFBFBD>Խ׵IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << count << endl;
vector<int> arr(n);
count = arrayTraversal(arr);
cout << "<EFBFBD><EFBFBD><EFBFBD>Խף<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << count << endl;
count = quadratic(n);
cout << "ƽ<EFBFBD><EFBFBD><EFBFBD>׵IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << count << endl;
vector<int> nums(n);
for (int i = 0; i < n; i++)
nums[i] = n - i; // [n,n-1,...,2,1]
count = bubbleSort(nums);
cout << "ƽ<EFBFBD><EFBFBD><EFBFBD>ף<EFBFBD>ð<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򣩵IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << count << endl;
count = exponential(n);
cout << "ָ<EFBFBD><EFBFBD><EFBFBD>ף<EFBFBD>ѭ<EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << count << endl;
count = expRecur(n);
cout << "ָ<EFBFBD><EFBFBD><EFBFBD>ף<EFBFBD><EFBFBD>ݹ<EFBFBD>ʵ<EFBFBD>֣<EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << count << endl;
count = logarithmic((float)n);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ף<EFBFBD>ѭ<EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << count << endl;
count = logRecur((float)n);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ף<EFBFBD><EFBFBD>ݹ<EFBFBD>ʵ<EFBFBD>֣<EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << count << endl;
count = linearLogRecur((float)n);
cout << "<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ף<EFBFBD><EFBFBD>ݹ<EFBFBD>ʵ<EFBFBD>֣<EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << count << endl;
count = factorialRecur(n);
cout << "<EFBFBD>׳˽ף<EFBFBD><EFBFBD>ݹ<EFBFBD>ʵ<EFBFBD>֣<EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << count << endl;
return 0;
}