forked from imnurav/Hactoberfest2021-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPancake_sort.cpp
76 lines (75 loc) · 1.35 KB
/
Pancake_sort.cpp
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
#include <bits/stdc++.h>
using namespace std;
bool isSorted(vector<int>& arr)
{
if(arr.size()==0 || arr.size()==1)
{
return 1;
}
for(int i=1;i<arr.size();i++)
{
if(arr[i-1]>arr[i])
{
return 0;
}
}
return 1;
}
void flip(vector<int>& arr,int k)
{
int i=0,j=k-1;
while(i<j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j] = temp;
i++;j--;
}
}
vector<int> pancakeSort(vector<int>& arr) {
vector<int> res;
int k=0,j=0,index=-1,large=INT_MIN,n=arr.size();
while(!isSorted(arr))
{
for(int i=0;i<n-j;i++)
{
if(large<arr[i])
{
large=arr[i];
index=i;
}
}
// bringing the largest values at head
k=index+1;
flip(arr,k);
res.push_back(k);
// fliping the head value at its sorted place
k=n-j;
flip(arr,k);
res.push_back(k);
j++;
large=INT_MIN;
}
return res;
}
int main()
{
int n;
cout<<"Enter lenght of array : ";
cin>>n;
vector<int> v;
cout<<"Enter elements in array : ";
for(int i=0;i<n;i++)
{
int x;
cin>>x;
v.push_back(x);
}
pancakeSort(v);
cout<<"\nSorted array : ";
for(auto u: v)
{
cout<<u<<" ";
}
return 0;
}