clang-format and clang-tidy fixes for 3d017c44

This commit is contained in:
github-actions 2020-10-18 20:10:44 +00:00
parent 0e2f1ae681
commit caf2ecc54e
2 changed files with 85 additions and 82 deletions

View File

@ -3,18 +3,20 @@
* @brief Implementation to check whether a number is a power of 2 or not. * @brief Implementation to check whether a number is a power of 2 or not.
* *
* @details * @details
* This algorithm uses bit manipulation to check if a number is a power of 2 or not. * This algorithm uses bit manipulation to check if a number is a power of 2 or
* not.
* *
* ### Algorithm * ### Algorithm
* Let the input number be n, then the bitwise and between n and n-1 will let us know * Let the input number be n, then the bitwise and between n and n-1 will let us
* whether the number is power of 2 or not * know whether the number is power of 2 or not
* *
* For Example, * For Example,
* If N= 32 then N-1 is 31, if we perform bitwise and of these two numbers then * If N= 32 then N-1 is 31, if we perform bitwise and of these two numbers then
* the result will be zero, which indicates that it is the power of 2 * the result will be zero, which indicates that it is the power of 2
* If N=23 then N-1 is 22, if we perform bitwise and of these two numbers then * If N=23 then N-1 is 22, if we perform bitwise and of these two numbers then
* the result will not be zero , which indicates that it is not the power of 2 * the result will not be zero , which indicates that it is not the power of 2
* \note This implementation is better than naive recursive or iterative approach. * \note This implementation is better than naive recursive or iterative
* approach.
* *
* @author [Neha Hasija](https://github.com/neha-hasija17) * @author [Neha Hasija](https://github.com/neha-hasija17)
*/ */
@ -25,7 +27,7 @@
* @param n description * @param n description
* @returns void * @returns void
*/ */
void power_of_two(int n){ void power_of_two(int n) {
/** /**
* This function finds whether a number is power of 2 or not * This function finds whether a number is power of 2 or not
* @param n value for which we want to check * @param n value for which we want to check
@ -34,13 +36,12 @@ void power_of_two(int n){
*/ */
/// result stores the /// result stores the
/// bitwise and of n and n-1 /// bitwise and of n and n-1
int result = n & (n-1); int result = n & (n - 1);
if(result==0) if (result == 0) {
std::cout << "Yes, the number " << n << " is a power of 2";
std::cout<<"Yes, the number "<<n<<" is a power of 2"; } else {
else std::cout << "No, the number " << n << " is not a power of 2";
std::cout<<"No, the number "<<n<<" is not a power of 2"; }
} }
/** /**
@ -49,10 +50,10 @@ void power_of_two(int n){
*/ */
int main() { int main() {
int n = 0; int n = 0;
///n stores the input from the user /// n stores the input from the user
std::cout<<"enter a number "<<std::endl; std::cout << "enter a number " << std::endl;
std::cin>>n; std::cin >> n;
///function call with @param n /// function call with @param n
power_of_two(n); power_of_two(n);
return 0; return 0;
} }

View File

@ -1,26 +1,25 @@
/** /**
* @file * @file
* @brief pancake sort sorts a disordered stack of pancakes by flipping any number of pancakes using a spatula using minimum number of flips. * @brief pancake sort sorts a disordered stack of pancakes by flipping any
* number of pancakes using a spatula using minimum number of flips.
* *
* @details * @details
* Unlike a traditional sorting algorithm, which attempts to sort with the fewest comparisons possible, * Unlike a traditional sorting algorithm, which attempts to sort with the
* the goal is to sort the sequence in as few reversals as possible. * fewest comparisons possible, the goal is to sort the sequence in as few
* Overall time complexity of pancake sort is O(n^2) * reversals as possible. Overall time complexity of pancake sort is O(n^2) For
* For example: example 1:- * example: example 1:- Disordered pancake sizes: {2,5,3,7,8} Sorted:
* Disordered pancake sizes: {2,5,3,7,8} * {2,3,5,7,8} For example: example 2:- Disordered pancake sizes:
* Sorted: {2,3,5,7,8} * {22,51,37,73,81} Sorted: {22,37,51,73,81}
* For example: example 2:-
* Disordered pancake sizes: {22,51,37,73,81}
* Sorted: {22,37,51,73,81}
* @author [Divyansh Gupta](https://github.com/divyansh12323) * @author [Divyansh Gupta](https://github.com/divyansh12323)
* @see more on [Pancake sort](https://en.wikipedia.org/wiki/Pancake_sorting) * @see more on [Pancake sort](https://en.wikipedia.org/wiki/Pancake_sorting)
* @see related problem at [Leetcode](https://leetcode.com/problems/pancake-sorting/) * @see related problem at
*/ * [Leetcode](https://leetcode.com/problems/pancake-sorting/)
*/
#include <iostream> // for io operations
#include <vector> // for std::vector
#include <algorithm> // for std::is_sorted #include <algorithm> // for std::is_sorted
#include <cassert> // for std::assert #include <cassert> // for std::assert
#include <iostream> // for io operations
#include <vector> // for std::vector
/** /**
* @namespace sorting * @namespace sorting
@ -29,19 +28,20 @@
namespace sorting { namespace sorting {
/** /**
* @namespace pancake_sort * @namespace pancake_sort
* @brief Functions for [Pancake sort](https://en.wikipedia.org/wiki/Pancake_sorting) algorithm * @brief Functions for [Pancake
* sort](https://en.wikipedia.org/wiki/Pancake_sorting) algorithm
*/ */
namespace pancake_sort { namespace pancake_sort {
/** /**
* @brief This implementation is for reversing elements in a a C-style array . * @brief This implementation is for reversing elements in a a C-style array .
* @param [start,end] arr our vector of elements. * @param [start,end] arr our vector of elements.
* @param start starting index of array * @param start starting index of array
* @param end ending index of array * @param end ending index of array
* @returns void * @returns void
*/ */
template<typename T> template <typename T>
void reverse(std::vector<T> &arr, int start, int end) { void reverse(std::vector<T> &arr, int start, int end) {
T temp; //Temporary variable T temp; // Temporary variable
while (start <= end) { while (start <= end) {
temp = arr[start]; temp = arr[start];
arr[start] = arr[end]; arr[start] = arr[end];
@ -49,17 +49,18 @@ namespace pancake_sort {
start++; start++;
end--; end--;
} }
} }
/** /**
* @brief This implementation is for a C-style array input that gets modified in place. * @brief This implementation is for a C-style array input that gets modified in
* place.
* @param [start,end] arr our vector of elements. * @param [start,end] arr our vector of elements.
* @param size size of given array * @param size size of given array
* @returns 0 on exit * @returns 0 on exit
*/ */
template<typename T> template <typename T>
int pancakeSort(std::vector<T> &arr, int size) { int pancakeSort(std::vector<T> &arr, int size) {
for (int i = size; i > 1; --i) { for (int i = size; i > 1; --i) {
int max_index = 0, j; //intialize some variables. int max_index = 0, j = 0; // intialize some variables.
T max_value = 0; T max_value = 0;
for (j = 0; j < i; j++) { for (j = 0; j < i; j++) {
if (arr[j] >= max_value) { if (arr[j] >= max_value) {
@ -67,14 +68,14 @@ namespace pancake_sort {
max_index = j; max_index = j;
} }
} }
if (max_index != i - 1) //check for reversing if (max_index != i - 1) // check for reversing
{ {
reverse(arr, 0, max_index); reverse(arr, 0, max_index);
reverse(arr, 0, i - 1); reverse(arr, 0, i - 1);
} }
} }
return 0; return 0;
} }
} // namespace pancake_sort } // namespace pancake_sort
} // namespace sorting } // namespace sorting
@ -98,7 +99,8 @@ static void test() {
// example 2: vector of double // example 2: vector of double
const int size2 = 8; const int size2 = 8;
std::cout << "\nTest 2- as std::vector<double>..."; std::cout << "\nTest 2- as std::vector<double>...";
std::vector<double> arr2 = {23.56, 10.62, 200.78, 111.484, 3.9, 1.2, 61.77, 79.6}; std::vector<double> arr2 = {23.56, 10.62, 200.78, 111.484,
3.9, 1.2, 61.77, 79.6};
sorting::pancake_sort::pancakeSort(arr2, size2); sorting::pancake_sort::pancakeSort(arr2, size2);
assert(std::is_sorted(arr2.begin(), arr2.end())); assert(std::is_sorted(arr2.begin(), arr2.end()));
std::cout << "Passed\n"; std::cout << "Passed\n";