-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathGnomeSort.js
77 lines (56 loc) · 2.08 KB
/
GnomeSort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Gnome sort is a simple algorithm which is similar to Insertion sort. The key idea of Gnome is to swap adjacent elements.
Inspired by the standard Dutch Garden Gnome sorting the flower pots. A garden gnome sorts the flower pots as :
Algorithm : 1) If the flower pot just before and after him are in correct order, then he moves one step forward.
2) If it is not in correct order, he swaps the pots and moves back one step
3) At the starting when there is no pot before him, he stpes forward and on reaching the end of the pot line, the list is sorted
Time Complexity : O(n ^ 2)
*/
// Prompt node package for taking user input
const prompt = require("prompt-sync")({ sigint: true });
// Perform gnomeSort
function gnomeSort(array) {
let start = 0;
// While the start (position counter) is less than array length
while (start < array.length) {
if (start === 0) start++;
// If adjacent elements are sorted, increment
if (array[start] >= array[start - 1]) start++;
// If not sorted, swap the elements & decrement the start
else {
[array[start], array[start - 1]] = [array[start - 1], array[start]];
start--;
}
}
}
/* Workflow of user input */
// Take array length as input
let arrayLength = +prompt("Enter array length - ");
// Check whether the entered value is number or not
if (isNaN(arrayLength)) return console.log("Only numbers are allowed");
// Globally declared array
let array = [];
// Take array items
for (let i = 1; i <= arrayLength; i++) {
array.push(+prompt(`Enter ${i} element - `));
// Check whether the entered value is number or not
if (array.includes(NaN)) return console.log("Only numbers are allowed");
}
console.log("Your array - ", array);
// Call the algorithm
gnomeSort(array);
console.log("Sorted array - ", array);
// Sample I/O
/*
> node Gnome
Enter array length - 7
Enter 1 element - 40
Enter 2 element - 50
Enter 3 element - 21
Enter 4 element - 90
Enter 5 element - 70
Enter 6 element - 30
Enter 7 element - 10
Your array - [ 40, 50, 21, 90, 70, 30, 10 ]
Sorted array - [ 10, 21, 30, 40, 50, 70, 90 ]
*/