TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
pancake sort sorts a disordered stack of pancakes by flipping any number of pancakes using a spatula using minimum number of flips. More...
#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
Go to the source code of this file.
Namespaces | |
namespace | sorting |
for working with vectors | |
namespace | pancake_sort |
Functions for Pancake sort algorithm. | |
Functions | |
template<typename T > | |
void | sorting::pancake_sort::reverse (std::vector< T > &arr, int start, int end) |
This implementation is for reversing elements in a a C-style array . | |
template<typename T > | |
int | sorting::pancake_sort::pancakeSort (std::vector< T > &arr, int size) |
This implementation is for a C-style array input that gets modified in place. | |
static void | test () |
Test implementations. | |
int | main () |
Main function. | |
pancake sort sorts a disordered stack of pancakes by flipping any number of pancakes using a spatula using minimum number of flips.
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}
Definition in file pancake_sort.cpp.
int main | ( | void | ) |
Main function.
Definition at line 128 of file pancake_sort.cpp.
int sorting::pancake_sort::pancakeSort | ( | std::vector< T > & | arr, |
int | size ) |
This implementation is for a C-style array input that gets modified in place.
[start,end] | arr our vector of elements. |
size | size of given array |
Definition at line 61 of file pancake_sort.cpp.
void sorting::pancake_sort::reverse | ( | std::vector< T > & | arr, |
int | start, | ||
int | end ) |
This implementation is for reversing elements in a a C-style array .
[start,end] | arr our vector of elements. |
start | starting index of array |
end | ending index of array |
Definition at line 43 of file pancake_sort.cpp.
|
static |
Test implementations.
Definition at line 86 of file pancake_sort.cpp.