Skip to content

Commit 120fe06

Browse files
committed
files renamed to standard - without spaces and made CPPLINT compatible
1 parent 4e8a2f7 commit 120fe06

14 files changed

+216
-129
lines changed

others/GCD_of_n_numbers.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
//This program aims at calculating the GCD of n numbers by division method
1+
// This program aims at calculating the GCD of n numbers by division method
22
#include <iostream>
3-
using namepsace std;
4-
int main()
5-
{
6-
cout << "Enter value of n:" << endl;
7-
cin >> n;
8-
int a[n];
9-
int i, j, gcd;
10-
cout << "Enter the n numbers:" << endl;
11-
for (i = 0; i < n; i++)
12-
cin >> a[i];
13-
j = 1; //to access all elements of the array starting from 1
14-
gcd = a[0];
15-
while (j < n)
16-
{
17-
if (a[j] % gcd == 0) //value of gcd is as needed so far
18-
j++; //so we check for next element
19-
else
20-
gcd = a[j] % gcd; //calculating GCD by division method
21-
}
22-
cout << "GCD of entered n numbers:" << gcd;
3+
4+
int main() {
5+
int n;
6+
std::cout << "Enter value of n:" << std::endl;
7+
std::cin >> n;
8+
int a[n];
9+
int i, j, gcd;
10+
std::cout << "Enter the n numbers:" << std::endl;
11+
for (i = 0; i < n; i++) std::cin >> a[i];
12+
j = 1; // to access all elements of the array starting from 1
13+
gcd = a[0];
14+
while (j < n) {
15+
if (a[j] % gcd == 0) // value of gcd is as needed so far
16+
j++; // so we check for next element
17+
else
18+
gcd = a[j] % gcd; // calculating GCD by division method
19+
}
20+
std::cout << "GCD of entered n numbers:" << gcd;
21+
return 0;
2322
}
File renamed without changes.

others/fibonacci.cpp

Lines changed: 0 additions & 42 deletions
This file was deleted.

others/fibonacci_fast.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// An efficient way to calculate nth fibonacci number faster and simpler than
2+
// O(nlogn) method of matrix exponentiation This works by using both recursion
3+
// and dynamic programming. as 93rd fibonacci exceeds 19 digits, which cannot be
4+
// stored in a single long long variable, we can only use it till 92nd fibonacci
5+
// we can use it for 10000th fibonacci etc, if we implement bigintegers.
6+
// This algorithm works with the fact that nth fibonacci can easily found if we
7+
// have already found n/2th or (n+1)/2th fibonacci It is a property of fibonacci
8+
// similar to matrix exponentiation.
9+
10+
#include <cstdio>
11+
#include <iostream>
12+
using namespace std;
13+
14+
const long long MAX = 93;
15+
16+
long long f[MAX] = {0};
17+
18+
long long fib(long long n) {
19+
if (n == 0) return 0;
20+
if (n == 1 || n == 2) return (f[n] = 1);
21+
22+
if (f[n]) return f[n];
23+
24+
long long k = (n % 2 != 0) ? (n + 1) / 2 : n / 2;
25+
26+
f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1))
27+
: (2 * fib(k - 1) + fib(k)) * fib(k);
28+
return f[n];
29+
}
30+
31+
int main() {
32+
// Main Function
33+
for (long long i = 1; i < 93; i++) {
34+
cout << i << " th fibonacci number is " << fib(i) << "\n";
35+
}
36+
return 0;
37+
}

others/matrix_exponentiation.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ The first element of this matrix is the required result.
1919
*/
2020

