-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathPancakeSorting.java
54 lines (47 loc) · 1.41 KB
/
PancakeSorting.java
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
// Time complexity is O(n2)
// Space complexity is O(n)
class PancakeSorting {
public List<Integer> pancakeSort(int[] A) {
int len = A.length;
List<Integer> ans = new ArrayList<>();
if(len==0){
return ans;
}
int index = A.length-1;
while(index>=0){
int maxElIndex = getMaxElIndex(A, index); // find the max element index
if(maxElIndex != index){ // if the index is not at the correct position
flip(A, maxElIndex); //Bring the max el to the top
ans.add(maxElIndex+1);
flip(A, index); // Bring the max el to the last index in the list
ans.add(index+1);
}
index--; // reduce the index as we got last elment at the bottom
}
return ans;
}
// helper to identify the max el index
private int getMaxElIndex(int[] A, int len){
int maxIndex = 0;
int maxEl = A[0];
for(int i=0;i<=len;i++){
if(maxEl<A[i]){
maxEl = A[i];
maxIndex = i;
}
}
return maxIndex;
}
// Flip the array
private void flip(int[] A, int len){
int start = 0;
int end = len;
while(start<=end){
int temp = A[start];
A[start] = A[end];
A[end] = temp;
start++;
end--;
}
}
}