Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Pancake sorting
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Algorithm == An example of the pancake sorting algorithm is given below in [[Python (programming language)|Python]]. The code is similar to [[bubble sort]] or [[selection sort]]. <syntaxhighlight lang="python3" line="1"> def flip(arr, k: int) -> None: left = 0 while left < k: arr[left], arr[k] = arr[k], arr[left] k -= 1 left += 1 def max_index(arr, k: int) -> int: index = 0 for i in range(k): if arr[i] > arr[index]: index = i return index def pancake_sort(arr) -> None: n = len(arr) while n > 1: maxidx = max_index(arr, n) if maxidx != n - 1: if maxidx != 0: flip(arr, maxidx) flip(arr, n - 1) n -= 1 arr = [15, 8, 9, 1, 78, 30, 69, 4, 10] pancake_sort(arr) print(arr) </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)