Skip to content

[pull] master from TheAlgorithms:master #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 9, 2022
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ add_subdirectory(data_structures)
add_subdirectory(machine_learning)
add_subdirectory(numerical_methods)
add_subdirectory(graph)
add_subdirectory(divide_and_conquer)

cmake_policy(SET CMP0054 NEW)
cmake_policy(SET CMP0057 NEW)
Expand Down
664 changes: 334 additions & 330 deletions DIRECTORY.md

Large diffs are not rendered by default.

88 changes: 88 additions & 0 deletions bit_manipulation/find_non_repeating_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* @file
* @brief Implementation to find the non repeating integer
* in an array of repeating integers. [Single
* Number](https://leetcode.com/problems/single-number/)
*
* @details
* Given an array of integers in which all of the numbers occur exactly
* twice except one integer which occurs only once. Find the non-repeating
* integer.
*
* Worst Case Time Complexity: O(n)
* Space complexity: O(1)

* @author [Ravidev Pandey](https://github.com/literalEval)
*/

#include <cassert> /// for assert
#include <iostream> /// for IO operations
#include <vector> /// storing the numbers

/**
* @namespace bit_manipulation
* @brief Bit manipulation algorithms
*/
namespace bit_manipulation {
/**
* @namespace find_non_repeating_integer
* @brief Functions to find the non repeating integer
* in an array of repeating integers. [Single
* Number](https://leetcode.com/problems/single-number/)
*/
namespace find_non_repeating_integer {
/**
* @brief The main function implements find single number
* @param nums vector of integers
* @returns returns the integer that occurs only once
*/
int64_t find_non_repeating_integer(const std::vector<int>& nums) {
// The idea is based on the property of XOR.
// We know that 'a' XOR 'a' is '0' and '0' XOR 'b'
// is b.
// Using this, if we XOR all the elements of the array,
// the repeating elements will give '0' and this '0'
// with the single number will give the number itself.

int _xor = 0;

for (const int& num: nums) {
_xor ^= num;
}

return _xor;
}
} // namespace find_non_repeating_integer
} // namespace bit_manipulation

/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
// n = 10,2 return 14

std::vector<int> nums_one{1, 1, 2, 2, 4, 5, 5};
std::vector<int> nums_two{203, 3434, 4545, 3434, 4545};
std::vector<int> nums_three{90, 1, 3, 90, 3};

assert(bit_manipulation::find_non_repeating_integer::
find_non_repeating_integer(nums_one) ==
4); // 4 is non repeating
assert(bit_manipulation::find_non_repeating_integer::
find_non_repeating_integer(nums_two) ==
203); // 203 is non repeating
assert(bit_manipulation::find_non_repeating_integer::
find_non_repeating_integer(nums_three) ==
1); // 1 is non repeating

std::cout << "All test cases successfully passed!" << std::endl;
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // run self-test implementations
return 0;
}
2 changes: 1 addition & 1 deletion data_structures/stack_using_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <queue> /// for queue data structure

/**
* @namespace data_strcutres
* @namespace data_structures
* @brief Data structures algorithms
*/
namespace data_structures {
Expand Down
18 changes: 18 additions & 0 deletions divide_and_conquer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
# with full pathname. RELATIVE may makes it easier to extract an executable name
# automatically.
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
foreach( testsourcefile ${APP_SOURCES} )
# I used a simple string replace, to cut off .cpp.
string( REPLACE ".cpp" "" testname ${testsourcefile} )
add_executable( ${testname} ${testsourcefile} )

set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX)
if(OpenMP_CXX_FOUND)
target_link_libraries(${testname} OpenMP::OpenMP_CXX)
endif()
install(TARGETS ${testname} DESTINATION "bin/divide_and_conquer")

endforeach( testsourcefile ${APP_SOURCES} )
2 changes: 1 addition & 1 deletion graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if(OpenGL_FOUND)
include(ExternalProject)
ExternalProject_Add (
FREEGLUT-PRJ
URL https://sourceforge.net/projects/freeglut/files/freeglut/3.2.1/freeglut-3.2.1.tar.gz
URL https://github.com/FreeGLUTProject/freeglut/releases/download/v3.2.1/freeglut-3.2.1.tar.gz
URL_MD5 cd5c670c1086358598a6d4a9d166949d
CMAKE_GENERATOR ${CMAKE_GENERATOR}
CMAKE_GENERATOR_TOOLSET ${CMAKE_GENERATOR_TOOLSET}
Expand Down
4 changes: 2 additions & 2 deletions others/stairs_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ where number of pairs line is given by user
/** main function */
int main() {
int l, st = 2, x, r, z, n, sp;
std::cout << "enter Index ";
std::cout << "Enter number of pair - ";
std::cin >> x;
z = x;
for (r = 1; r <= x; r++) {
Expand All @@ -26,7 +26,7 @@ int main() {
std::cout << " ";
}
for (l = 1; l <= st; l++) {
std::cout << "*";
std::cout << "\\*";
}
std::cout << std::endl;
}
Expand Down
139 changes: 139 additions & 0 deletions physics/ground_to_ground_projectile_motion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/**
* @file
* @brief Ground to ground [projectile
* motion](https://en.wikipedia.org/wiki/Projectile_motion) equation
* implementations
* @details Ground to ground projectile motion is when a projectile's trajectory
* starts at the ground, reaches the apex, then falls back on the ground.
*
* @author [Focusucof](https://github.com/Focusucof)
*/

#include <cassert> /// for assert()
#include <cmath> /// for std::pow(), std::sin(), and std::cos()
#include <iostream> /// for IO operations

/**
* @namespace physics
* @brief Physics algorithms
*/
namespace physics {
/**
* @namespace ground_to_ground_projectile_motion
* @brief Functions for the Ground to ground [projectile
* motion](https://en.wikipedia.org/wiki/Projectile_motion) equation
*/
namespace ground_to_ground_projectile_motion {
/**
* @brief Convert radians to degrees
* @param radian Angle in radians
* @param PI The definition of the constant PI
* @returns Angle in degrees
*/
double degrees_to_radians(double radian, double PI = 3.14) {
return (radian * (PI / 180));
}

/**
* @brief Calculate the time of flight
* @param initial_velocity The starting velocity of the projectile
* @param angle The angle that the projectile is launched at in degrees
* @param gravity The value used for the gravity constant
* @returns The time that the projectile is in the air for
*/
template <typename T>
T time_of_flight(T initial_velocity, T angle, double gravity = 9.81) {
double Viy = initial_velocity * (std::sin(degrees_to_radians(angle))); // calculate y component of the initial velocity
return 2.0 * Viy / gravity;
}

/**
* @brief Calculate the horizontal distance that the projectile travels
* @param initial_velocity The starting velocity of the projectile
* @param time The time that the projectile is in the air
* @returns Horizontal distance that the projectile travels
*/
template <typename T>
T horizontal_range(T initial_velocity, T angle, T time) {
double Vix = initial_velocity * (std::cos(degrees_to_radians(angle))); // calculate x component of the initial velocity
return Vix * time;
}

/**
* @brief Calculate the max height of the projectile
* @param initial_velocity The starting velocity of the projectile
* @param angle The angle that the projectile is launched at in degrees
* @param gravity The value used for the gravity constant
* @returns The max height that the projectile reaches
*/
template <typename T>
T max_height(T initial_velocity, T angle, double gravity = 9.81) {
double Viy = initial_velocity * (std::sin(degrees_to_radians(angle))); // calculate y component of the initial velocity
return (std::pow(Viy, 2) / (2.0 * gravity));
}
} // namespace ground_to_ground_projectile_motion
} // namespace physics

/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
// initial input variables
double initial_velocity = 5.0; // double initial_velocity input
double angle = 40.0; // double angle input

// 1st test
double expected_time_of_flight = 0.655; // expected time output
double flight_time_output =
std::round(physics::ground_to_ground_projectile_motion::time_of_flight(initial_velocity, angle) * 1000.0) /
1000.0; // round output to 3 decimal places

std::cout << "Projectile Flight Time (double)" << std::endl;
std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
std::cout << "Input Angle: " << angle << std::endl;
std::cout << "Expected Output: " << expected_time_of_flight << std::endl;
std::cout << "Output: " << flight_time_output << std::endl;
assert(flight_time_output == expected_time_of_flight);
std::cout << "TEST PASSED" << std::endl << std::endl;

// 2nd test
double expected_horizontal_range = 2.51; // expected range output
double horizontal_range_output =
std::round(physics::ground_to_ground_projectile_motion::horizontal_range(initial_velocity, angle,
flight_time_output) *
100.0) /
100.0; // round output to 2 decimal places

std::cout << "Projectile Horizontal Range (double)" << std::endl;
std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
std::cout << "Input Angle: " << angle << std::endl;
std::cout << "Input Time Of Flight: " << flight_time_output << std::endl;
std::cout << "Expected Output: " << expected_horizontal_range << std::endl;
std::cout << "Output: " << horizontal_range_output << std::endl;
assert(horizontal_range_output == expected_horizontal_range);
std::cout << "TEST PASSED" << std::endl << std::endl;

// 3rd test
double expected_max_height = 0.526; // expected height output
double max_height_output =
std::round(physics::ground_to_ground_projectile_motion::max_height(initial_velocity, angle) * 1000.0) /
1000.0; // round output to 3 decimal places

std::cout << "Projectile Max Height (double)" << std::endl;
std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
std::cout << "Input Angle: " << angle << std::endl;
std::cout << "Expected Output: " << expected_max_height << std::endl;
std::cout << "Output: " << max_height_output << std::endl;
assert(max_height_output == expected_max_height);
std::cout << "TEST PASSED" << std::endl << std::endl;
}

/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // run self-test implementations
return 0;
}
4 changes: 2 additions & 2 deletions sorting/merge_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
* arr[l..m] and arr[m+1..r] are sorted and merges the two
* sorted sub-arrays into one.
*
* @param arr - array with two halves arr[l...m] and arr[m+1...l]
* @param arr - array with two halves arr[l...m] and arr[m+1...r]
* @param l - left index or start index of first half array
* @param m - right index or end index of first half array
*
* (The second array starts form m+1 and goes till l)
* (The second array starts form m+1 and goes till r)
*
* @param r - end index or right index of second half array
*/
Expand Down
Loading