Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"env": {
"browser": true,
"node": true,
"commonjs": true,
"jest": true,
"es6": true
},
"globals": {
"err": true,
"req": true,
"res": true,
"next": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-console": "off",
"indent": [ "error", 2 ],
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
"comma-dangle": ["error", "always-multiline"],
"semi": [ "error", "always" ]
}
}

42 changes: 42 additions & 0 deletions BLOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Insertion Sort
Insertion sort is the sorting mechanism where the sorted array is built having one item at a time. The array elements are compared with each other sequentially and then arranged simultaneously in some particular order.


## Pseudocode :

InsertionSort(int[] arr)

FOR i = 1 to arr.length

int j <-- i - 1
int temp <-- arr[i]

WHILE j >= 0 AND temp < arr[j]
arr[j + 1] <-- arr[j]
j <-- j - 1

arr[j + 1] <-- temp




## Trace :

Sample Array : [8,4,23,42,16,15]

![pass1,2,3,4](p1.jpg)
![pass5](p2.jpg)


## Efficency :

I need to implement an Insertion Sort a while loop within a for loop; the for loop iterated over the entire array, the while loop only ran if the current item was less than the item prior.

Time: O(n^2)

The basic operation of this algorithm is comparison. This will happen n * (n-1) number of times…concluding the algorithm to be n squared.

Space: O(1)

No additional space is being created. This array is being sorted in place…keeping the space at constant O(1).

6 changes: 0 additions & 6 deletions README.md

This file was deleted.

31 changes: 31 additions & 0 deletions challanges/insertionSort/insertionSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';
function insertionSort(arr){
for (let i = 1; i < arr.length; i++){
let temp = arr[i];
let j = i-1 ;
console.log('i berfore while ' , i);
console.log('j berfore while ' ,j);

console.log ('temp outside while ' , temp );

while (j >= 0 && arr[j] > temp ){

arr[j+1] = arr[j];
console.log('arr[j]' , arr[j]);
j = j-1 ;
console.log ('temp ' , temp );
console.log('i' , i);
console.log('j' , j);
console.log('arr[j+1]' ,arr[j+1] );

}
arr[j+1] = temp ;
console.log('arr' , arr );
}
return arr ;
}
let array = [8,4,23,42,16,15];
let insert = insertionSort(array);
console.log(insert);

module.exports = insertionSort ;
12 changes: 12 additions & 0 deletions challanges/insertionSort/insertionSort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
const insertionSort = require('./insertionSort.js');
describe('Insertion Sort ' , () => {
it('unsorted array returns the array sorted ' , ()=> {
expect(insertionSort([5,6,2,8,1])).toEqual([1,2,5,6,8]);
});
it('A sorted array returns the same sorted array' , ()=> {
expect(insertionSort([1,2,5,6,8])).toEqual([1,2,5,6,8]);

});

});
Binary file added p1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added p2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading