Skip to content

add pancake sort #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion algorithm/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
"quick": "Quicksort",
"radix": "Radix Sort",
"selection": "Selection Sort",
"shell": "Shellsort"
"shell": "Shellsort",
"pancake": "Pancake Sort"
},
"name": "Sorting"
},
Expand Down
31 changes: 31 additions & 0 deletions algorithm/sorting/pancake/basic/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
logger._print('original array = [' + D.join(', ') + ']');
var N = D.length;
function flip (start) {
tracer._select(start, N)._wait();
var idx = 0;
for (var i=start;i<(start+N)/2;i++) {
tracer._select(i)._wait();
var temp = D[i];
D[i] = D[N-idx-1];
D[N-idx-1] = temp;
idx++;
tracer._notify(i, D[i])._notify(N-idx, D[N-idx])._wait();
tracer._denotify(i)._denotify(N-idx);
tracer._deselect(i);
}
tracer._deselect(start, N);
}
for (var i=0;i<N-1;i++) {
logger._print('round ' + (i+1));
var currArr = D.slice(i, N);
var currMax = currArr.reduce((prev, curr, idx) => {
return (curr > prev.val) ? { idx: idx, val: curr} : prev;
}, {idx: 0, val: currArr[0]});
if (currMax.idx !== i) {
logger._print('flip at ' + (currMax.idx+i) + ' (step 1)');
flip(currMax.idx+i, N);
logger._print('flip at ' + (i) + ' (step 2)');
flip(i, N);
}
}
logger._print('sorted array = [' + D.join(', ') + ']');
5 changes: 5 additions & 0 deletions algorithm/sorting/pancake/basic/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var chart = new ChartTracer();
var tracer = new Array1DTracer().attach(chart);
var logger = new LogTracer();
var D = Array1D.random(10);
tracer._setData(D);
14 changes: 14 additions & 0 deletions algorithm/sorting/pancake/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Pancake Sort": "Pancake Sort,inspired from sorting a stack of pancake using spatula, is a simple sorting algorithm that only have 1 operation called flip. </br> flip (i) : Reverse array from i to N where N is length of array ",
"Complexity": {
"time": "worst $O(n^2)$",
"space": "worst $O(1)$ auxiliary"
},
"References": [
"<a href='https://en.wikipedia.org/wiki/Pancake_sorting'>Wikipedia</a>",
"<a href='http://www.geeksforgeeks.org/pancake-sorting/'>Geeksforgeeks</a>"
],
"files": {
"basic": "Pancake sort"
}
}