forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pancakesorting.py
96 lines (70 loc) · 2.33 KB
/
pancakesorting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'''
What is Pancake Sorting??
Given an array of integers, sort the array using a given Flip operation.
This is called Pancake sorting because this uses Flip operation which is analogous to flipping pancakes.
For example, if there are 5 pancakes stacked one of top of the other, then Flip operation is when a spatula is inserted at any point in the stack and then turned upside down flipping all pancakes above it.
'''
'''
Pancake sorting uses only one operations i.e. flip operation
flip(arr,i): Reverse array from 0 to i
'''
'''
Algorithm for Pancake Sorting
1.) Start form the length of the array and reduce it by one while its greater than one
1 Let current length be length. do following for len
--- Find index of maximum element in arr[0..length-1].Let the index of maximum element be i.
--- Call flip(arr,i)
--- Call flip(arr,length-1)
'''
#Python Program for sorting the array
#Reverse the array[0..i]
def flip(arr,i):
st = 0
while st < i:
temp = arr[st]
arr[st] = arr[i]
arr[i] = temp
st+=1
i-=1
#Return index of maximum element in
def maxx(arr,n):
maxx=0
for i in range(0,n):
if arr[i]>arr[maxx]:
maxx=i
return maxx
#Writing the function for Pancake Sorting using array arr and length of arr n as parameters
def PancakeSoritng(arr,n):
#Taking the cuurent length of array as length
# and reducing it by 1
length = n
while length > 1:
#finding index of maximum element
# in arr[0..length-1]
i = maxx(arr,length)
#Moving maximum elelemnt to end
#if not present already
if i!=length-1:
#first moving maximum element to beginning
flip(arr,i)
#now moving maximum element to end
flip(arr,length-1)
length-=1
#Testing our function on a user array
user_input = input("Enter input numbers separated by a comma\n").strip()
#Storing value of input element in the variable arr
arr= [int(elem) for elem in user_input.split(',')]
#Storing length of array in n
n=len(arr)
print('Length of array is {}'.format(n))
#Applying the fuction on the input array
PancakeSoritng(arr,n)
#Printing the sorted array
print("The Sorted array is \n{}".format(arr))
"""
#Sample Input
[2,6,5,1,8,6,9]
#Sample Output
Length of the array is 7
The Sorted array is [1,2,5,6,6,8,9]
"""