Skip to content

Commit

Permalink
Added Shell Sort [Javascript] (iiitv#312)
Browse files Browse the repository at this point in the history
* Added Shell Sort [Javascript]

* Added Shell Sort [Javascript]

* Auto stash before rebase of "origin/master"
Fix spaces

* Added param comment

* Renamed

* Fixed temp declaration
  • Loading branch information
mohitkyadav authored and aviaryan committed Jun 19, 2017
1 parent c5e68c9 commit 467d1fb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
| [Quick Select](https://en.wikipedia.org/wiki/Quickselect) | [:white_check_mark:](quick_select/quick_select.c) | | [:white_check_mark:](quick_select/QuickSelect.java) | [:white_check_mark:](quick_select/quick_select.py) | | |
| [Quicksort](https://en.wikipedia.org/wiki/Quicksort) | [:white_check_mark:](quicksort/quicksort.c) | | [:white_check_mark:](quicksort/QuickSort.java) | [:white_check_mark:](quicksort/quick_sort.py) | [:white_check_mark:](quicksort/quick_sort.go) | |
| [Rod Cutting Problem](http://www.geeksforgeeks.org/dynamic-programming-set-13-cutting-a-rod/) | [:white_check_mark:](rod_cutting_problem/rod_cutting.c) | | [:white_check_mark:](rod_cutting_problem/RodCutting.java) | [:white_check_mark:](rod_cutting_problem/rod_cutting.py) | [:white_check_mark:](rod_cutting_problem/rod_cutting.go) | |
| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | | [:white_check_mark:](shell_sort/ShellSort.cpp) | | [:white_check_mark:](/shell_sort/shell_sort.py) | [:white_check_mark:](shell_sort/shell_sort.go) | |
| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | | [:white_check_mark:](shell_sort/ShellSort.cpp) | | [:white_check_mark:](/shell_sort/shell_sort.py) | [:white_check_mark:](shell_sort/shell_sort.go) | [:white_check_mark:](shell_sort/shellSort.js) |
| [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) | [:white_check_mark:](sieve_of_eratosthenes/sieveOfEratosthenes.c) | | [:white_check_mark:](sieve_of_eratosthenes/SieveOfEratosthenes.java) | [:white_check_mark:](sieve_of_eratosthenes/sieve_of_eratosthenes.py) | [:white_check_mark:](sieve_of_eratosthenes/sieve_of_eratosthenes.go) | |
| [Sleep Sort](http://www.geeksforgeeks.org/sleep-sort-king-laziness-sorting-sleeping/) | | | | [:white_check_mark:](sleep_sort/sleep_sort.py) | | |

Expand Down
30 changes: 30 additions & 0 deletions shell_sort/shellSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Worst case time complexity = O(n^2)
* Best case complexity = O(nlog(n))
* @param {Array} data
* returns sorted data
*/
function shellSort (data) {
let temp;
for (let i = Math.floor(data.length / 2); i > 0; i = Math.floor(i / 2)) {
for (let j = i; j < data.length; j++) {
for (let k = j - i; k >= 0; k -= i) {
if (data[k + i] >= data[k]) {
break;
} else {
temp = data[k];
data[k] = data[k + i];
data[k + i] = temp;
}
}
}
}
return data;
}

function main () {
let data = [1000, 45, -45, 121, 47, 45, 65, 121, -1, 103, 45, 34];
console.log(shellSort(data));
}

main();

0 comments on commit 467d1fb

Please sign in to comment.