forked from iiitv/algos
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Shell Sort [Javascript] (iiitv#312)
* 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
1 parent
c5e68c9
commit 467d1fb
Showing
2 changed files
with
31 additions
and
1 deletion.
There are no files selected for viewing
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
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,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(); |