data-footstone
Advanced tools
Comparing version 0.1.2 to 0.1.3
'use strict'; | ||
var order = require('./order.js'); | ||
var stack = require('./stack.js'); | ||
@@ -13,2 +14,3 @@ var queue = require('./queue.js'); | ||
exports.order = order; | ||
exports.Stack = stack.Stack; | ||
@@ -15,0 +17,0 @@ exports.PriorityQueue = queue.PriorityQueue; |
@@ -0,1 +1,3 @@ | ||
import * as order from './order.js'; | ||
export { order }; | ||
export { Stack } from './stack.js'; | ||
@@ -2,0 +4,0 @@ export { PriorityQueue, Queue } from './queue.js'; |
{ | ||
"name": "data-footstone", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "data structure", | ||
@@ -75,3 +75,3 @@ "author": "feigebaobei <18515195415@163.com>", | ||
}, | ||
"gitHead": "7a10032ce95bb2bd5ced12923ecd9dbae9467423" | ||
"gitHead": "4fac9843e0455fc3ac6a8683d20b0f2de94dfd53" | ||
} |
@@ -1,2 +0,2 @@ | ||
// import * as order from './order' | ||
import * as order from './order'; | ||
import { Stack } from './stack'; | ||
@@ -9,4 +9,2 @@ import { Queue, PriorityQueue } from './queue'; | ||
import { BaseTree, BinarySearchTree, AVLTree, RedBackTree } from './tree'; | ||
export { | ||
// ...order, | ||
Stack, Queue, PriorityQueue, SingleChain, DoublyChain, SingleCircleChain, DoublyCircleChain, PSet, PMap, HashMap, BaseTree, BinarySearchTree, AVLTree, RedBackTree, }; | ||
export { order, Stack, Queue, PriorityQueue, SingleChain, DoublyChain, SingleCircleChain, DoublyCircleChain, PSet, PMap, HashMap, BaseTree, BinarySearchTree, AVLTree, RedBackTree, }; |
@@ -23,30 +23,52 @@ // n^2 | ||
}; | ||
let selectSort = (arr) => { | ||
let selectSort = (arr, order = 'asc') => { | ||
for (let i = 0; i < arr.length - 1; i++) { | ||
let minIndex = i; | ||
let index = i; | ||
for (let j = i + 1; j < arr.length; j++) { | ||
if (arr[j] < arr[minIndex]) { | ||
minIndex = j; | ||
switch (order) { | ||
case 'asc': | ||
if (arr[j] < arr[index]) { | ||
index = j; | ||
} | ||
break; | ||
case 'des': | ||
if (arr[j] > arr[index]) { | ||
index = j; | ||
} | ||
break; | ||
} | ||
} | ||
if (minIndex !== i) { | ||
if (index !== i) { | ||
; | ||
[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]]; | ||
[arr[i], arr[index]] = [arr[index], arr[i]]; | ||
} | ||
} | ||
}; | ||
let merge = (leftArr, rightArr) => { | ||
let merge = (leftArr, rightArr, order) => { | ||
let tempArr = []; | ||
let res = []; | ||
while (leftArr.length && rightArr.length) { | ||
if (leftArr[0] < rightArr[0]) { | ||
tempArr.push(leftArr.shift()); | ||
switch (order) { | ||
case 'asc': | ||
if (leftArr[0] < rightArr[0]) { | ||
tempArr.push(leftArr.shift()); | ||
} | ||
else { | ||
tempArr.push(rightArr.shift()); | ||
} | ||
break; | ||
case 'des': | ||
if (leftArr[0] < rightArr[0]) { | ||
tempArr.push(rightArr.shift()); | ||
} | ||
else { | ||
tempArr.push(leftArr.shift()); | ||
} | ||
break; | ||
} | ||
else { | ||
tempArr.push(rightArr.shift()); | ||
} | ||
} | ||
return [...tempArr, ...leftArr, ...rightArr]; | ||
return res; | ||
}; | ||
// nlogn | ||
let mergeSort = (arr) => { | ||
let mergeSort = (arr, order = 'asc') => { | ||
if (arr.length < 2) { | ||
@@ -58,14 +80,25 @@ return arr; | ||
let right = arr.slice(m); | ||
return merge(mergeSort(left), mergeSort(right)); | ||
return merge(mergeSort(left), mergeSort(right), order); | ||
}; | ||
// n^2 | ||
let insertSort = (arr) => { | ||
let insertSort = (arr, order = 'asc') => { | ||
for (let i = 1; i < arr.length; i++) { | ||
let cur = arr[i]; | ||
let lastIndex = i - 1; | ||
while (lastIndex >= 0 && arr[lastIndex] > cur) { | ||
arr[lastIndex + 1] = arr[lastIndex]; | ||
lastIndex--; | ||
switch (order) { | ||
case 'asc': | ||
while (lastIndex >= 0 && arr[lastIndex] > cur) { | ||
arr[lastIndex + 1] = arr[lastIndex]; | ||
lastIndex--; | ||
} | ||
arr[lastIndex + 1] = cur; | ||
break; | ||
case 'des': | ||
while (lastIndex >= 0 && arr[lastIndex] < cur) { | ||
arr[lastIndex + 1] = arr[lastIndex]; | ||
lastIndex--; | ||
} | ||
arr[lastIndex + 1] = cur; | ||
break; | ||
} | ||
arr[lastIndex + 1] = cur; | ||
return arr; | ||
@@ -75,3 +108,3 @@ } | ||
// nlogn | ||
let quickSort = (arr) => { | ||
let quickSort = (arr, order = 'asc') => { | ||
if (arr.length < 1) { | ||
@@ -84,10 +117,22 @@ return arr; | ||
arr.forEach((item) => { | ||
if (item > p) { | ||
right.push(item); | ||
switch (order) { | ||
case 'asc': | ||
if (item > p) { | ||
right.push(item); | ||
} | ||
else { | ||
left.push(item); | ||
} | ||
break; | ||
case 'des': | ||
if (item < p) { | ||
right.push(item); | ||
} | ||
else { | ||
left.push(item); | ||
} | ||
break; | ||
} | ||
else { | ||
left.push(item); | ||
} | ||
}); | ||
return [...quickSort(left), ...quickSort(right)]; | ||
return [...quickSort(left, order), ...quickSort(right, order)]; | ||
}; | ||
@@ -94,0 +139,0 @@ let heapSort = () => { }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
274420
61
4300