Change syntax for follow the pattern

This commit is contained in:
edumoreira1506 2019-09-10 14:06:39 -03:00
parent 3b3d7b9ec5
commit e1b38711dd

View File

@ -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;
}
}