Skip to content

Commit 622ad9e

Browse files
committed
sorts/
1 parent 852563a commit 622ad9e

File tree

7 files changed

+112
-108
lines changed

7 files changed

+112
-108
lines changed

Sorts/TopologicalSort.js

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,59 @@
11

2-
function TopologicalSorter() {
3-
var graph = {},
4-
isVisitedNode,
5-
finishTimeCount,
6-
finishingTimeList,
7-
nextNode;
8-
9-
this.addOrder = function (nodeA, nodeB) {
10-
nodeA = String(nodeA);
11-
nodeB = String(nodeB);
12-
graph[nodeA] = graph[nodeA] || [];
13-
graph[nodeA].push(nodeB);
14-
}
15-
16-
this.sortAndGetOrderedItems = function () {
17-
isVisitedNode = Object.create(null);
18-
finishTimeCount = 0;
19-
finishingTimeList = [];
20-
21-
for (var node in graph) {
22-
if (graph.hasOwnProperty(node) && !isVisitedNode[node]) {
23-
dfsTraverse(node);
24-
}
25-
}
26-
27-
finishingTimeList.sort(function (item1, item2) {
28-
return item1.finishTime > item2.finishTime ? -1 : 1;
29-
});
30-
31-
return finishingTimeList.map(function (value) { return value.node })
2+
function TopologicalSorter () {
3+
var graph = {}
4+
var isVisitedNode
5+
var finishTimeCount
6+
var finishingTimeList
7+
var nextNode
8+
9+
this.addOrder = function (nodeA, nodeB) {
10+
nodeA = String(nodeA)
11+
nodeB = String(nodeB)
12+
graph[nodeA] = graph[nodeA] || []
13+
graph[nodeA].push(nodeB)
14+
}
15+
16+
this.sortAndGetOrderedItems = function () {
17+
isVisitedNode = Object.create(null)
18+
finishTimeCount = 0
19+
finishingTimeList = []
20+
21+
for (var node in graph) {
22+
if (Object.prototype.hasOwnProperty.call(graph, node) && !isVisitedNode[node]) {
23+
dfsTraverse(node)
24+
}
3225
}
3326

34-
function dfsTraverse(node) {
35-
isVisitedNode[node] = true;
36-
if (graph[node]) {
37-
for (var i = 0; i < graph[node].length; i++) {
38-
nextNode = graph[node][i];
39-
if (isVisitedNode[nextNode]) continue;
40-
dfsTraverse(nextNode);
41-
}
42-
}
43-
44-
finishingTimeList.push({
45-
node: node,
46-
finishTime: ++finishTimeCount
47-
});
27+
finishingTimeList.sort(function (item1, item2) {
28+
return item1.finishTime > item2.finishTime ? -1 : 1
29+
})
30+
31+
return finishingTimeList.map(function (value) { return value.node })
32+
}
33+
34+
function dfsTraverse (node) {
35+
isVisitedNode[node] = true
36+
if (graph[node]) {
37+
for (var i = 0; i < graph[node].length; i++) {
38+
nextNode = graph[node][i]
39+
if (isVisitedNode[nextNode]) continue
40+
dfsTraverse(nextNode)
41+
}
4842
}
49-
}
5043

44+
finishingTimeList.push({
45+
node: node,
46+
finishTime: ++finishTimeCount
47+
})
48+
}
49+
}
5150

5251
/* TEST */
53-
var topoSorter = new TopologicalSorter();
54-
topoSorter.addOrder(5, 2);
55-
topoSorter.addOrder(5, 0);
56-
topoSorter.addOrder(4, 0);
57-
topoSorter.addOrder(4, 1);
58-
topoSorter.addOrder(2, 3);
59-
topoSorter.addOrder(3, 1);
60-
console.log(topoSorter.sortAndGetOrderedItems());
52+
var topoSorter = new TopologicalSorter()
53+
topoSorter.addOrder(5, 2)
54+
topoSorter.addOrder(5, 0)
55+
topoSorter.addOrder(4, 0)
56+
topoSorter.addOrder(4, 1)
57+
topoSorter.addOrder(2, 3)
58+
topoSorter.addOrder(3, 1)
59+
console.log(topoSorter.sortAndGetOrderedItems())

Sorts/bubblesort.js

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,65 @@
1-
/*Bubble Sort is a algorithm to sort an array. It
1+
/* Bubble Sort is a algorithm to sort an array. It
22
* compares adjacent element and swaps thier position
3-
* The big O on bubble sort in worst and best case is O(N^2).
3+
* The big O on bubble sort in worst and best case is O(N^2).
44
* Not efficient.
55
*/
6-
function bubbleSort(items) {
7-
let length = items.length;
8-
for (let i = (length - 1); i > 0; i--) {
9-
//Number of passes
10-
for (let j = (length - i); j > 0; j--) {
11-
//Compare the adjacent positions
12-
if (items[j] < items[j - 1]) {
13-
//Swap the numbers
14-
let tmp = items[j];
15-
items[j] = items[j - 1];
16-
items[j - 1] = tmp;
17-
}
18-
}
6+
7+
function bubbleSort (items) {
8+
const length = items.length
9+
for (let i = (length - 1); i > 0; i--) {
10+
// Number of passes
11+
for (let j = (length - i); j > 0; j--) {
12+
// Compare the adjacent positions
13+
if (items[j] < items[j - 1]) {
14+
// Swap the numbers
15+
const tmp = items[j]
16+
items[j] = items[j - 1]
17+
items[j - 1] = tmp
18+
}
1919
}
20+
}
2021
}
2122

22-
//Implementation of bubbleSort
23+
// Implementation of bubbleSort
2324

24-
var ar=[5,6,7,8,1,2,12,14];
25-
//Array before Sort
26-
console.log("-----before sorting-----")
27-
console.log(ar);
28-
bubbleSort(ar);
29-
//Array after sort
30-
console.log("-----after sorting-----")
31-
console.log(ar);
25+
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
26+
// Array before Sort
27+
console.log('-----before sorting-----')
28+
console.log(ar)
29+
bubbleSort(ar)
30+
// Array after sort
31+
console.log('-----after sorting-----')
32+
console.log(ar)
3233

33-
/*alternative implementation of bubble sort algorithm.
34+
/* alternative implementation of bubble sort algorithm.
3435
Using a while loop instead. For educational purposses only
3536
*/
3637
/*
3738
*In bubble sort, we keep iterating while something was swapped in
38-
*the previous inner-loop iteration. By swapped I mean, in the
39-
*inner loop iteration, we check each number if the number proceeding
39+
*the previous inner-loop iteration. By swapped I mean, in the
40+
*inner loop iteration, we check each number if the number proceeding
4041
*it is greater than itself, if so we swap them.
4142
*/
4243

43-
function bubbleSort(arr){
44-
let swapped = true;
45-
while(swapped){
46-
swapped = false;
47-
for(let i = 0; i < arr.length-1; i++){
48-
if(arr[i] > arr[i + 1]){
49-
let temp = arr[i];
50-
arr[i] = arr[i + 1];
51-
arr[i + 1] = temp;
52-
swapped = true;
53-
}
54-
}
44+
function alternativeBubbleSort (arr) {
45+
let swapped = true
46+
while (swapped) {
47+
swapped = false
48+
for (let i = 0; i < arr.length - 1; i++) {
49+
if (arr[i] > arr[i + 1]) {
50+
const temp = arr[i]
51+
arr[i] = arr[i + 1]
52+
arr[i + 1] = temp
53+
swapped = true
54+
}
5555
}
56-
return arr;
56+
}
57+
return arr
5758
}
5859

59-
//test
60-
console.log("-----before sorting-----")
61-
var array = [10,5,3,8,2,6,4,7,9,1];
62-
console.log(array);
63-
console.log("-----after sorting-----")
64-
console.log(bubbleSort(array));
65-
66-
60+
// test
61+
console.log('-----before sorting-----')
62+
var array = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1]
63+
console.log(array)
64+
console.log('-----after sorting-----')
65+
console.log(alternativeBubbleSort(array))

Sorts/bucketSort.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ function bucketSort (list, size) {
5252
}
5353
return sorted
5454
}
55+
56+
// Testing
5557
const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14]
5658
// Array before Sort
5759
console.log(arrOrignal)
58-
arrSorted = bucketSort(arrOrignal)
60+
const arrSorted = bucketSort(arrOrignal)
5961
// Array after sort
6062
console.log(arrSorted)

Sorts/combSort.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ elements goes down (for each iteration of outer loop) in steps of
1414
a "shrink factor" k: [ n/k, n/k2, n/k3, ..., 1 ].
1515
1616
*/
17+
1718
function combSort (list) {
1819
if (list.length === 0) {
1920
return list
@@ -45,6 +46,6 @@ function combSort (list) {
4546
const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14]
4647
// Array before Sort
4748
console.log(arrOrignal)
48-
arrSorted = combSort(arrOrignal)
49+
const arrSorted = combSort(arrOrignal)
4950
// Array after sort
5051
console.log(arrSorted)

Sorts/cycleSort.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ number of writes to the original array, unlike any other in-place sorting
55
algorithm. It is based on the idea that the permutation to be sorted can
66
be factored into cycles, which can individually be rotated to give a sorted result.
77
*/
8+
89
function cycleSort (list) {
910
let writes = 0
1011
for (let cycleStart = 0; cycleStart < list.length; cycleStart++) {
@@ -18,11 +19,11 @@ function cycleSort (list) {
1819
}
1920
}
2021
// if its the same continue
21-
if (position == cycleStart) {
22+
if (position === cycleStart) {
2223
continue
2324
}
2425

25-
while (value == list[position]) {
26+
while (value === list[position]) {
2627
position++
2728
}
2829

@@ -32,14 +33,14 @@ function cycleSort (list) {
3233
writes++
3334

3435
// rotate the rest
35-
while (position != cycleStart) {
36+
while (position !== cycleStart) {
3637
position = cycleStart
3738
for (let i = cycleStart + 1; i < list.length; i++) {
3839
if (list[i] < value) {
3940
position++
4041
}
4142
}
42-
while (value == list[position]) {
43+
while (value === list[position]) {
4344
position++
4445
}
4546
const oldValueCycle = list[position]

Sorts/heapSort.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* key (the value) of node P is greater than the key of node C"
66
* Source: https://en.wikipedia.org/wiki/Heap_(data_structure)
77
*/
8+
89
Array.prototype.heapify = function (index, heapSize) {
910
let largest = index
1011
const leftIndex = 2 * index + 1

Sorts/selectionSort.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*In every iteration of selection sort, the minimum element (considering ascending order)
88
*from the unsorted subarray is picked and moved to the sorted subarray.
99
*/
10+
1011
function selectionSort (items) {
1112
var length = items.length
1213
for (var i = 0; i < length - 1; i++) {
@@ -17,7 +18,7 @@ function selectionSort (items) {
1718
min = j // Change the current min number position if a smaller num is found
1819
}
1920
}
20-
if (min != i) {
21+
if (min !== i) {
2122
// After each pass, if the current min num != initial min num, exchange the position.
2223
// Swap the numbers
2324
var tmp = items[i]

0 commit comments

Comments
 (0)