二分查找
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);
}
-----log(n)
归并算法
-----logn层(从第0层开始),每层n次计算
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
均摊时间复杂度分析
#include <iostream>
#include <cassert>
#include <cmath>
#include <ctime>
#include "Myvector.h"
using namespace std;
int main(){
for( int i = 10; i <= 26 ; i++){
int n = pow(2.0,(double)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;
}
//
// Created by liuyubobobo on 12/01/2017.
//
#ifndef INC_06_AMORTIZED_TIME_MYVECTOR_H
#define INC_06_AMORTIZED_TIME_MYVECTOR_H
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 --;
return data[size];
}
};
#endif //INC_06_AMORTIZED_TIME_MYVECTOR_H
-----resize时copy到新数组的耗费为n,但是均摊下去,每一个数组元素耗费为1+1=2
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
元素增加到一定大小应该resize更大的空间,那么当删除一个元素的时候是否应该减小空间呢
T pop_back(){
assert( size > 0);
T ret = data[size - 1];
size-- ;
if( size == capacity/2)
resize( capacity/2);
return ret;
}
注意:应当在resize之前将需要pop出来的这个元素保存起来,否则resize之后的数组中将不再包含该元素
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
删除元素时应该等一等,当元素个数为1/4时再resize为1/2,为增加元素留出余地
#include <iostream>
#include <cassert>
#include <cmath>
#include <ctime>
#include "Myvector.h"
using namespace std;
int main(){
for( int i = 10; i <= 26 ; i++){
int n = pow(2.0,(double)i);
clock_t startTime = clock();
MyVector<int> vec;
for(int i = 0; i < n; i++){
vec.push_back(i);
}
for(int i = 0; i < n; i++){
vec.pop_back();
}
clock_t endTime = clock();
cout << 2*n <<" operations: \t" ;
cout << double(endTime - startTime)/CLOCKS_PER_SEC << " s"<< endl;
}
return 0;
}
using namespace std;
template <typename T>
class MyVector{
private:
T* data;
int size;
int capacity;
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;
}
void push_back(T e){
if( size == capacity)
resize( 2* capacity);
data[size++] = e ;
}
T pop_back(){
assert( size > 0);
T ret = data[size - 1];
size-- ;
if( size == capacity/4)
resize( capacity/2);
return ret;
}
};