mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-21 03:30:55 +08:00
npx standard --fix
This commit is contained in:
@ -3,34 +3,31 @@
|
||||
* more information: https://en.wikipedia.org/wiki/Gnome_sort
|
||||
*
|
||||
*/
|
||||
function gnomeSort(items) {
|
||||
function gnomeSort (items) {
|
||||
if (items.length <= 1) {
|
||||
return
|
||||
}
|
||||
|
||||
if (items.length <= 1) {
|
||||
let i = 1
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
let i = 1;
|
||||
|
||||
while (i < items.length) {
|
||||
|
||||
if (items[i - 1] <= items[i]) {
|
||||
i++;
|
||||
} else {
|
||||
let temp = items[i];
|
||||
items[i] = items[i - 1];
|
||||
items[i - 1] = temp;
|
||||
|
||||
i = Math.max(1, i - 1);
|
||||
}
|
||||
while (i < items.length) {
|
||||
if (items[i - 1] <= items[i]) {
|
||||
i++
|
||||
} else {
|
||||
const temp = items[i]
|
||||
items[i] = items[i - 1]
|
||||
items[i - 1] = temp
|
||||
|
||||
i = Math.max(1, i - 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Implementation of gnomeSort
|
||||
// Implementation of gnomeSort
|
||||
|
||||
var ar = [5, 6, 7, 8, 1, 2, 12, 14];
|
||||
//Array before Sort
|
||||
console.log(ar);
|
||||
gnomeSort(ar);
|
||||
//Array after sort
|
||||
console.log(ar);
|
||||
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
|
||||
// Array before Sort
|
||||
console.log(ar)
|
||||
gnomeSort(ar)
|
||||
// Array after sort
|
||||
console.log(ar)
|
||||
|
Reference in New Issue
Block a user