Skip to content

Commit

Permalink
Added Insertion Sort [JavaScript] (iiitv#394)
Browse files Browse the repository at this point in the history
* Added Insertion Sort [JavaScript]

*  bot fix v1.0
  • Loading branch information
anshumanv authored and aviaryan committed Jul 6, 2017
1 parent 73f2b94 commit f85ce51
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 @@ -25,7 +25,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
| [Euclidean GCD](https://en.wikipedia.org/wiki/Euclidean_algorithm) | [:white_check_mark:](euclidean_gcd/euclidean_gcd.c) | | [:white_check_mark:](euclidean_gcd/EuclideanGCD.java) | [:white_check_mark:](euclidean_gcd/euclidean_gcd.py) | [:white_check_mark:](euclidean_gcd/euclidean_gcd.go) | [:white_check_mark:](euclidean_gcd/euclideanGCD.js) |
| [Exponentiation by Squaring](https://en.wikipedia.org/wiki/Exponentiation_by_squaring) | [:white_check_mark:](exponentiation_by_squaring/exponentiation_by_squaring.c) | | [:white_check_mark:](exponentiation_by_squaring/ExponentiationBySquaring.java) | [:white_check_mark:](exponentiation_by_squaring/exponentiation_by_squaring.py) | [:white_check_mark:](exponentiation_by_squaring/exponentiation_by_squaring.go) | [:white_check_mark:](exponentiation_by_squaring/exponentiationBySquaring.js) |
| [Heap Sort](https://en.wikipedia.org/wiki/Heapsort) | [:white_check_mark:](heap_sort/heap_sort.c) | | [:white_check_mark:](heap_sort/HeapSort.java) | [:white_check_mark:](heap_sort/heap_sort.py) | | [:white_check_mark:](heap_sort/heapSort.js) |
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:white_check_mark:](insertion_sort/insertion_sort.c) | | [:white_check_mark:](insertion_sort/InsertionSort.java)| [:white_check_mark:](insertion_sort/insertion_sort.py) | [:white_check_mark:](insertion_sort/insertion_sort.go) | |
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:white_check_mark:](insertion_sort/insertion_sort.c) | | [:white_check_mark:](insertion_sort/InsertionSort.java)| [:white_check_mark:](insertion_sort/insertion_sort.py) | [:white_check_mark:](insertion_sort/insertion_sort.go) | [:white_check_mark:](insertion_sort/insertionSort.js) |
| [k-NN](https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm) | | | | [:white_check_mark:](k_nn/k_nn.py) | | |
| [Largest Sum Contiguous Subarray](http://www.geeksforgeeks.org/largest-sum-contiguous-subarray/) | [:white_check_mark:](largest_sum_contiguous_subarray/largestSumContiguousSubarray.c) | | [:white_check_mark:](largest_sum_contiguous_subarray/LargestSumContiguousSubarray.java) | [:white_check_mark:](largest_sum_contiguous_subarray/largest_sum_contiguous_subarray.py) | [:white_check_mark:](largest_sum_contiguous_subarray/largestSumContiguousSubarray.go) | [:white_check_mark:](largest_sum_contiguous_subarray/largestSumContiguousSubarray.js) |
| [Linear Regression](https://en.wikipedia.org/wiki/Linear_regression) | | | | [:white_check_mark:](linear_regression/linear_regression.py) | | |
Expand Down
30 changes: 30 additions & 0 deletions insertion_sort/insertionSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* Following algorithm sorts the input array in ascending order
* Time Complexity : O(n^2)
* Auxiliary Space: O(1)
* n is the number of elements in the array to be sorted
*/

function insertionSort (arr) {
/*
: param arr : Array to be sorted
: return : Sorted Array
*/
for (let i = 1; i < arr.length; i++) {
let j = i - 1;
let temp = arr[i];
while (j >= 0 && arr[j] > temp) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
return arr;
}

function main () {
let arr = [5, 9, 3, 1, 99];
arr = insertionSort(arr);
console.log(arr);
}

main();

0 comments on commit f85ce51

Please sign in to comment.