Skip to content

Commit a5efc8f

Browse files
committed
Insertion sort code comment and doc updated
1 parent f4d5536 commit a5efc8f

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

Sorting/Insertion Sort/README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
**Insertion sort** does exactly what you would expect: it inserts each element of the array into its proper position, leaving progressively larger stretches of the array sorted. What this means in practice is that the sort iterates down an array, and the part of the array already covered is in order; then, the current element of the array is inserted into the proper position at the head of the array, and the rest of the elements are moved down, using the space just vacated by the element inserted as the final space.
44

5+
56
![Insertion Sort](insertion_sort.jpg)
67

78

8-
#####A visualization on Insertion Sort
9+
#### A visualization on Insertion Sort
910
![Insertion sort demonstration](https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif)
1011

1112

12-
####Complexity Analysis
13-
- Worst Case - О(n<sup>2</sup>) comparisons, swaps
14-
- Average Case - О(n<sup>2</sup>) comparisons, swaps
15-
- Best Case - O(n) comparisons, O(1) swaps
13+
#### Complexity Analysis
14+
- Worst Case - О(n<sup>2</sup>)
15+
- Average Case - О(n<sup>2</sup>)
16+
- Best Case - O(n)
17+
1618
### More on this topic
1719
- https://en.wikipedia.org/wiki/Insertion_sort
1820
- http://quiz.geeksforgeeks.org/insertion-sort/

Sorting/Insertion Sort/insertion-sort.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
//Insertion sort implementation
2-
function insertionSort(arr){
3-
var len = arr.length;
4-
5-
for(var i=1; i<len; i++){
6-
currentValue = arr[i];
7-
j = i-1;
8-
9-
/* Move elements of arr[0..i-1], that are
10-
greater than current_value, to one position ahead
11-
of their current position */
12-
while (j >= 0 && arr[j] > currentValue)
13-
{
14-
arr[j+1] = arr[j];
15-
j--;
16-
}
17-
arr[j+1] = currentValue;
18-
}
19-
20-
return arr;
1+
/*Insertion sort implementation in JavaScript */
2+
function insertionSort(arr) {
3+
var len = arr.length;
4+
5+
for (var i = 1; i < len; i++) {
6+
currentValue = arr[i];
7+
j = i - 1;
8+
9+
/* Move elements of arr[0..i-1], that are
10+
greater than current_value, to one position ahead
11+
of their current position */
12+
while (j >= 0 && arr[j] > currentValue) {
13+
arr[j + 1] = arr[j];
14+
j--;
15+
}
16+
arr[j + 1] = currentValue;
17+
}
18+
19+
return arr;
2120
}
2221

2322
/******************* Testing Selection sort algorithm *********************/
@@ -27,13 +26,13 @@ function insertionSort(arr){
2726
* Using Math.round() will give you a non-uniform distribution!
2827
*/
2928
function getRandomInt(min, max) {
30-
return Math.floor(Math.random() * (max - min + 1)) + min;
29+
return Math.floor(Math.random() * (max - min + 1)) + min;
3130
}
3231

3332
var arr = [];
3433

35-
for(var i=0;i<10;i++){ //initialize a random integer unsorted array
36-
arr.push(getRandomInt(1, 100));
34+
for (var i = 0; i < 10; i++) { //initialize a random integer unsorted array
35+
arr.push(getRandomInt(1, 100));
3736
}
3837

3938
console.log("Unsorted array: ");

0 commit comments

Comments
 (0)