Skip to content

Commit f85ce51

Browse files
anshumanvaviaryan
authored andcommitted
Added Insertion Sort [JavaScript] (iiitv#394)
* Added Insertion Sort [JavaScript] * bot fix v1.0
1 parent 73f2b94 commit f85ce51

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
2525
| [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) |
2626
| [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) |
2727
| [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) |
28-
| [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) | |
28+
| [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) |
2929
| [k-NN](https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm) | | | | [:white_check_mark:](k_nn/k_nn.py) | | |
3030
| [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) |
3131
| [Linear Regression](https://en.wikipedia.org/wiki/Linear_regression) | | | | [:white_check_mark:](linear_regression/linear_regression.py) | | |

insertion_sort/insertionSort.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* Following algorithm sorts the input array in ascending order
2+
* Time Complexity : O(n^2)
3+
* Auxiliary Space: O(1)
4+
* n is the number of elements in the array to be sorted
5+
*/
6+
7+
function insertionSort (arr) {
8+
/*
9+
: param arr : Array to be sorted
10+
: return : Sorted Array
11+
*/
12+
for (let i = 1; i < arr.length; i++) {
13+
let j = i - 1;
14+
let temp = arr[i];
15+
while (j >= 0 && arr[j] > temp) {
16+
arr[j + 1] = arr[j];
17+
j--;
18+
}
19+
arr[j + 1] = temp;
20+
}
21+
return arr;
22+
}
23+
24+
function main () {
25+
let arr = [5, 9, 3, 1, 99];
26+
arr = insertionSort(arr);
27+
console.log(arr);
28+
}
29+
30+
main();

0 commit comments

Comments
 (0)