Skip to content

Commit

Permalink
new post: Sort Algorithm 2
Browse files Browse the repository at this point in the history
  • Loading branch information
liuduwei committed Mar 23, 2024
1 parent 6594ded commit 5b55fb3
Showing 1 changed file with 40 additions and 0 deletions.
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.

0 comments on commit 5b55fb3

Please sign in to comment.