-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
...ce/_posts/Sort-Algorithm(2):-Elementary-Sort-Javascript-Implement-Shell-Sort.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
title: Sort Algorithm(2): Elementary Sort Javascript Implement - Shell Sort | ||
date: 2024-03-23 20:30:41 | ||
tags: [Javascript, Algorithm, Sort] | ||
--- | ||
|
||
Shell Sort is a very efficient sorting algorithm; its time complexity can almost reach O(n) in real-world production. | ||
|
||
The code Shell Sort moves elements in the same way as Selection Sort, but it seeks local sorting and then achieves global sorting. | ||
|
||
Let's see How it works: | ||
|
||
``` | ||
var { less } = require("./Shuffle"); | ||
function shellSort(a) { | ||
var length = a.length; | ||
var h = 1; | ||
while (h < Math.floor(length / 3)) h = 3 * h + 1; //3x + 1 increament | ||
while (h >= 1) { | ||
for (let i = h; i < length; i++) { | ||
// h-insertion_sort | ||
for (let j = i; j >= h && less(a[j], a[j - h]); j -= h) { | ||
let temp = a[j - h]; | ||
a[j - h] = a[j]; | ||
a[j] = temp; | ||
} | ||
} | ||
h = Math.floor(h / 3); // next increament | ||
} | ||
} | ||
module.exports = shellSort; | ||
``` | ||
|
||
Why is Shell Sort so effective compared to Selection Sort? It | ||
moves elements in multiple positions, the number of increments every time, but Selection Sort moves elements in only one position. | ||
|
||
Shell Sort is more simple than Quick Sort, so the application is widespread in small embedded devices. |