mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-14 10:50:13 +08:00
clang-format and clang-tidy fixes for db3f6944
This commit is contained in:
@@ -1,17 +1,19 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief A simple program to check if the given number is a magic number or not.
|
||||
* A number is said to be a magic number, if the sum of its digits are calculated
|
||||
* till a single digit recursively by adding the sum of the digits after every addition.
|
||||
* If the single digit comes out to be 1,then the number is a magic number.
|
||||
* @brief A simple program to check if the given number is a magic number or
|
||||
* not. A number is said to be a magic number, if the sum of its digits are
|
||||
* calculated till a single digit recursively by adding the sum of the digits
|
||||
* after every addition. If the single digit comes out to be 1,then the number
|
||||
* is a magic number.
|
||||
*
|
||||
* This is a shortcut method to verify Magic Number.
|
||||
* On dividing the input by 9, if the remainder is 1 then the number is a magic number else not.
|
||||
* The divisibility rule of 9 says that a number is divisible by 9 if the sum of its digits
|
||||
* are also divisible by 9. Therefore, if a number is divisible by 9, then, recursively,
|
||||
* all the digit sums are also divisible by 9. The final digit sum is always 9.
|
||||
* An increase of 1 in the original number will increase the ultimate value by 1,
|
||||
* making it 10 and the ultimate sum will be 1, thus verifying that it is a magic number.
|
||||
* This is a shortcut method to verify Magic Number.
|
||||
* On dividing the input by 9, if the remainder is 1 then the number is a magic
|
||||
* number else not. The divisibility rule of 9 says that a number is divisible
|
||||
* by 9 if the sum of its digits are also divisible by 9. Therefore, if a number
|
||||
* is divisible by 9, then, recursively, all the digit sums are also divisible
|
||||
* by 9. The final digit sum is always 9. An increase of 1 in the original
|
||||
* number will increase the ultimate value by 1, making it 10 and the ultimate
|
||||
* sum will be 1, thus verifying that it is a magic number.
|
||||
* @author [Neha Hasija](https://github.com/neha-hasija17)
|
||||
*/
|
||||
#include <cassert>
|
||||
@@ -28,10 +30,13 @@ bool magic_number(int n) {
|
||||
return false;
|
||||
}
|
||||
// result stores the modulus of @param n with 9
|
||||
int result=n%9;
|
||||
//if result is 1 then the number is a magic number else not
|
||||
if(result==1) return true;
|
||||
else return false;
|
||||
int result = n % 9;
|
||||
// if result is 1 then the number is a magic number else not
|
||||
if (result == 1) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Test function
|
||||
|
||||
@@ -1,26 +1,25 @@
|
||||
/**
|
||||
* @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
|
||||
* Unlike a traditional sorting algorithm, which attempts to sort with the fewest comparisons possible,
|
||||
* the goal is to sort the sequence in as few reversals as possible.
|
||||
* Overall time complexity of pancake sort is O(n^2)
|
||||
* For example: example 1:-
|
||||
* Disordered pancake sizes: {2,5,3,7,8}
|
||||
* Sorted: {2,3,5,7,8}
|
||||
* For example: example 2:-
|
||||
* Disordered pancake sizes: {22,51,37,73,81}
|
||||
* Sorted: {22,37,51,73,81}
|
||||
* Unlike a traditional sorting algorithm, which attempts to sort with the
|
||||
* fewest comparisons possible, the goal is to sort the sequence in as few
|
||||
* reversals as possible. Overall time complexity of pancake sort is O(n^2) For
|
||||
* example: example 1:- Disordered pancake sizes: {2,5,3,7,8} Sorted:
|
||||
* {2,3,5,7,8} 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)
|
||||
* @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 <cassert> // for std::assert
|
||||
#include <iostream> // for io operations
|
||||
#include <vector> // for std::vector
|
||||
|
||||
/**
|
||||
* @namespace sorting
|
||||
@@ -29,52 +28,54 @@
|
||||
namespace sorting {
|
||||
/**
|
||||
* @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 {
|
||||
/**
|
||||
* @brief This implementation is for reversing elements in a a C-style array .
|
||||
* @param [start,end] arr our vector of elements.
|
||||
* @param start starting index of array
|
||||
* @param end ending index of array
|
||||
* @returns void
|
||||
*/
|
||||
template<typename T>
|
||||
void reverse(std::vector<T> &arr, int start, int end) {
|
||||
T temp; //Temporary variable
|
||||
while (start <= end) {
|
||||
temp = arr[start];
|
||||
arr[start] = arr[end];
|
||||
arr[end] = temp;
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
/**
|
||||
* @brief This implementation is for reversing elements in a a C-style array .
|
||||
* @param [start,end] arr our vector of elements.
|
||||
* @param start starting index of array
|
||||
* @param end ending index of array
|
||||
* @returns void
|
||||
*/
|
||||
template <typename T>
|
||||
void reverse(std::vector<T> &arr, int start, int end) {
|
||||
T temp; // Temporary variable
|
||||
while (start <= end) {
|
||||
temp = arr[start];
|
||||
arr[start] = arr[end];
|
||||
arr[end] = temp;
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
/**
|
||||
* @brief This implementation is for a C-style array input that gets modified in place.
|
||||
* @param [start,end] arr our vector of elements.
|
||||
* @param size size of given array
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
template<typename T>
|
||||
int pancakeSort(std::vector<T> &arr, int size) {
|
||||
for (int i = size; i > 1; --i) {
|
||||
int max_index = 0, j; //intialize some variables.
|
||||
T max_value = 0;
|
||||
for (j = 0; j < i; j++) {
|
||||
if (arr[j] >= max_value) {
|
||||
max_value = arr[j];
|
||||
max_index = j;
|
||||
}
|
||||
}
|
||||
if (max_index != i - 1) //check for reversing
|
||||
{
|
||||
reverse(arr, 0, max_index);
|
||||
reverse(arr, 0, i - 1);
|
||||
}
|
||||
/**
|
||||
* @brief This implementation is for a C-style array input that gets modified in
|
||||
* place.
|
||||
* @param [start,end] arr our vector of elements.
|
||||
* @param size size of given array
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
template <typename T>
|
||||
int pancakeSort(std::vector<T> &arr, int size) {
|
||||
for (int i = size; i > 1; --i) {
|
||||
int max_index = 0, j = 0; // intialize some variables.
|
||||
T max_value = 0;
|
||||
for (j = 0; j < i; j++) {
|
||||
if (arr[j] >= max_value) {
|
||||
max_value = arr[j];
|
||||
max_index = j;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
if (max_index != i - 1) // check for reversing
|
||||
{
|
||||
reverse(arr, 0, max_index);
|
||||
reverse(arr, 0, i - 1);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
} // namespace pancake_sort
|
||||
} // namespace sorting
|
||||
|
||||
@@ -98,7 +99,8 @@ static void test() {
|
||||
// example 2: vector of double
|
||||
const int size2 = 8;
|
||||
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);
|
||||
assert(std::is_sorted(arr2.begin(), arr2.end()));
|
||||
std::cout << "Passed\n";
|
||||
|
||||
Reference in New Issue
Block a user