mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-04 02:59:43 +08:00
files renamed to standard - without spaces and made CPPLINT compatible
This commit is contained in:
@@ -1,23 +1,22 @@
|
||||
//This program aims at calculating the GCD of n numbers by division method
|
||||
// This program aims at calculating the GCD of n numbers by division method
|
||||
#include <iostream>
|
||||
using namepsace std;
|
||||
int main()
|
||||
{
|
||||
cout << "Enter value of n:" << endl;
|
||||
cin >> n;
|
||||
int a[n];
|
||||
int i, j, gcd;
|
||||
cout << "Enter the n numbers:" << endl;
|
||||
for (i = 0; i < n; i++)
|
||||
cin >> a[i];
|
||||
j = 1; //to access all elements of the array starting from 1
|
||||
gcd = a[0];
|
||||
while (j < n)
|
||||
{
|
||||
if (a[j] % gcd == 0) //value of gcd is as needed so far
|
||||
j++; //so we check for next element
|
||||
else
|
||||
gcd = a[j] % gcd; //calculating GCD by division method
|
||||
}
|
||||
cout << "GCD of entered n numbers:" << gcd;
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
std::cout << "Enter value of n:" << std::endl;
|
||||
std::cin >> n;
|
||||
int a[n];
|
||||
int i, j, gcd;
|
||||
std::cout << "Enter the n numbers:" << std::endl;
|
||||
for (i = 0; i < n; i++) std::cin >> a[i];
|
||||
j = 1; // to access all elements of the array starting from 1
|
||||
gcd = a[0];
|
||||
while (j < n) {
|
||||
if (a[j] % gcd == 0) // value of gcd is as needed so far
|
||||
j++; // so we check for next element
|
||||
else
|
||||
gcd = a[j] % gcd; // calculating GCD by division method
|
||||
}
|
||||
std::cout << "GCD of entered n numbers:" << gcd;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
//An efficient way to calculate nth fibonacci number faster and simpler than O(nlogn) method of matrix exponentiation
|
||||
//This works by using both recursion and dynamic programming.
|
||||
//as 93rd fibonacci exceeds 19 digits, which cannot be stored in a single long long variable, we can only use it till 92nd fibonacci
|
||||
//we can use it for 10000th fibonacci etc, if we implement bigintegers.
|
||||
//This algorithm works with the fact that nth fibonacci can easily found if we have already found n/2th or (n+1)/2th fibonacci
|
||||
//It is a property of fibonacci similar to matrix exponentiation.
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
using namespace std;
|
||||
|
||||
const long long MAX = 93;
|
||||
|
||||
long long f[MAX] = {0};
|
||||
|
||||
long long fib(long long n)
|
||||
{
|
||||
|
||||
if (n == 0)
|
||||
return 0;
|
||||
if (n == 1 || n == 2)
|
||||
return (f[n] = 1);
|
||||
|
||||
if (f[n])
|
||||
return f[n];
|
||||
|
||||
long long k = (n % 2 != 0) ? (n + 1) / 2 : n / 2;
|
||||
|
||||
f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1))
|
||||
: (2 * fib(k - 1) + fib(k)) * fib(k);
|
||||
return f[n];
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
//Main Function
|
||||
for (long long i = 1; i < 93; i++)
|
||||
{
|
||||
cout << i << " th fibonacci number is " << fib(i) << "\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
37
others/fibonacci_fast.cpp
Normal file
37
others/fibonacci_fast.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
// An efficient way to calculate nth fibonacci number faster and simpler than
|
||||
// O(nlogn) method of matrix exponentiation This works by using both recursion
|
||||
// and dynamic programming. as 93rd fibonacci exceeds 19 digits, which cannot be
|
||||
// stored in a single long long variable, we can only use it till 92nd fibonacci
|
||||
// we can use it for 10000th fibonacci etc, if we implement bigintegers.
|
||||
// This algorithm works with the fact that nth fibonacci can easily found if we
|
||||
// have already found n/2th or (n+1)/2th fibonacci It is a property of fibonacci
|
||||
// similar to matrix exponentiation.
|
||||
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
const long long MAX = 93;
|
||||
|
||||
long long f[MAX] = {0};
|
||||
|
||||
long long fib(long long n) {
|
||||
if (n == 0) return 0;
|
||||
if (n == 1 || n == 2) return (f[n] = 1);
|
||||
|
||||
if (f[n]) return f[n];
|
||||
|
||||
long long k = (n % 2 != 0) ? (n + 1) / 2 : n / 2;
|
||||
|
||||
f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1))
|
||||
: (2 * fib(k - 1) + fib(k)) * fib(k);
|
||||
return f[n];
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Main Function
|
||||
for (long long i = 1; i < 93; i++) {
|
||||
cout << i << " th fibonacci number is " << fib(i) << "\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -19,6 +19,8 @@ The first element of this matrix is the required result.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
using std::vector;
|
||||
@@ -46,8 +48,7 @@ vector<vector<ll>> multiply(vector<vector<ll>> A, vector<vector<ll>> B) {
|
||||
|
||||
// computing power of a matrix
|
||||
vector<vector<ll>> power(vector<vector<ll>> A, ll p) {
|
||||
if (p == 1)
|
||||
return A;
|
||||
if (p == 1) return A;
|
||||
if (p % 2 == 1) {
|
||||
return multiply(A, power(A, p - 1));
|
||||
} else {
|
||||
@@ -58,14 +59,11 @@ vector<vector<ll>> power(vector<vector<ll>> A, ll p) {
|
||||
|
||||
// main function
|
||||
ll ans(ll n) {
|
||||
if (n == 0)
|
||||
return 0;
|
||||
if (n <= k)
|
||||
return b[n - 1];
|
||||
if (n == 0) return 0;
|
||||
if (n <= k) return b[n - 1];
|
||||
// F1
|
||||
vector<ll> F1(k + 1);
|
||||
for (ll i = 1; i <= k; i++)
|
||||
F1[i] = b[i - 1];
|
||||
for (ll i = 1; i <= k; i++) F1[i] = b[i - 1];
|
||||
|
||||
// Transpose matrix
|
||||
vector<vector<ll>> T(k + 1, vector<ll>(k + 1));
|
||||
|
||||
@@ -1,63 +1,53 @@
|
||||
#include<iostream>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void show_pascal(int **arr, int n)
|
||||
{
|
||||
//pint Pascal's Triangle
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
for (int j = 0; j < n + i; ++j)
|
||||
{
|
||||
if (arr[i][j] == 0)
|
||||
cout << " ";
|
||||
else
|
||||
cout << arr[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
void show_pascal(int **arr, int n) {
|
||||
// pint Pascal's Triangle
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n + i; ++j) {
|
||||
if (arr[i][j] == 0)
|
||||
std::cout << " ";
|
||||
else
|
||||
std::cout << arr[i][j];
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
int **pascal_triangle(int **arr, int n)
|
||||
{
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
for (int j = n - i - 1; j < n + i; ++j)
|
||||
{
|
||||
if (j == n - i - 1 || j == n + i - 1)
|
||||
arr[i][j] = 1; //The edge of the Pascal triangle goes in 1
|
||||
else
|
||||
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j + 1];
|
||||
}
|
||||
}
|
||||
int **pascal_triangle(int **arr, int n) {
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = n - i - 1; j < n + i; ++j) {
|
||||
if (j == n - i - 1 || j == n + i - 1)
|
||||
arr[i][j] = 1; // The edge of the Pascal triangle goes in 1
|
||||
else
|
||||
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j + 1];
|
||||
}
|
||||
}
|
||||
|
||||
return arr;
|
||||
return arr;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n = 0;
|
||||
int main() {
|
||||
int n = 0;
|
||||
|
||||
cout << "Set Pascal's Triangle Height" << endl;
|
||||
cin >> n;
|
||||
|
||||
//memory allocation (Assign two-dimensional array to store Pascal triangle)
|
||||
int **arr = new int*[n];
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
arr[i] = new int[2 * n - 1];
|
||||
memset(arr[i], 0, sizeof(int)*(2 * n - 1));
|
||||
}
|
||||
|
||||
pascal_triangle(arr, n);
|
||||
show_pascal(arr, n);
|
||||
std::cout << "Set Pascal's Triangle Height" << std::endl;
|
||||
std::cin >> n;
|
||||
|
||||
//deallocation
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
delete[] arr[i];
|
||||
}
|
||||
delete[] arr;
|
||||
// memory allocation (Assign two-dimensional array to store Pascal triangle)
|
||||
int **arr = new int *[n];
|
||||
for (int i = 0; i < n; ++i) {
|
||||
arr[i] = new int[2 * n - 1];
|
||||
memset(arr[i], 0, sizeof(int) * (2 * n - 1));
|
||||
}
|
||||
|
||||
return 0;
|
||||
pascal_triangle(arr, n);
|
||||
show_pascal(arr, n);
|
||||
|
||||
// deallocation
|
||||
for (int i = 0; i < n; ++i) {
|
||||
delete[] arr[i];
|
||||
}
|
||||
delete[] arr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
105
sorting/shell_sort2.cpp
Normal file
105
sorting/shell_sort2.cpp
Normal file
@@ -0,0 +1,105 @@
|
||||
#include <array>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
|
||||
// for std::swap
|
||||
#include <utility>
|
||||
|
||||
template <class T> void show_data(T *arr, size_t LEN) {
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < LEN; i++)
|
||||
std::cout << arr[i] << ", ";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
template <class T, size_t N> void show_data(T (&arr)[N]) { show_data(arr, N); }
|
||||
|
||||
/**
|
||||
* Optimized algorithm - takes half the time by utilizing
|
||||
* Mar
|
||||
**/
|
||||
template <class T> void shell_sort(T *arr, size_t LEN) {
|
||||
const unsigned int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
|
||||
const unsigned int gap_len = 8;
|
||||
size_t i, j, g;
|
||||
|
||||
for (g = 0; g < gap_len; g++) {
|
||||
unsigned int gap = gaps[g];
|
||||
for (i = gap; i < LEN; i++) {
|
||||
T tmp = arr[i];
|
||||
|
||||
for (j = i; j >= gap && (arr[j - gap] - tmp) > 0; j -= gap)
|
||||
arr[j] = arr[j - gap];
|
||||
|
||||
arr[j] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <class T, size_t N> void shell_sort(T (&arr)[N]) {
|
||||
shell_sort(arr, N);
|
||||
}
|
||||
|
||||
/**
|
||||
* function to compare sorting using cstdlib's qsort
|
||||
**/
|
||||
int compare(const void *a, const void *b) {
|
||||
int arg1 = *static_cast<const int *>(a);
|
||||
int arg2 = *static_cast<const int *>(b);
|
||||
|
||||
if (arg1 < arg2)
|
||||
return -1;
|
||||
if (arg1 > arg2)
|
||||
return 1;
|
||||
return 0;
|
||||
|
||||
// return (arg1 > arg2) - (arg1 < arg2); // possible shortcut
|
||||
// return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present)
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int i, NUM_DATA;
|
||||
|
||||
if (argc == 2)
|
||||
NUM_DATA = atoi(argv[1]);
|
||||
else
|
||||
NUM_DATA = 200;
|
||||
|
||||
// int array = new int[NUM_DATA];
|
||||
int *data = new int[NUM_DATA];
|
||||
int *data2 = new int[NUM_DATA];
|
||||
// int array2 = new int[NUM_DATA];
|
||||
int range = 1800;
|
||||
|
||||
std::srand(time(NULL));
|
||||
for (i = 0; i < NUM_DATA; i++)
|
||||
data[i] = data2[i] = (std::rand() % range) - (range >> 1);
|
||||
|
||||
std::cout << "Unsorted original data: " << std::endl;
|
||||
show_data(data, NUM_DATA);
|
||||
std::clock_t start = std::clock();
|
||||
shell_sort(data, NUM_DATA);
|
||||
std::clock_t end = std::clock();
|
||||
|
||||
std::cout << std::endl
|
||||
<< "Data Sorted using custom implementation: " << std::endl;
|
||||
show_data(data, NUM_DATA);
|
||||
|
||||
double elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC;
|
||||
std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl;
|
||||
|
||||
start = std::clock();
|
||||
qsort(data2, NUM_DATA, sizeof(data2[0]), compare);
|
||||
end = std::clock();
|
||||
std::cout << "Data Sorted using cstdlib qsort: " << std::endl;
|
||||
show_data(data2, NUM_DATA);
|
||||
|
||||
elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC;
|
||||
std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl;
|
||||
|
||||
free(data);
|
||||
free(data2);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user