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

Commit 678b550

Browse files
Merge pull request #143 from sejalshri/master
sejalshri_pancakesort.js #47
2 parents 900ed52 + 06e8717 commit 678b550

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

CONTRIBUTORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ If you have contributed to this repository, kindly add your username here
1010
- [Giancarmine](https://github.com/Giancarmine)
1111
- [Mon Pacleb](https://github.com/bananaKetchup)
1212
- [lucifer79gg](https://github.com/lucifer79gg)
13+
- [sejalshri](https://github.com/sejalshri)
1314
- [afaditya](https://github.com/afaditya)
1415
- [aaishikasb](https://github.com/aaishikasb)
1516
- [sam0hack](https://github.com/sam0hack)

js/sejalshri_pancakesort.js

+45
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)