Skip to content
This repository was archived by the owner on Jun 26, 2022. It is now read-only.

Commit 85ea017

Browse files
authored
sejalshri_pancakesort.js
1 parent 078046e commit 85ea017

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

js/sejalshri_pancakesort.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var n = prompt("enter the size of an array required");
2+
var arr = new Array();
3+
//taking input for array
4+
for (let i = 0; i < n; i++) {
5+
arr[i] = prompt("element" + (i + 1) + ":");
6+
arr[i] = parseInt(arr[i]);
7+
}
8+
var count = 0;
9+
for (let i = 0; i < n - 1; i++) {
10+
let max = arr[0];
11+
let m = 0,
12+
t = m;
13+
//finding maximum element from unsorted array
14+
for (let j = 1; j < n - count; j++) {
15+
if (arr[j] > max) {
16+
max = arr[j];
17+
t = j;
18+
}
19+
}
20+
if (t != m) {
21+
let newarr;
22+
//reversing the array until the maximum element position
23+
newarr = arr.slice(0, t + 1).reverse();
24+
for (let i = 0; i < t + 1; i++) {
25+
arr[i] = newarr[i];
26+
}
27+
}
28+
let neww;
29+
//reversing the whole unsorted array
30+
neww = arr.slice(0, n - count).reverse();
31+
for (let i = 0; i < n - count; i++) {
32+
arr[i] = neww[i];
33+
}
34+
count++;
35+
}
36+
console.log("the sorted array is " + arr);
37+
38+
/* sample input-output
39+
enter the size of an array required: 5
40+
element 1:56
41+
element 2:23
42+
element 3:78
43+
element 4:18
44+
element 5:9
45+
the sorted array is 9,18,23,56,78 */

0 commit comments

Comments
 (0)