Repository: keon/algorithms
Pull request #336 (2018-06-07 17:04:36)

Created pancake_sort.py in algorithms/sorts
(Put an `X` inside the `[ ]` to denote check mark `[X]`.)


- [ X ] **If creating a new file :**
  - [ X ] added links to it in the README files ?
  - [ X ] included tests with it ?
  - [ X ] added description (overview of algorithm, time and space compleixty, and possible edge case) in docstrings ?

- [ ] **if done some changes :**
  - [ ] wrote short description in the PR explaining what the changes do ?
  - [ ] Fixes #[issue number] if related to any issue

- [ ] **other**


coveralls: ## Pull Request Test Coverage Report for [Build 558](https://coveralls.io/builds/17376194)

* **12** of **13**   **(92.31%)**  changed or added relevant lines in **2** files are covered.
* **1** unchanged line in **1** file lost coverage.
* Overall coverage increased (+**0.06%**) to **69.352%**

---

|  Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
| :-----|--------------|--------|---: |
| [algorithms/sort/pancake_sort.py](https://coveralls.io/builds/17376194/source?filename=algorithms%2Fsort%2Fpancake_sort.py#L14) | 10 | 11 | 90.91%
<!-- | **Total:** | **12** | **13** | **92.31%** | -->

|  Files with Coverage Reduction | New Missed Lines | % |
| :-----|--------------|--: |
| [algorithms/linkedlist/is_palindrome.py](https://coveralls.io/builds/17376194/source?filename=algorithms%2Flinkedlist%2Fis_palindrome.py#L85) | 1 | 93.33% |
<!-- | **Total:** | **1** |  | -->

|  Totals | [![Coverage Status](https://coveralls.io/builds/17376194/badge)](https://coveralls.io/builds/17376194) |
| :-- | --: |
| Change from base [Build 555](https://coveralls.io/builds/17373477): |  0.06% |
| Covered Lines: | 3220 |
| Relevant Lines: | 4643 |

---
##### 💛  - [Coveralls](https://coveralls.io)

danghai: @9967han Great! Thanks PR 

Patch:
diff --git a/README.md b/README.md
index 7fcf88c..af1f9c5 100644
--- a/README.md
+++ b/README.md
@@ -235,6 +235,7 @@ If you want to uninstall algorithms, it is as simple as:
     - [insertion_sort](algorithms/sort/insertion_sort.py)
     - [meeting_rooms](algorithms/sort/meeting_rooms.py)
     - [merge_sort](algorithms/sort/merge_sort.py)
+    - [pancake_sort](algorithms/sort/pancake_sort.py)
     - [quick_sort](algorithms/sort/quick_sort.py)
     - [radix_sort](algorithms/sort/radix_sort.py)
     - [selection_sort](algorithms/sort/selection_sort.py)
diff --git a/README_CN.md b/README_CN.md
index 72bdaea..f729b88 100644
--- a/README_CN.md
+++ b/README_CN.md
@@ -221,6 +221,7 @@ pip3 uninstall -y algorithms
     - [insertion_sort：插入排序](algorithms/sort/insertion_sort.py)
     - [meeting_rooms：会议室](algorithms/sort/meeting_rooms.py)
     - [merge_sort：归并排序](algorithms/sort/merge_sort.py)
+    - [pancake_sort](algorithms/sort/pancake_sort.py)
     - [quick_sort：快速排序](algorithms/sort/quick_sort.py)
     - [radix_sort](algorithms/sort/radix_sort.py)
     - [selection_sort：选择排序](algorithms/sort/selection_sort.py)
diff --git a/README_GE.md b/README_GE.md
index 5106358..c6b57bf 100644
--- a/README_GE.md
+++ b/README_GE.md
@@ -242,6 +242,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
     - [insertion_sort](algorithms/sort/insertion_sort.py)
     - [meeting_rooms](algorithms/sort/meeting_rooms.py)
     - [merge_sort](algorithms/sort/merge_sort.py)
+    - [pancake_sort](algorithms/sort/pancake_sort.py)
     - [quick_sort](algorithms/sort/quick_sort.py)
     - [radix_sort](algorithms/sort/radix_sort.py)
     - [selection_sort](algorithms/sort/selection_sort.py)
diff --git a/README_JP.md b/README_JP.md
index 0bb40aa..ffc546a 100644
--- a/README_JP.md
+++ b/README_JP.md
@@ -236,6 +236,7 @@ if __name__ == "__main__":
     - [insertion_sort](algorithms/sort/insertion_sort.py)
     - [meeting_rooms](algorithms/sort/meeting_rooms.py)
     - [merge_sort](algorithms/sort/merge_sort.py)
+    - [pancake_sort](algorithms/sort/pancake_sort.py)
     - [quick_sort](algorithms/sort/quick_sort.py)
     - [radix_sort](algorithms/sort/radix_sort.py)
     - [selection_sort](algorithms/sort/selection_sort.py)
diff --git a/README_KR.md b/README_KR.md
index 20efba9..8a30c02 100644
--- a/README_KR.md
+++ b/README_KR.md
@@ -232,6 +232,7 @@ if __name__ == "__main__":
     - [insertion_sort](algorithms/sort/insertion_sort.py)
     - [meeting_rooms](algorithms/sort/meeting_rooms.py)
     - [merge_sort](algorithms/sort/merge_sort.py)
+    - [pancake_sort](algorithms/sort/pancake_sort.py)
     - [quick_sort](algorithms/sort/quick_sort.py)
     - [radix_sort](algorithms/sort/radix_sort.py)
     - [selection_sort](algorithms/sort/selection_sort.py)
diff --git a/algorithms/sort/__init__.py b/algorithms/sort/__init__.py
index a7dba9a..19648fd 100644
--- a/algorithms/sort/__init__.py
+++ b/algorithms/sort/__init__.py
@@ -6,6 +6,7 @@ from .counting_sort import *
 from .heap_sort import *
 from .insertion_sort import *
 from .merge_sort import *
+from .pancake_sort import *
 from .quick_sort import *
 from .selection_sort import *
 from .top_sort import *
diff --git a/algorithms/sort/pancake_sort.py b/algorithms/sort/pancake_sort.py
new file mode 100644
index 0000000..19725c0
--- /dev/null
+++ b/algorithms/sort/pancake_sort.py
@@ -0,0 +1,25 @@
+def pancake_sort(arr):
+    """
+    Pancake_sort
+    Sorting a given array
+    mutation of selection sort
+
+    reference: https://www.geeksforgeeks.org/pancake-sorting/
+    
+    Overall time complexity : O(N^2)
+    """
+
+    len_arr = len(arr)
+    if len_arr <= 1:
+        return arr
+    for cur in range(len(arr), 1, -1):
+        #Finding index of maximum number in arr
+        index_max = arr.index(max(arr[0:cur]))
+        if index_max+1 != cur:
+            #Needs moving
+            if index_max != 0:
+                #reverse from 0 to index_max
+                arr[:index_max+1] = reversed(arr[:index_max+1])
+            # Reverse list
+            arr[:cur] = reversed(arr[:cur])
+    return arr


Test patch:
diff --git a/tests/test_sort.py b/tests/test_sort.py
index 7d8ea8a..3615f19 100644
--- a/tests/test_sort.py
+++ b/tests/test_sort.py
@@ -7,6 +7,7 @@ from algorithms.sort import (
     max_heap_sort, min_heap_sort,
     insertion_sort,
     merge_sort,
+    pancake_sort,
     quick_sort,
     selection_sort,
     bucket_sort,
@@ -60,6 +61,10 @@ class TestSuite(unittest.TestCase):
         self.assertEqual([1, 5, 23, 57, 65, 1232],
                          merge_sort([1, 5, 65, 23, 57, 1232]))
 
+    def test_pancake_sort(self):
+        self.assertEqual([1, 5, 23, 57, 65, 1232],
+                         pancake_sort([1, 5, 65, 23, 57, 1232]))
+        
     def test_quick_sort(self):
         self.assertEqual([1, 5, 23, 57, 65, 1232],
                          quick_sort([1, 5, 65, 23, 57, 1232]))