Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
pancake_sort.cpp File Reference

pancake sort sorts a disordered stack of pancakes by flipping any number of pancakes using a spatula using minimum number of flips. More...

#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
Include dependency graph for pancake_sort.cpp:

Namespaces

 sorting
 Sorting algorithms.
 
 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 . More...
 
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. More...
 
static void test ()
 Test implementations. More...
 
int main ()
 Main function. More...
 

Detailed Description

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}

Author
Divyansh Gupta
See also
more on Pancake sort
related problem at Leetcode

Function Documentation

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
126  {
127  test();
128  return 0;
129 }
Here is the call graph for this function:

◆ pancakeSort()

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.

Parameters
[start,end]arr our vector of elements.
sizesize of given array
Returns
0 on exit
60  {
61  for (int i = size; i > 1; --i) {
62  int max_index = 0, j; //intialize some variables.
63  T max_value = 0;
64  for (j = 0; j < i; j++) {
65  if (arr[j] >= max_value) {
66  max_value = arr[j];
67  max_index = j;
68  }
69  }
70  if (max_index != i - 1) //check for reversing
71  {
72  reverse(arr, 0, max_index);
73  reverse(arr, 0, i - 1);
74  }
75  }
76  return 0;
77  }
Here is the call graph for this function:

◆ reverse()

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 .

Parameters
[start,end]arr our vector of elements.
startstarting index of array
endending index of array
Returns
void
43  {
44  T temp; //Temporary variable
45  while (start <= end) {
46  temp = arr[start];
47  arr[start] = arr[end];
48  arr[end] = temp;
49  start++;
50  end--;
51  }
52  }

◆ test()

static void test ( )
static

Test implementations.

Returns
void
85  {
86  // example 1: vector of int
87  const int size1 = 7;
88  std::cout << "\nTest 1- as std::vector<int>...";
89  std::vector<int> arr1 = {23, 10, 20, 11, 12, 6, 7};
91  assert(std::is_sorted(arr1.begin(), arr1.end()));
92  std::cout << "Passed\n";
93  for (int i = 0; i < size1; i++) {
94  std::cout << arr1[i] << " ,";
95  }
97 
98  // example 2: vector of double
99  const int size2 = 8;
100  std::cout << "\nTest 2- as std::vector<double>...";
101  std::vector<double> arr2 = {23.56, 10.62, 200.78, 111.484, 3.9, 1.2, 61.77, 79.6};
103  assert(std::is_sorted(arr2.begin(), arr2.end()));
104  std::cout << "Passed\n";
105  for (int i = 0; i < size2; i++) {
106  std::cout << arr2[i] << ", ";
107  }
108  std::cout << std::endl;
109 
110  // example 3:vector of float
111  const int size3 = 7;
112  std::cout << "\nTest 3- as std::vector<float>...";
113  std::vector<float> arr3 = {6.56, 12.62, 200.78, 768.484, 19.27, 68.87, 9.6};
115  assert(std::is_sorted(arr3.begin(), arr3.end()));
116  std::cout << "Passed\n";
117  for (int i = 0; i < size3; i++) {
118  std::cout << arr3[i] << ", ";
119  }
120  std::cout << std::endl;
121 }
Here is the call graph for this function:
sorting::pancake_sort::pancakeSort
int pancakeSort(std::vector< T > &arr, int size)
This implementation is for a C-style array input that gets modified in place.
Definition: pancake_sort.cpp:60
std::vector< int >
std::reverse
T reverse(T... args)
std::is_sorted
T is_sorted(T... args)
std::cout
std::endl
T endl(T... args)
std::vector::begin
T begin(T... args)
test
static void test()
Test implementations.
Definition: pancake_sort.cpp:85
std::end
T end(T... args)