2121
#include <iostream>
22+
#include <vector>
23+
2224
using std::cin;
2325
using std::cout;
2426
using std::vector;
@@ -46,8 +48,7 @@ vector<vector<ll>> multiply(vector<vector<ll>> A, vector<vector<ll>> B) {
4648

4749
// computing power of a matrix
4850
vector<vector<ll>> power(vector<vector<ll>> A, ll p) {
49-
if (p == 1)
50-
return A;
51+
if (p == 1) return A;
5152
if (p % 2 == 1) {
5253
return multiply(A, power(A, p - 1));
5354
} else {
@@ -58,14 +59,11 @@ vector<vector<ll>> power(vector<vector<ll>> A, ll p) {
5859

5960
// main function
6061
ll ans(ll n) {
61-
if (n == 0)
62-
return 0;
63-
if (n <= k)
64-
return b[n - 1];
62+
if (n == 0) return 0;
63+
if (n <= k) return b[n - 1];
6564
// F1
6665
vector<ll> F1(k + 1);
67-
for (ll i = 1; i <= k; i++)
68-
F1[i] = b[i - 1];
66+
for (ll i = 1; i <= k; i++) F1[i] = b[i - 1];
6967

7068
// Transpose matrix
7169
vector<vector<ll>> T(k + 1, vector<ll>(k + 1));
File renamed without changes.

others/pascal_triangle.cpp

Lines changed: 48 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,53 @@
1-
#include<iostream>
2-
3-
using namespace std;
4-
5-
void show_pascal(int **arr, int n)
6-
{
7-
//pint Pascal's Triangle
8-
for (int i = 0; i < n; ++i)
9-
{
10-
for (int j = 0; j < n + i; ++j)
11-
{
12-
if (arr[i][j] == 0)
13-
cout << " ";
14-
else
15-
cout << arr[i][j];
16-
}
17-
cout << endl;
18-
}
1+
#include <cstring>
2+
#include <iostream>
3+
4+
void show_pascal(int **arr, int n) {
5+
// pint Pascal's Triangle
6+
for (int i = 0; i < n; ++i) {
7+
for (int j = 0; j < n + i; ++j) {
8+
if (arr[i][j] == 0)
9+
std::cout << " ";
10+
else
11+
std::cout << arr[i][j];
12+
}
13+
std::cout << std::endl;
14+
}
1915
}
2016

21-
int **pascal_triangle(int **arr, int n)
22-
{
23-
for (int i = 0; i < n; ++i)
24-
{
25-
for (int j = n - i - 1; j < n + i; ++j)
26-
{
27-
if (j == n - i - 1 || j == n + i - 1)
28-
arr[i][j] = 1; //The edge of the Pascal triangle goes in 1
29-
else
30-
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j + 1];
31-
}
32-
}
33-
34-
return arr;
17+
int **pascal_triangle(int **arr, int n) {
18+
for (int i = 0; i < n; ++i) {
19+
for (int j = n - i - 1; j < n + i; ++j) {
20+
if (j == n - i - 1 || j == n + i - 1)
21+
arr[i][j] = 1; // The edge of the Pascal triangle goes in 1
22+
else
23+
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j + 1];
24+
}
25+
}
26+
27+
return arr;
3528
}
3629

37-
int main()
38-
{
39-
int n = 0;
40-
41-
cout << "Set Pascal's Triangle Height" << endl;
42-
cin >> n;
43-
44-
//memory allocation (Assign two-dimensional array to store Pascal triangle)
45-
int **arr = new int*[n];
46-
for (int i = 0; i < n; ++i)
47-
{
48-
arr[i] = new int[2 * n - 1];
49-
memset(arr[i], 0, sizeof(int)*(2 * n - 1));
50-
}
51-
52-
pascal_triangle(arr, n);
53-
show_pascal(arr, n);
54-
55-
//deallocation
56-
for (int i = 0; i < n; ++i)
57-
{
58-
delete[] arr[i];
59-
}
60-
delete[] arr;
61-
62-
return 0;
30+
int main() {
31+
int n = 0;
32+
33+
std::cout << "Set Pascal's Triangle Height" << std::endl;
34+
std::cin >> n;
35+
36+
// memory allocation (Assign two-dimensional array to store Pascal triangle)
37+
int **arr = new int *[n];
38+
for (int i = 0; i < n; ++i) {
39+
arr[i] = new int[2 * n - 1];
40+
memset(arr[i], 0, sizeof(int) * (2 * n - 1));
41+
}
42+
43+
pascal_triangle(arr, n);
44+
show_pascal(arr, n);
45+
46+
// deallocation
47+
for (int i = 0; i < n; ++i) {
48+
delete[] arr[i];
49+
}
50+
delete[] arr;
51+
52+
return 0;
6353
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

sorting/shell_sort2.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <array>
2+
#include <cstdlib>
3+
#include <ctime>
4+
#include <iostream>
5+
6+
// for std::swap
7+
#include <utility>
8+
9+
template <class T> void show_data(T *arr, size_t LEN) {
10+
size_t i;
11+
12+
for (i = 0; i < LEN; i++)
13+
std::cout << arr[i] << ", ";
14+
std::cout << std::endl;
15+
}
16+
17+
template <class T, size_t N> void show_data(T (&arr)[N]) { show_data(arr, N); }
18+
19+
/**
20+
* Optimized algorithm - takes half the time by utilizing
21+
* Mar
22+
**/
23+
template <class T> void shell_sort(T *arr, size_t LEN) {
24+
const unsigned int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
25+
const unsigned int gap_len = 8;
26+
size_t i, j, g;
27+
28+
for (g = 0; g < gap_len; g++) {
29+
unsigned int gap = gaps[g];
30+
for (i = gap; i < LEN; i++) {
31+
T tmp = arr[i];
32+
33+
for (j = i; j >= gap && (arr[j - gap] - tmp) > 0; j -= gap)
34+
arr[j] = arr[j - gap];
35+
36+
arr[j] = tmp;
37+
}
38+
}
39+
}
40+
41+
template <class T, size_t N> void shell_sort(T (&arr)[N]) {
42+
shell_sort(arr, N);
43+
}
44+
45+
/**
46+
* function to compare sorting using cstdlib's qsort
47+
**/
48+
int compare(const void *a, const void *b) {
49+
int arg1 = *static_cast<const int *>(a);
50+
int arg2 = *static_cast<const int *>(b);
51+
52+
if (arg1 < arg2)
53+
return -1;
54+
if (arg1 > arg2)
55+
return 1;
56+
return 0;
57+
58+
// return (arg1 > arg2) - (arg1 < arg2); // possible shortcut
59+
// return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present)
60+
}
61+
62+
int main(int argc, char *argv[]) {
63+
int i, NUM_DATA;
64+
65+
if (argc == 2)
66+
NUM_DATA = atoi(argv[1]);
67+
else
68+
NUM_DATA = 200;
69+
70+
// int array = new int[NUM_DATA];
71+
int *data = new int[NUM_DATA];
72+
int *data2 = new int[NUM_DATA];
73+
// int array2 = new int[NUM_DATA];
74+
int range = 1800;
75+
76+
std::srand(time(NULL));
77+
for (i = 0; i < NUM_DATA; i++)
78+
data[i] = data2[i] = (std::rand() % range) - (range >> 1);
79+
80+
std::cout << "Unsorted original data: " << std::endl;
81+
show_data(data, NUM_DATA);
82+
std::clock_t start = std::clock();
83+
shell_sort(data, NUM_DATA);
84+
std::clock_t end = std::clock();
85+
86+
std::cout << std::endl
87+
<< "Data Sorted using custom implementation: " << std::endl;
88+
show_data(data, NUM_DATA);
89+
90+
double elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC;
91+
std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl;
92+
93+
start = std::clock();
94+
qsort(data2, NUM_DATA, sizeof(data2[0]), compare);
95+
end = std::clock();
96+
std::cout << "Data Sorted using cstdlib qsort: " << std::endl;
97+
show_data(data2, NUM_DATA);
98+
99+
elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC;
100+
std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl;
101+
102+
free(data);
103+
free(data2);
104+
return 0;
105+
}

0 commit comments

Comments
 (0)