-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathPancake Sorting.java
41 lines (38 loc) · 1.05 KB
/
Pancake Sorting.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
// Runtime: 2 ms (Top 77.93%) | Memory: 42.7 MB (Top 83.40%)
// BruteForce Approach!
// Author - Nikhil Sharma
// LinkedIn - https://www.linkedin.com/in/nikhil-sharma-41a287226/
// Twitter - https://twitter.com/Sharma_Nikh12
class Solution {
public List<Integer> pancakeSort(int[] arr) {
List<Integer> list = new ArrayList<>();
int n = arr.length;
while(n!=1) {
int maxIndex = findIndex(arr,n);
reverse(arr, maxIndex);
reverse(arr, n-1);
list.add(maxIndex+1);
list.add(n);
n--;
}
return list;
}
static int findIndex(int[] arr, int value) {
for(int i=0; i<arr.length; i++) {
if(arr[i] == value){
return i;
}
}
return 0;
}
static void reverse(int[] arr, int maxIndex) {
int l = 0;
while(l<maxIndex) {
int temp = arr[l];
arr[l] = arr[maxIndex];
arr[maxIndex] = temp;
l++;
maxIndex--;
}
}
}