Skip to content

Commit 2f1f7a3

Browse files
committed
Added Shell Sort
1 parent e75ed58 commit 2f1f7a3

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Algorithm-Visualiser.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,39 @@ class algorithmVisualiser{
268268
}
269269
},
270270

271+
{
272+
name: "Shell Sort", algorithm: async () => {
273+
// Define a gap distance.
274+
let gap = Math.floor(self.dataSet.data.length / 2);
275+
276+
// Until gap is bigger then zero do elements comparisons and swaps.
277+
while (gap > 0) {
278+
// Go and compare all distant element pairs.
279+
for (let i = 0; i < (self.dataSet.data.length - gap); i += 1) {
280+
let currentIndex = i;
281+
let gapShiftedIndex = i + gap;
282+
283+
while (currentIndex >= 0) {
284+
self.dataSet.iteration(); // Iterate counter
285+
// Compare and swap array elements if needed.
286+
if (self.dataSet.lessthan(gapShiftedIndex, currentIndex)) {
287+
self.dataSet.swap(currentIndex, gapShiftedIndex);
288+
}
289+
await self.wait(self.options.delay); self
290+
gapShiftedIndex = currentIndex;
291+
currentIndex -= gap;
292+
}
293+
}
294+
295+
// Shrink the gap.
296+
gap = Math.floor(gap / 2);
297+
}
298+
299+
self.dataSet.swapIndicator = []; // Finished Empty active data
300+
self.dataSet.compIndicator = []; // Finished Empty active data
301+
}
302+
},
303+
271304
];
272305
}
273306
}

0 commit comments

Comments
 (0)