时间复杂度分析
2018-03-13 16:28
148 查看
#include<iostream>
#include<string>
#include<cassert>
using namespace std;
//O(1)
void swapTwoInts(int &a, int &b) {
int temp = a;
a = b;
b = temp;
return;
}
//O(n)
int sum(int n) {
assert(n >= 0);
int ret = 0;
for (int i = 0; i <= n; i++)
ret += i;
return ret;
}
void reverse(string &s) {
int n = s.size();
for (int i = 0; i < n / 2; i++)
swap(s[i], s[n - 1 - i]);
return;
}
//O(n^2) Time Complexity
void selectionSort(int arr[], int n) {
for (int i = 0; i < n; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex])
minIndex = j;
}
swap(arr[i], arr[minIndex]);
}
}
//O(n) Time Complexity
void printInfomation(int n) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= 30; j++)
cout << "Class " << i << " - " << "No. " << j << endl;
return;
}
//O(logn) Time Complexity
int binarySearch(int arr[], int n, int target) {
int l = 0, r = n - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] > target) r = mid - 1;
else l = mid + 1;
}
return -1;
}
string intTostring(int num) {
string s = "";
string sign = "+";
if (num < 0) {
num = -num;
sign = "-";
}
while (num) {
s += '0' + num % 10;
num /= 10;
}
if (s == "")
s = "0";
reverse(s);
if (sign == "-")
return sign + s;
else
return s;
}
//O(nlogn)
void hello(int n) {
for (int sz = 1; sz < n; sz += sz)
for (int i = 1; i < n; i++)
cout << "Hello, Algorithm" << endl;
}
//O(sqrt(n)) Time Complexity
bool isPrime(int num) {
for (int x = 2; x*x <= num; x++)
if (num % x == 0)
return false;
return true;
}
bool isPrime2(int num) {
if (num <= 1) return false;
if (num == 2) return true;
if (num % 2 == 0) return false;
for (int x = 3; x*x <= num; x += 2)
if (num%x == 0)
return false;
return true;
}
int main() {
cout << intTostring(123) << endl;
cout << intTostring(0) << endl;
cout << intTostring(-123) << endl;
cout << endl;
if (isPrime2(137)) cout << "137 is a prime" << endl;
else cout << "137 is not a prime" << endl;
if (isPrime(121)) cout << "121 is a prime" << endl;
else cout << "121 is not a prime" << endl;
return 0;
}
#include<string>
#include<cassert>
using namespace std;
//O(1)
void swapTwoInts(int &a, int &b) {
int temp = a;
a = b;
b = temp;
return;
}
//O(n)
int sum(int n) {
assert(n >= 0);
int ret = 0;
for (int i = 0; i <= n; i++)
ret += i;
return ret;
}
void reverse(string &s) {
int n = s.size();
for (int i = 0; i < n / 2; i++)
swap(s[i], s[n - 1 - i]);
return;
}
//O(n^2) Time Complexity
void selectionSort(int arr[], int n) {
for (int i = 0; i < n; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex])
minIndex = j;
}
swap(arr[i], arr[minIndex]);
}
}
//O(n) Time Complexity
void printInfomation(int n) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= 30; j++)
cout << "Class " << i << " - " << "No. " << j << endl;
return;
}
//O(logn) Time Complexity
int binarySearch(int arr[], int n, int target) {
int l = 0, r = n - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] > target) r = mid - 1;
else l = mid + 1;
}
return -1;
}
string intTostring(int num) {
string s = "";
string sign = "+";
if (num < 0) {
num = -num;
sign = "-";
}
while (num) {
s += '0' + num % 10;
num /= 10;
}
if (s == "")
s = "0";
reverse(s);
if (sign == "-")
return sign + s;
else
return s;
}
//O(nlogn)
void hello(int n) {
for (int sz = 1; sz < n; sz += sz)
for (int i = 1; i < n; i++)
cout << "Hello, Algorithm" << endl;
}
//O(sqrt(n)) Time Complexity
bool isPrime(int num) {
for (int x = 2; x*x <= num; x++)
if (num % x == 0)
return false;
return true;
}
bool isPrime2(int num) {
if (num <= 1) return false;
if (num == 2) return true;
if (num % 2 == 0) return false;
for (int x = 3; x*x <= num; x += 2)
if (num%x == 0)
return false;
return true;
}
int main() {
cout << intTostring(123) << endl;
cout << intTostring(0) << endl;
cout << intTostring(-123) << endl;
cout << endl;
if (isPrime2(137)) cout << "137 is a prime" << endl;
else cout << "137 is not a prime" << endl;
if (isPrime(121)) cout << "121 is a prime" << endl;
else cout << "121 is not a prime" << endl;
return 0;
}
相关文章推荐
- 一个台阶总共有n级,如果一次可以跳1级,也可以跳2级。 求总共有多少总跳法,并分析算法的时间复杂度。
- 合并排序算法时间复杂度分析
- c语言实现线性表的建立,初始化,插入,删除,查找,遍历以及时间复杂度分析
- 递归函数时间复杂度分析
- 递归算法的时间复杂度分析
- ACM算法-时间复杂度分析(2.最坏,平均,最好时间复杂度分析)
- 给定整型数组,其中每个元素表示木板的高度,木板的宽度都相同,求这些木板拼出的最大矩形的面积。并分析时间复杂度。
- 归并排序及其时间复杂度分析
- 递归算法的时间复杂度分析
- 常用排序算法稳定性、时间复杂度分析
- 递归算法的时间复杂度分析
- 七种排序方法(稳定性、空间复杂度、时间复杂度)分析总结
- 递归函数时间复杂度分析
- 时间复杂度和空间复杂度的分析
- 几种堆的时间复杂度分析
- 直接插入排序算法c语言实现。 己它的时间复杂度分析。
- 转载:快排时间复杂度分析
- 从一道求最大子列和问题分析时间复杂度
- 一个文本文件,大约有一万行,每行一个词,要求统计出其中最频繁出现的前10个词,请给出思想,给出时间复杂度分析
- 递归算法的时间复杂度分析