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

Commitca93912

Browse files
committed
insertion_sort
1 parentfab2f15 commitca93912

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

‎Linked-List/generic-Linked-list.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
functionNode(element){
2+
this.element=element;
3+
this.next=null;
4+
this.previous=null;
5+
}
6+
7+
functionLList(){
8+
this.head=newNode("head");
9+
this.find=find;
10+
this.insert=insert;
11+
this.display=display;
12+
this.remove=remove;
13+
this.findLast=findLast;
14+
this.dispReverse=dispReverse;
15+
}
16+
17+
functiondispReverse(){
18+
varcurrNode=this.head;
19+
currNode=this.findLast();
20+
while(!(currNode.previous==null)){
21+
console.log(currNode.element);
22+
currNode=currNode.previous;
23+
}
24+
}
25+
26+
functionfindLast(){
27+
varcurrNode=this.head;
28+
while(!(currNode.next==null)){
29+
currNode=currNode.next;
30+
}
31+
returncurrNode;
32+
}
33+
34+
functionremove(item){
35+
varcurrNode=this.find(item);
36+
if(!(currNode.next==null)){
37+
currNode.previous.next=currNode.next;
38+
currNode.next.previous=currNode.previous;
39+
currNode.next=null;
40+
currNode.previous=null;
41+
}
42+
}
43+
44+
// findPrevious is no longer needed
45+
/*function findPrevious(item) {
46+
var currNode = this.head;
47+
while (!(currNode.next == null) &&
48+
(currNode.next.element != item)) {
49+
currNode = currNode.next;
50+
}
51+
return currNode;
52+
}*/
53+
54+
functiondisplay(){
55+
varcurrNode=this.head;
56+
while(!(currNode.next==null)){
57+
console.log(currNode.next.element);
58+
currNode=currNode.next;
59+
}
60+
}
61+
62+
functionfind(item){
63+
varcurrNode=this.head;
64+
while(currNode.element!=item){
65+
currNode=currNode.next;
66+
}
67+
returncurrNode;
68+
}
69+
70+
functioninsert(newElement,item){
71+
varnewNode=newNode(newElement);
72+
varcurrent=this.find(item);
73+
newNode.next=current.next;
74+
newNode.previous=current;
75+
current.next=newNode;
76+
}
77+
78+
79+
varcities=newLList();
80+
cities.insert("Conway","head");
81+
cities.insert("Russellville","Conway");
82+
cities.insert("Carlisle","Russellville");
83+
cities.insert("Alma","Carlisle");
84+
cities.display();
85+
console.log();
86+
cities.remove("Carlisle");
87+
cities.display();
88+
console.log();
89+
cities.dispReverse();

‎Sorting/insertion_sort.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Implement the [insertion sort algorithm](http://en.wikipedia.org/wiki/Insertion_sort). */
2+
3+
functioninsertion_sort(array){
4+
if(!Array.isArray(array)||array.length<2){
5+
returnarray;
6+
};
7+
8+
// function to swap elements of the array
9+
varswap=function(array,first,second){
10+
vartemp=array[first];
11+
array[first]=array[second];
12+
array[second]=temp;
13+
returnarray;
14+
}
15+
16+
// write the compare function if not given
17+
18+
if(typeofcompare!=='function'){
19+
compare=function(a,b){
20+
returna>b ?1 :-1;
21+
};
22+
}
23+
24+
vari,j;
25+
26+
/*
27+
1. Assume first element is sorted
28+
2. Add first unsorted element to sorted array
29+
3. Compare new element in sorted array with previous elements to determine correct destination index in sorted array
30+
4. In the below, at the last step, I had to decrement j by one, to stop the while loop. Because, with the decremented value of j, in the next iteration of the loop, one of the while loop condition will fail
31+
*/
32+
33+
for(i=1;i<array.length;i++){
34+
j=i;
35+
// So, now for each value of i, compare this index position with the previous index position.
36+
while((j-1)>=0&&compare(array[j],array[j-1])<0){
37+
swap(array,j,j-1);
38+
j--
39+
}
40+
}
41+
returnarray;
42+
}
43+
44+
constlist=[54,26,93,17,77,31,44,55,20]
45+
console.log(insertion_sort(list));
46+
// [ 17, 20, 26, 31, 44, 54, 55, 77, 93 ]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp