mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Change syntax for follow the pattern
This commit is contained in:
parent
3b3d7b9ec5
commit
e1b38711dd
@ -5,6 +5,8 @@ export default class GnomeSort extends Sort {
|
||||
* @param {number[]} originalArray
|
||||
*/
|
||||
sort(originalArray) {
|
||||
const sortedArray = originalArray;
|
||||
|
||||
// Variable that will be itering during loop
|
||||
let index = 0;
|
||||
|
||||
@ -12,22 +14,20 @@ export default class GnomeSort extends Sort {
|
||||
while (index < originalArray.length) {
|
||||
// Detects the first iteration
|
||||
if (index === 0) {
|
||||
index++;
|
||||
index += 1;
|
||||
}
|
||||
|
||||
// Detects if the current position is smaller of previous
|
||||
if (originalArray[index] >= originalArray[index - 1]) {
|
||||
index++;
|
||||
index += 1;
|
||||
} else {
|
||||
// Change the position of current position for before
|
||||
const temp = originalArray[index];
|
||||
originalArray[index] = originalArray[index - 1];
|
||||
originalArray[index - 1] = temp;
|
||||
index--;
|
||||
sortedArray[index] = originalArray[index - 1];
|
||||
sortedArray[index - 1] = originalArray[index];
|
||||
index -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
const sortedArray = originalArray;
|
||||
return sortedArray;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user