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 ca93912

Browse files
insertion_sort
1 parent fab2f15 commit ca93912

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+
function Node(element) {
2+
this.element = element;
3+
this.next = null;
4+
this.previous = null;
5+
}
6+
7+
function LList() {
8+
this.head = new Node("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+
function dispReverse() {
18+
var currNode = this.head;
19+
currNode = this.findLast();
20+
while (!(currNode.previous == null)) {
21+
console.log(currNode.element);
22+
currNode = currNode.previous;
23+
}
24+
}
25+
26+
function findLast() {
27+
var currNode = this.head;
28+
while (!(currNode.next == null)) {
29+
currNode = currNode.next;
30+
}
31+
return currNode;
32+
}
33+
34+
function remove(item) {
35+
var currNode = 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+
function display() {
55+
var currNode = this.head;
56+
while (!(currNode.next == null)) {
57+
console.log(currNode.next.element);
58+
currNode = currNode.next;
59+
}
60+
}
61+
62+
function find(item) {
63+
var currNode = this.head;
64+
while (currNode.element != item) {
65+
currNode = currNode.next;
66+
}
67+
return currNode;
68+
}
69+
70+
function insert(newElement, item) {
71+
var newNode = new Node(newElement);
72+
var current = this.find(item);
73+
newNode.next = current.next;
74+
newNode.previous = current;
75+
current.next = newNode;
76+
}
77+
78+
79+
var cities = new LList();
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+
function insertion_sort(array) {
4+
if(!Array.isArray(array) || array.length < 2) {
5+
return array;
6+
};
7+
8+
// function to swap elements of the array
9+
var swap = function(array, first, second) {
10+
var temp = array[first];
11+
array[first] = array[second];
12+
array[second] = temp;
13+
return array;
14+
}
15+
16+
// write the compare function if not given
17+
18+
if (typeof compare !== 'function') {
19+
compare = function (a, b) {
20+
return a > b ? 1 : -1;
21+
};
22+
}
23+
24+
var i, 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+
return array;
42+
}
43+
44+
const list = [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)

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