Movatterモバイル変換


[0]ホーム

URL:


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

Commit9acbd4b

Browse files
committed
better performance
1 parentc67316e commit9acbd4b

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

‎book/chapters/insertion-sort.adoc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
Insertion sort is one of the most natural way of sorting. If you are given some cards that's probably how you are going to sort the.
44

5-
.This is how it works:
6-
. Start with the first element, assume that everything to its left is sorted and everything to the right is not.
7-
. Move to the next element.
5+
Insertion sorts visit elements one by one. Everything to the left of the current element is curretly sorted and everything to the right is not.
86

9-
. Take the first element and consideritsorted.
10-
.Takethe2nd element, compare it with thefirst element. It 2nd is smaller swapitwith the first element.
11-
.Go to the 3rdelementcompare it withthealready sorted elements and if any of themissmaller, swap them.
7+
.This is howitworks:
8+
.Visit 2nd. Ifthecurrent element is smaller than theprevious one move to the placeitshould be.
9+
.Visit nextelementand dothesame thing. The leftisalways sorted.
1210
. Repeat for the rest of the array.
1311

1412
More efficient in practice than most other simple quadratic (i.e., O(n^2^)) algorithms such as <<Selection Sort>> or <<Bubble Sort>>.

‎src/algorithms/sorting/insertion-sort.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
const{ swap}=require('./sorting-common');
2-
31
functioninsertionSort(collection){
42
constarray=Array.from(collection);
53

6-
for(letouter=1;outer<array.length;outer+=1){
4+
for(letouter=0;outer<array.length;outer+=1){
5+
constinsert=array[outer];
76
letinner=outer-1;
8-
while(inner>=0&&array[outer]<array[inner]){
7+
while(inner>=0&&array[inner]>insert){
8+
array[inner+1]=array[inner];
99
inner-=1;
1010
}
11-
if(outer!==inner+1){
12-
const[insert]=array.splice(outer,1);
13-
array.splice(inner+1,0,insert);
14-
}
11+
array[inner+1]=insert;
1512
}
1613
returnarray;
1714
}

‎src/algorithms/sorting/sorting-common.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// tag::swap[]
22
/**
33
* Swap array elements in place
4+
* Runtime: O(1)
45
*@param {array} array to be modified
56
*@param {integer} from index of the first element
67
*@param {integer} to index of the 2nd element
@@ -10,6 +11,20 @@ function swap(array, from, to) {
1011
}
1112
// end::swap[]
1213

14+
/**
15+
* Move an element in an array *from* a postion *to* another.
16+
* Runtime: O(n)
17+
*@param {array} array
18+
*@param {integer} from index of the element to remove (source)
19+
*@param {integer} to index where the removed element would be move (destination)
20+
*/
21+
functionmoveElement(array,from,to){
22+
if(from===to+1)return;
23+
const[elementToInsert]=array.splice(from,1);// delete from position
24+
array.splice(to+1,0,elementToInsert);// insert element in to the position.
25+
}
26+
1327
module.exports={
1428
swap,
29+
moveElement,
1530
};

‎src/algorithms/sorting/sorting.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ const sortingAlgorithms = [
55

66
sortingAlgorithms.forEach((sort)=>{
77
describe(`Sorting with${sort.name}`,()=>{
8+
it('should work with zero numbers',()=>{
9+
expect(sort([])).toEqual([]);
10+
});
11+
12+
it('should work with one number',()=>{
13+
expect(sort([3])).toEqual([3]);
14+
});
15+
816
it('should sort numbers',()=>{
917
expect(sort([3,5,0])).toEqual([0,3,5]);
1018
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp