算法A:O(n) 所需執行指令數:10000n
算法B:O(n^2) 所需執行指令數:10n^2ios
當n大於某個臨界點,a必定會超過b。這是量級上的差距。c++
複雜度很高的算法可能有前面的優點,在數據量很小的時候有意義。算法
不一樣時間複雜度隨着數據規模的增大數組
O( nlogn + n ) = O( nlogn )bash
O( nlogn + n^2 ) = O( n^2 )微信
有一個字符串數組,將數組中的每個字符串按照字母序排序;以後再將整個字符串數組按照字典序排序。整個操做的時間複雜度?dom
假設最長的字符串長度爲s;數組中有n個字符串
對每一個字符串排序:O(slogs)
將數組中的每個字符串按照字母序排序:O(n*slog(s))函數
將整個字符串數組按照字典序排序:O(s*nlog(n))測試
O(nslog(s)) + O(snlog(n)) = O( nslogs + snlogn )= O( ns(logs+logn) )優化
字符串數組進行字典序排序。比較nlogn次,每次比較須要O(s)時間複雜度。
嚴謹算法最好最差平均。咱們常常關注的是大多數。
極端狀況內心有數就好了。
對 10^5 的數據進行選擇排序,結果計算機假死?
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
int main() {
// 數據規模每次增大10倍進行測試
// 有興趣的同窗也能夠試驗一下數據規模每次增大2倍哦:)
for( int x = 1 ; x <= 9 ; x ++ ){
int n = pow(10, x);
clock_t startTime = clock();
long long sum = 0;
for( int i = 0 ; i < n ; i ++ )
sum += i;
clock_t endTime = clock();
cout << "sum = " << sum << endl;
cout << "10^" << x << " : "
<< double(endTime - startTime)/CLOCKS_PER_SEC
<< " s" << endl << endl;
}
return 0;
}
複製代碼
運行結果
sum = 45
10^1 : 2e-06 s
sum = 4950
10^2 : 1e-06 s
sum = 499500
10^3 : 4e-06 s
sum = 49995000
10^4 : 2.9e-05 s
sum = 4999950000
10^5 : 0.000305 s
sum = 499999500000
10^6 : 0.003049 s
sum = 49999995000000
10^7 : 0.029234 s
sum = 4999999950000000
10^8 : 0.308056 s
sum = 499999999500000000
10^9 : 2.98528 s
複製代碼
由於咱們剛纔的操做很簡單,就是簡單的加法。因此正常還須要低估一點,再除以10
O(1)
沒有數據規模的變化
// O(1)
void swapTwoInts( int &a , int &b ){
int temp = a;
a = b;
b = temp;
return;
}
複製代碼
O(n)
循環操做次數爲c.n。c是個常數不必定爲大於1的數
// O(n) Time Complexity
int sum( int n ){
int ret = 0;
for( int i = 0 ; i <= n ; i ++ )
ret += i;
return ret;
}
複製代碼
O(n)
循環次數爲1/2 * n次
字符串翻轉。abc-cba.第一個和倒數第一個。第2個和倒數第二個
掃描一半就交換完了:1/2*n次swap操做:O(n)
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)
選擇排序法。O(n^2)
雙重循環: 第一重到n。第二重到n。都是+1.
所執行的指令數和n^2成比例。
i = 0;j執行了n-1次 等差數列求和
(n-1) + (n-2) + (n-3) + … + 0
= (0+n-1)*n/2
= (1/2)n*(n-1)
= 1/2*n^2 - 1/2*n
= O(n^2)
// 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] );
}
}
複製代碼
30n次基本操做:O(n)
由於第二層循環是固定的不受n影響的。
// O(n) Time Complexity
void printInformation(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)
對有序數組找到中間元素來判斷元素和中間元素的關係。
若是沒有查找到,均可以扔掉一半的元素。
二分查找法
// 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;
}
複製代碼
O(logn)
n通過幾回「除以10」操做後,等於0?
log10n = O(logn)
while循環中每次除以10,直到0結束。
reverse(s)複雜度:1/2 n次的交換操做。s字符串有多少位,與n一致。
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)
第二重循環就是n
第一重size+=size就是乘以2.log2n
// 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))
x從n走一直走到根號n結束
// 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;
}
複製代碼
咱們自覺得寫出了一個O(nlogn)的算法,但實際是O(n^2)的算法?
若是要想在1s以內解決問題:
前面的常數差距有可能很大。
實驗,觀察趨勢:
每次將數據規模提升兩倍,看時間的變化
四個不一樣複雜度的算法。
namespace MyAlgorithmTester{
// O(logN)
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;
}
// O(N)
int findMax( int arr[], int n ){
assert( n > 0 );
int res = arr[0];
for( int i = 1 ; i < n ; i ++ )
if( arr[i] > res )
res = arr[i];
return res;
}
// O(NlogN) 自底向上
void __merge(int arr[], int l, int mid, int r, int aux[]){
for(int i = l ; i <= r ; i ++)
aux[i] = arr[i];
int i = l, j = mid+1;
for( int k = l ; k <= r; k ++ ){
if( i > mid ) { arr[k] = aux[j]; j ++;}
else if( j > r ){ arr[k] = aux[i]; i ++;}
else if( aux[i] < aux[j] ){ arr[k] = aux[i]; i ++;}
else { arr[k] = aux[j]; j ++;}
}
}
void mergeSort( int arr[], int n ){
int *aux = new int[n];
for( int i = 0 ; i < n ; i ++ )
aux[i] = arr[i];
for( int sz = 1; sz < n ; sz += sz )
for( int i = 0 ; i < n ; i += sz+sz )
__merge(arr, i, i+sz-1, min(i+sz+sz-1,n-1), aux );
delete[] aux;
return;
}
// O(N^2) 選擇排序
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] );
}
return;
}
}
複製代碼
生成測試用例的代碼:
namespace MyUtil {
int *generateRandomArray(int n, int rangeL, int rangeR) {
assert( n > 0 && rangeL <= rangeR );
int *arr = new int[n];
srand(time(NULL));
for (int i = 0; i < n; i++)
arr[i] = rand() % (rangeR - rangeL + 1) + rangeL;
return arr;
}
int *generateOrderedArray(int n) {
assert( n > 0 );
int *arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = i;
return arr;
}
}
複製代碼
int main() {
// 數據規模倍乘測試findMax
// O(n)
cout<<"Test for findMax:"<<endl;
for( int i = 10 ; i <= 26 ; i ++ ){
int n = pow(2,i);
int *arr = MyUtil::generateRandomArray(n, 0, 100000000);
clock_t startTime = clock();
MyAlgorithmTester::findMax(arr, n);
clock_t endTime = clock();
cout<<"data size 2^"<<i<<" = "<<n<<"\t";
cout<<"Time cost: "<<double(endTime - startTime)/CLOCKS_PER_SEC<<endl;
delete[] arr;
}
return 0;
}
複製代碼
運行結果:
Test for findMax:
data size 2^10 = 1024 Time cost: 5e-06 s
data size 2^11 = 2048 Time cost: 7e-06 s
data size 2^12 = 4096 Time cost: 1.2e-05 s
data size 2^13 = 8192 Time cost: 2.5e-05 s
data size 2^14 = 16384 Time cost: 4.7e-05 s
data size 2^15 = 32768 Time cost: 9.2e-05 s
data size 2^16 = 65536 Time cost: 0.000169 s
data size 2^17 = 131072 Time cost: 0.000431 s
data size 2^18 = 262144 Time cost: 0.000737 s
data size 2^19 = 524288 Time cost: 0.001325 s
data size 2^20 = 1048576 Time cost: 0.002489 s
data size 2^21 = 2097152 Time cost: 0.005739 s
data size 2^22 = 4194304 Time cost: 0.011373 s
data size 2^23 = 8388608 Time cost: 0.019566 s
data size 2^24 = 16777216 Time cost: 0.040289 s
data size 2^25 = 33554432 Time cost: 0.095169 s
data size 2^26 = 67108864 Time cost: 0.201682 s
data size 2^27 = 134217728 Time cost: 0.330673 s
data size 2^28 = 268435456 Time cost: 0.750136 s
複製代碼
n增長了兩倍。時間也大體增長兩倍,因此該算法爲O(n)級別的。
int main() {
// 數據規模倍乘測試selectionSort
// O(n^2)
cout<<"Test for selectionSort:"<<endl;
for( int i = 10 ; i <= 15 ; i ++ ){
int n = pow(2,i);
int *arr = MyUtil::generateRandomArray(n, 0, 100000000);
clock_t startTime = clock();
MyAlgorithmTester::selectionSort(arr,n);
clock_t endTime = clock();
cout<<"data size 2^"<<i<<" = "<<n<<"\t";
cout<<"Time cost: "<<double(endTime - startTime)/CLOCKS_PER_SEC<<endl;
delete[] arr;
}
return 0;
}
複製代碼
運行結果:大約4倍
Test for Selection Sort:
data size 2^10 = 1024 Time cost: 0.001581 s
data size 2^11 = 2048 Time cost: 0.006221 s
data size 2^12 = 4096 Time cost: 0.021913 s
data size 2^13 = 8192 Time cost: 0.081103 s
data size 2^14 = 16384 Time cost: 0.323263 s
data size 2^15 = 32768 Time cost: 1.32474 s
data size 2^16 = 65536 Time cost: 5.19642 s
複製代碼
數據量n增長了2倍。時間增長了4倍。
int main() {
// 數據規模倍乘測試binarySearch
// O(logn)
cout<<"Test for binarySearch:"<<endl;
for( int i = 10 ; i <= 28 ; i ++ ){
int n = pow(2,i);
int *arr = MyUtil::generateOrderedArray(n);
clock_t startTime = clock();
MyAlgorithmTester::binarySearch(arr,n,0);
clock_t endTime = clock();
cout<<"data size 2^"<<i<<" = "<<n<<"\t";
cout<<"Time cost: "<<double(endTime - startTime)/CLOCKS_PER_SEC<<endl;
delete[] arr;
}
return 0;
}
複製代碼
複雜度試驗
log2N / logN
= (log2 + logN)/logN
= 1 + log2/logN
複製代碼
當數據規模變大兩倍。運行效率增長1.幾倍。
運行結果:
Test for Binary Search:
data size 2^10 = 1024 Time cost: 1e-06 s
data size 2^11 = 2048 Time cost: 0 s
data size 2^12 = 4096 Time cost: 0 s
data size 2^13 = 8192 Time cost: 2e-06 s
data size 2^14 = 16384 Time cost: 1e-06 s
data size 2^15 = 32768 Time cost: 1e-06 s
data size 2^16 = 65536 Time cost: 1e-06 s
data size 2^17 = 131072 Time cost: 2e-06 s
data size 2^18 = 262144 Time cost: 3e-06 s
data size 2^19 = 524288 Time cost: 1e-06 s
data size 2^20 = 1048576 Time cost: 4e-06 s
data size 2^21 = 2097152 Time cost: 3e-06 s
data size 2^22 = 4194304 Time cost: 3e-06 s
data size 2^23 = 8388608 Time cost: 4e-06 s
data size 2^24 = 16777216 Time cost: 4e-06 s
data size 2^25 = 33554432 Time cost: 1.2e-05 s
data size 2^26 = 67108864 Time cost: 9e-06 s
data size 2^27 = 134217728 Time cost: 1.1e-05 s
data size 2^28 = 268435456 Time cost: 2.4e-05 s
複製代碼
運行結果,變化小
順序查找轉換爲二分查找,大大提升效率
和O(N)差很少
int main() {
// 數據規模倍乘測試mergeSort
// O(nlogn)
cout<<"Test for mergeSort:"<<endl;
for( int i = 10 ; i <= 24 ; i ++ ){
int n = pow(2,i);
int *arr = MyUtil::generateRandomArray(n,0,1<<30);
clock_t startTime = clock();
MyAlgorithmTester::mergeSort(arr,n);
clock_t endTime = clock();
cout<<"data size 2^"<<i<<" = "<<n<<"\t";
cout<<"Time cost: "<<double(endTime - startTime)/CLOCKS_PER_SEC<<endl;
delete[] arr;
}
return 0;
}
複製代碼
運行結果:
Test for Merge Sort:
data size 2^10 = 1024 Time cost: 0.000143 s
data size 2^11 = 2048 Time cost: 0.000325 s
data size 2^12 = 4096 Time cost: 0.000977 s
data size 2^13 = 8192 Time cost: 0.001918 s
data size 2^14 = 16384 Time cost: 0.003678 s
data size 2^15 = 32768 Time cost: 0.007635 s
data size 2^16 = 65536 Time cost: 0.015768 s
data size 2^17 = 131072 Time cost: 0.034462 s
data size 2^18 = 262144 Time cost: 0.069586 s
data size 2^19 = 524288 Time cost: 0.136214 s
data size 2^20 = 1048576 Time cost: 0.294626 s
data size 2^21 = 2097152 Time cost: 0.619943 s
data size 2^22 = 4194304 Time cost: 1.37317 s
data size 2^23 = 8388608 Time cost: 2.73054 s
data size 2^24 = 16777216 Time cost: 5.60827 s
複製代碼
大約兩倍
左半邊或者右半邊。不管選那邊都只進行一次
每次減半,遞歸調用的深度爲logn,處理問題的複雜度爲O(1)
// binarySearch
int binarySearch(int arr[], int l, int r, int target){
if( l > r )
return -1;
int mid = l + (r-l)/2;
if( arr[mid] == target )
return mid;
else if( arr[mid] > target )
return binarySearch(arr, l, mid-1, target);
else
return binarySearch(arr, mid+1, r, target);
}
複製代碼
若是遞歸函數中,只進行一次遞歸調用,
遞歸深度爲depth;
在每一個遞歸函數中,時間複雜度爲T;
則整體的時間複雜度爲O( T * depth )
遞歸深度:n
時間複雜度:O(n)
// sum
int sum( int n ){
assert( n >= 0 );
if( n == 0 )
return 0;
return n + sum(n-1);
}
複製代碼
// pow2
double pow( double x, int n ){
assert( n >= 0 );
if( n == 0 )
return 1.0;
double t = pow(x, n/2);
//奇數
if( n%2 )
return x*t*t;
return t*t;
}
複製代碼
遞歸深度:logn
時間複雜度:O(logn)
遞歸樹的深度是N
2^0 + 2^1 + 2^2 + 2^3 + … + 2^n
= 2n+1 - 1
= O(2^n)
指數級的算法:很是慢。n在20左右。30就很是慢 剪枝操做:動態規劃。人工智能:搜索樹
// f
int f(int n){
assert( n >= 0 );
if( n == 0 )
return 1;
return f(n-1) + f(n-1);
}
複製代碼
樹的深度是logN 當n等於8時,層數爲3層。每一層處理的數據規模愈來愈小
一個分紅logn層。每一層相加的整體規模仍是n
// mergeSort
void mergeSort(int arr[], int l, int r){
if( l >= r )
return;
int mid = (l+r)/2;
mergeSort(arr, l, mid);
mergeSort(arr, mid+1, r);
merge(arr, l, mid, r);
}
複製代碼
一個算法複雜度相對較高,可是它是爲了方便其餘的操做。
比較高的會均攤到總體。
template <typename T>
class MyVector{
private:
T* data;
int size; // 存儲數組中的元素個數
int capacity; // 存儲數組中能夠容納的最大的元素個數
// O(n):一重循環。
void resize(int newCapacity){
assert( newCapacity >= size );
T *newData = new T[newCapacity];
for( int i = 0 ; i < size ; i ++ )
newData[i] = data[i];
delete[] data;
data = newData;
capacity = newCapacity;
}
public:
MyVector(){
data = new T[100];
size = 0;
capacity = 100;
}
~MyVector(){
delete[] data;
}
// Average: O(1)
void push_back(T e){
//動態數組
if( size == capacity )
resize( 2* capacity );
data[size++] = e;
}
// O(1)
T pop_back(){
assert( size > 0 );
size --;
//size是從0開始的。也就是0號索引size爲1.
//因此要拿到最後一個元素,就得size-1
return data[size];
}
};
複製代碼
均攤
第n+1次會花費O(n)可是會把這n分攤到前面n次操做。也就是變成了O(2)
仍是常數O(1)級的。
resize是有條件的,而不是每次都調用。
int main() {
for( int i = 10 ; i <= 26 ; i ++ ){
int n = pow(2,i);
clock_t startTime = clock();
MyVector<int> vec;
for( int i = 0 ; i < n ; i ++ ){
vec.push_back(i);
}
clock_t endTime = clock();
cout<<n<<" operations: \t";
cout<<double(endTime - startTime)/CLOCKS_PER_SEC<<" s"<<endl;
}
return 0;
}
複製代碼
1024 operations: 2.5e-05 s
2048 operations: 2.9e-05 s
4096 operations: 7.4e-05 s
8192 operations: 0.000154 s
16384 operations: 0.000265 s
32768 operations: 0.000391 s
65536 operations: 0.001008 s
131072 operations: 0.002006 s
262144 operations: 0.003863 s
524288 operations: 0.005842 s
1048576 operations: 0.014672 s
2097152 operations: 0.029367 s
4194304 operations: 0.06675 s
8388608 operations: 0.124446 s
16777216 operations: 0.240025 s
33554432 operations: 0.486061 s
67108864 operations: 0.960224 s
複製代碼
基本知足2倍關係
每次普通刪除時間複雜度都爲O(1)
只剩下n個。此次resize n 刪除這個元素爲1
重複這個過程,沒法均攤,複雜度爲O(n)
當元素個數爲數組容量的1/4時,resize.爲再添加元素留出餘地
template <typename T>
class MyVector{
private:
T* data;
int size; // 存儲數組中的元素個數
int capacity; // 存儲數組中能夠容納的最大的元素個數
// O(n)
void resize(int newCapacity){
assert( newCapacity >= size );
T *newData = new T[newCapacity];
for( int i = 0 ; i < size ; i ++ )
newData[i] = data[i];
delete[] data;
data = newData;
capacity = newCapacity;
}
public:
MyVector(){
data = new T[100];
size = 0;
capacity = 100;
}
~MyVector(){
delete[] data;
}
// Average: O(1)
void push_back(T e){
if( size == capacity )
resize( 2* capacity );
data[size++] = e;
}
// Average: O(1)
T pop_back(){
assert( size > 0 );
T ret = data[size-1];
size --;
if( size == capacity/4 )
resize( capacity/2 );
//resize以後會把data[size]元素抹掉
return ret;
}
};
複製代碼
運行結果
2048 operations: 4.3e-05 s
4096 operations: 6.3e-05 s
8192 operations: 0.000107 s
16384 operations: 0.000316 s
32768 operations: 0.000573 s
65536 operations: 0.001344 s
131072 operations: 0.001995 s
262144 operations: 0.004102 s
524288 operations: 0.008599 s
1048576 operations: 0.014714 s
2097152 operations: 0.027181 s
4194304 operations: 0.063136 s
8388608 operations: 0.126046 s
16777216 operations: 0.242574 s
33554432 operations: 0.456381 s
67108864 operations: 0.96618 s
134217728 operations: 1.76422 s
複製代碼
-------------------------華麗的分割線--------------------
看完的朋友能夠點個喜歡/關注,您的支持是對我最大的鼓勵。
想了解更多,歡迎關注個人微信公衆號:番茄技術小棧