Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 40ba377

Browse files
author
Hari Shekhar
committed
insertion sort completed
1 parent 2097a10 commit 40ba377

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

‎bubbleSort.js‎

Whitespace-only changes.

‎insertionSort.js‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1-
function insertionSort(array) {
2-
return array;
3-
}
1+
const insertionSort = array => {
2+
for (var outer = 1; outer < array.length; outer++) {
3+
// take the comparableElement in the array
4+
for (var inner = 0; inner < outer; inner++) {
5+
// comparableElement will be compared with complete array and compare till comparableElement index
6+
if (array[outer] < array[inner]) {
7+
// comparableElement check with element if it is smaller than any element
8+
console.log(array);
9+
var [elem] = array.splice(outer, 1); // comparableElement will be deleted from the location and merge the array again.
10+
array.splice(inner, 0, elem); // comparableElement will insert before the just bigger element;
11+
}
12+
}
13+
}
14+
return array; // return sorted array
15+
};
416

517
const numbers = [8, 5, 6, 9, 3, 1, 4, 2, 7, 10];
6-
insertionSort(numbers);
18+
const sortedNumber = insertionSort(numbers);
19+
console.log(sortedNumber);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /