Skip to content

Instantly share code, notes, and snippets.

@rootid
Created May 24, 2015 18:01
Show Gist options
  • Save rootid/6c64505981f2652ef667 to your computer and use it in GitHub Desktop.
Save rootid/6c64505981f2652ef667 to your computer and use it in GitHub Desktop.
//:pancake sorting
//:insert the spatuala to max item idx
//:flip the max item idx to first
//:flip all items to size - 1
//:reduce size
void flip (int a[],int n) {
int start = 0;
int end = n;
while (start < end) {
swap(a[start++],a[end--]);
}
}
int findMaxIdx (int a[],int n) {
int mxIdx = 0;
while (n > 0) {
if (a[mxIdx] < a[n]) {
mxIdx = n;
}
n -= 1;
}
return mxIdx ;
}
void panCakeSort (int a[],int n) {
int size_ = n;
while (size_ > 0) {
int mxIdx = findMaxIdx(a,size_ - 1);
flip (a,mxIdx);
flip (a,size_ - 1);
size_ -= 1;
}
}
int main () {
//int a[] = {2,6,4,1,3};
int a[] = {2,6,4,1,3,7,0,-1};
int n = sizeof(a)/sizeof(a[0]);
panCakeSort (a,n);
for (int i=0;i<n;i++) {
cout << a[i] << ",";
}
cout << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment