Added insertion sort algorithm in js and updated README (#37)
* Added insertion sort algorithm in javascript * listed sorting algorithm in javascript in README.mdpull/41/head
parent
4c9fab8025
commit
c7f4950ace
|
@ -26,4 +26,6 @@
|
|||
### JavaScript
|
||||
|
||||
1. [Bubble Sort](js/bubble-sort.js)
|
||||
2. [Insertion Sort](js/insertion-sort.js)
|
||||
3. [Selection Sort](js/selection-sort.js)
|
||||
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
let sort = [12,6,3,88,1,4,8];
|
||||
|
||||
for(let i = 0; i<sort.length; i++){
|
||||
let key = sort[i];
|
||||
let j = i-1;
|
||||
//comparing keys with other elements
|
||||
while(key<sort[j] && j>=0){
|
||||
sort[j+1]=sort[j];
|
||||
j--;
|
||||
}
|
||||
sort[j+1]=key;
|
||||
}
|
||||
console.log(sort)
|
Loading…
Reference in New Issue