diff --git a/algorithm/category.json b/algorithm/category.json index 85fe4061..e9cdf176 100644 --- a/algorithm/category.json +++ b/algorithm/category.json @@ -85,7 +85,8 @@ "quick": "Quicksort", "radix": "Radix Sort", "selection": "Selection Sort", - "shell": "Shellsort" + "shell": "Shellsort", + "pancake": "Pancake Sort" }, "name": "Sorting" }, diff --git a/algorithm/sorting/pancake/basic/code.js b/algorithm/sorting/pancake/basic/code.js new file mode 100644 index 00000000..a51da1e2 --- /dev/null +++ b/algorithm/sorting/pancake/basic/code.js @@ -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 { + 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(', ') + ']'); diff --git a/algorithm/sorting/pancake/basic/data.js b/algorithm/sorting/pancake/basic/data.js new file mode 100644 index 00000000..69e23e94 --- /dev/null +++ b/algorithm/sorting/pancake/basic/data.js @@ -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); diff --git a/algorithm/sorting/pancake/desc.json b/algorithm/sorting/pancake/desc.json new file mode 100644 index 00000000..e116e4f2 --- /dev/null +++ b/algorithm/sorting/pancake/desc.json @@ -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.
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": [ + "Wikipedia", + "Geeksforgeeks" + ], + "files": { + "basic": "Pancake sort" + } +}