-
Notifications
You must be signed in to change notification settings - Fork 1
/
runningTIme.cpp
141 lines (114 loc) · 2.95 KB
/
runningTIme.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
void generateRandomValue(int *arr, int size, int min, int max)
{
for (int i = 0; i < size; ++i) {
arr[i] = rand() % (max - min + 1) + min;
}
}
void BubbleSort(int *arr, int size) {
for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size - i - 1; ++j) {
if (arr[j] > arr[j+1]) {
swap(arr[j], arr[j+1]);
}
}
}
}
void SelectionSort(int *arr, int size) {
for (int i = 0; i < size; ++i) {
int minIndex = i;
for (int j = i+1; j < size; ++j) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
swap(arr[i], arr[minIndex]);
}
}
void Merge(int *arr, int aLeft, int aRight, int bLeft, int bRight) {
int size = bRight - aLeft + 1;
int tmp [size];
int tIndex = 0;
int aIndex = aLeft;
int bIndex = bLeft;
while (aIndex <= aRight && bIndex <= bRight) {
if (arr[aIndex] <= arr[bIndex]) {
tmp[tIndex] = arr[aIndex];
++aIndex;
}
else {
tmp[tIndex] = arr[bIndex];
++bIndex;
}
++tIndex;
}
while (aIndex <= aRight) {
tmp[tIndex] = arr[aIndex];
++tIndex;
++aIndex;
}
while (bIndex <= bRight) {
tmp[tIndex] = arr[bIndex];
++tIndex;
++bIndex;
}
for (int i = 0; i < size; ++i) {
arr[aLeft+i] = tmp[i];
}
}
void MergeSort(int *arr, int left, int right) {
if (left == right) return;
int mid = (left + right) / 2;
MergeSort(arr, left, mid);
MergeSort(arr, mid+1, right);
Merge(arr, left, mid, mid+1, right);
}
int partition(int *arr, int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}
void QuickSort(int *arr, int low, int high) {
if (low < high) {
int pivot = partition(arr, low, high);
QuickSort(arr, low, pivot - 1);
QuickSort(arr, pivot + 1, high);
}
}
long long fibonacciDynamicProgramming(int n) {
static int dp[105];
dp[0] = dp[1] = 1;
if (dp[n] == 0) {
dp[n] = fibonacciDynamicProgramming(n-1) + fibonacciDynamicProgramming(n-2);
}
return dp[n];
}
long long fibonacciRecursive(long long val) {
if (val <=1) return val;
return fibonacciRecursive(val-1) + fibonacciRecursive(val-2);
}
int main()
{
srand(time(0));
clock_t start, end;
int size;
cin >> size;
start = clock();
fibonacciDynamicProgramming(size);
end = clock();
double time_taken = double(end - start)/CLOCKS_PER_SEC;
cout << "Time taken by program is : " << fixed
<< time_taken*1000 << setprecision(15);
cout << " milisecond " << endl;
return 0;
}