1
1
/* *
2
2
* @file
3
- * @brief pancake sort sorts a disordered stack of pancakes by flipping any number of pancakes using a spatula using minimum number of flips.
3
+ * @brief pancake sort sorts a disordered stack of pancakes by flipping any
4
+ * number of pancakes using a spatula using minimum number of flips.
4
5
*
5
6
* @details
6
- * Unlike a traditional sorting algorithm, which attempts to sort with the fewest comparisons possible,
7
- * the goal is to sort the sequence in as few reversals as possible.
8
- * Overall time complexity of pancake sort is O(n^2)
9
- * For example: example 1:-
10
- * Disordered pancake sizes: {2,5,3,7,8}
11
- * Sorted: {2,3,5,7,8}
12
- * For example: example 2:-
13
- * Disordered pancake sizes: {22,51,37,73,81}
14
- * Sorted: {22,37,51,73,81}
7
+ * Unlike a traditional sorting algorithm, which attempts to sort with the
8
+ * fewest comparisons possible, the goal is to sort the sequence in as few
9
+ * reversals as possible. Overall time complexity of pancake sort is O(n^2) For
10
+ * example: example 1:- Disordered pancake sizes: {2,5,3,7,8} Sorted:
11
+ * {2,3,5,7,8} For example: example 2:- Disordered pancake sizes:
12
+ * {22,51,37,73,81} Sorted: {22,37,51,73,81}
15
13
* @author [Divyansh Gupta](https://github.com/divyansh12323)
16
14
* @see more on [Pancake sort](https://en.wikipedia.org/wiki/Pancake_sorting)
17
- * @see related problem at [Leetcode](https://leetcode.com/problems/pancake-sorting/)
18
- */
15
+ * @see related problem at
16
+ * [Leetcode](https://leetcode.com/problems/pancake-sorting/)
17
+ */
19
18
20
- #include < iostream> // for io operations
21
- #include < vector> // for std::vector
22
19
#include < algorithm> // for std::is_sorted
23
20
#include < cassert> // for std::assert
21
+ #include < iostream> // for io operations
22
+ #include < vector> // for std::vector
24
23
25
24
/* *
26
25
* @namespace sorting
29
28
namespace sorting {
30
29
/* *
31
30
* @namespace pancake_sort
32
- * @brief Functions for [Pancake sort](https://en.wikipedia.org/wiki/Pancake_sorting) algorithm
31
+ * @brief Functions for [Pancake
32
+ * sort](https://en.wikipedia.org/wiki/Pancake_sorting) algorithm
33
33
*/
34
34
namespace pancake_sort {
35
- /* *
36
- * @brief This implementation is for reversing elements in a a C-style array .
37
- * @param [start,end] arr our vector of elements.
38
- * @param start starting index of array
39
- * @param end ending index of array
40
- * @returns void
41
- */
42
- template <typename T>
43
- void reverse (std::vector<T> &arr, int start, int end) {
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
- }
35
+ /* *
36
+ * @brief This implementation is for reversing elements in a a C-style array .
37
+ * @param [start,end] arr our vector of elements.
38
+ * @param start starting index of array
39
+ * @param end ending index of array
40
+ * @returns void
41
+ */
42
+ template <typename T>
43
+ void reverse (std::vector<T> &arr, int start, int end) {
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--;
52
51
}
53
- /* *
54
- * @brief This implementation is for a C-style array input that gets modified in place.
55
- * @param [start,end] arr our vector of elements.
56
- * @param size size of given array
57
- * @returns 0 on exit
58
- */
59
- template <typename T>
60
- int pancakeSort (std::vector<T> &arr, int size) {
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 );
52
+ }
53
+ /* *
54
+ * @brief This implementation is for a C-style array input that gets modified in
55
+ * place.
56
+ * @param [start,end] arr our vector of elements.
57
+ * @param size size of given array
58
+ * @returns 0 on exit
59
+ */
60
+ template <typename T>
61
+ int pancakeSort (std::vector<T> &arr, int size) {
62
+ for (int i = size; i > 1 ; --i) {
63
+ int max_index = 0 , j = 0 ; // intialize some variables.
64
+ T max_value = 0 ;
65
+ for (j = 0 ; j < i; j++) {
66
+ if (arr[j] >= max_value) {
67
+ max_value = arr[j];
68
+ max_index = j;
74
69
}
75
70
}
76
- return 0 ;
71
+ if (max_index != i - 1 ) // check for reversing
72
+ {
73
+ reverse (arr, 0 , max_index);
74
+ reverse (arr, 0 , i - 1 );
75
+ }
77
76
}
77
+ return 0 ;
78
+ }
78
79
} // namespace pancake_sort
79
80
} // namespace sorting
80
81
@@ -98,7 +99,8 @@ static void test() {
98
99
// example 2: vector of double
99
100
const int size2 = 8 ;
100
101
std::cout << " \n Test 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 };
102
+ std::vector<double > arr2 = {23.56 , 10.62 , 200.78 , 111.484 ,
103
+ 3.9 , 1.2 , 61.77 , 79.6 };
102
104
sorting::pancake_sort::pancakeSort (arr2, size2);
103
105
assert (std::is_sorted (arr2.begin (), arr2.end ()));
104
106
std::cout << " Passed\n " ;
0 commit comments