mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
sorts/
This commit is contained in:
@ -1,60 +1,59 @@
|
|||||||
|
|
||||||
function TopologicalSorter() {
|
function TopologicalSorter () {
|
||||||
var graph = {},
|
var graph = {}
|
||||||
isVisitedNode,
|
var isVisitedNode
|
||||||
finishTimeCount,
|
var finishTimeCount
|
||||||
finishingTimeList,
|
var finishingTimeList
|
||||||
nextNode;
|
var nextNode
|
||||||
|
|
||||||
this.addOrder = function (nodeA, nodeB) {
|
this.addOrder = function (nodeA, nodeB) {
|
||||||
nodeA = String(nodeA);
|
nodeA = String(nodeA)
|
||||||
nodeB = String(nodeB);
|
nodeB = String(nodeB)
|
||||||
graph[nodeA] = graph[nodeA] || [];
|
graph[nodeA] = graph[nodeA] || []
|
||||||
graph[nodeA].push(nodeB);
|
graph[nodeA].push(nodeB)
|
||||||
}
|
}
|
||||||
|
|
||||||
this.sortAndGetOrderedItems = function () {
|
|
||||||
isVisitedNode = Object.create(null);
|
|
||||||
finishTimeCount = 0;
|
|
||||||
finishingTimeList = [];
|
|
||||||
|
|
||||||
for (var node in graph) {
|
this.sortAndGetOrderedItems = function () {
|
||||||
if (graph.hasOwnProperty(node) && !isVisitedNode[node]) {
|
isVisitedNode = Object.create(null)
|
||||||
dfsTraverse(node);
|
finishTimeCount = 0
|
||||||
}
|
finishingTimeList = []
|
||||||
}
|
|
||||||
|
|
||||||
finishingTimeList.sort(function (item1, item2) {
|
for (var node in graph) {
|
||||||
return item1.finishTime > item2.finishTime ? -1 : 1;
|
if (Object.prototype.hasOwnProperty.call(graph, node) && !isVisitedNode[node]) {
|
||||||
});
|
dfsTraverse(node)
|
||||||
|
}
|
||||||
return finishingTimeList.map(function (value) { return value.node })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function dfsTraverse(node) {
|
finishingTimeList.sort(function (item1, item2) {
|
||||||
isVisitedNode[node] = true;
|
return item1.finishTime > item2.finishTime ? -1 : 1
|
||||||
if (graph[node]) {
|
})
|
||||||
for (var i = 0; i < graph[node].length; i++) {
|
|
||||||
nextNode = graph[node][i];
|
|
||||||
if (isVisitedNode[nextNode]) continue;
|
|
||||||
dfsTraverse(nextNode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
finishingTimeList.push({
|
return finishingTimeList.map(function (value) { return value.node })
|
||||||
node: node,
|
}
|
||||||
finishTime: ++finishTimeCount
|
|
||||||
});
|
function dfsTraverse (node) {
|
||||||
|
isVisitedNode[node] = true
|
||||||
|
if (graph[node]) {
|
||||||
|
for (var i = 0; i < graph[node].length; i++) {
|
||||||
|
nextNode = graph[node][i]
|
||||||
|
if (isVisitedNode[nextNode]) continue
|
||||||
|
dfsTraverse(nextNode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
finishingTimeList.push({
|
||||||
|
node: node,
|
||||||
|
finishTime: ++finishTimeCount
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* TEST */
|
/* TEST */
|
||||||
var topoSorter = new TopologicalSorter();
|
var topoSorter = new TopologicalSorter()
|
||||||
topoSorter.addOrder(5, 2);
|
topoSorter.addOrder(5, 2)
|
||||||
topoSorter.addOrder(5, 0);
|
topoSorter.addOrder(5, 0)
|
||||||
topoSorter.addOrder(4, 0);
|
topoSorter.addOrder(4, 0)
|
||||||
topoSorter.addOrder(4, 1);
|
topoSorter.addOrder(4, 1)
|
||||||
topoSorter.addOrder(2, 3);
|
topoSorter.addOrder(2, 3)
|
||||||
topoSorter.addOrder(3, 1);
|
topoSorter.addOrder(3, 1)
|
||||||
console.log(topoSorter.sortAndGetOrderedItems());
|
console.log(topoSorter.sortAndGetOrderedItems())
|
||||||
|
@ -1,66 +1,65 @@
|
|||||||
/*Bubble Sort is a algorithm to sort an array. It
|
/* Bubble Sort is a algorithm to sort an array. It
|
||||||
* compares adjacent element and swaps thier position
|
* compares adjacent element and swaps thier position
|
||||||
* The big O on bubble sort in worst and best case is O(N^2).
|
* The big O on bubble sort in worst and best case is O(N^2).
|
||||||
* Not efficient.
|
* Not efficient.
|
||||||
*/
|
*/
|
||||||
function bubbleSort(items) {
|
|
||||||
let length = items.length;
|
function bubbleSort (items) {
|
||||||
for (let i = (length - 1); i > 0; i--) {
|
const length = items.length
|
||||||
//Number of passes
|
for (let i = (length - 1); i > 0; i--) {
|
||||||
for (let j = (length - i); j > 0; j--) {
|
// Number of passes
|
||||||
//Compare the adjacent positions
|
for (let j = (length - i); j > 0; j--) {
|
||||||
if (items[j] < items[j - 1]) {
|
// Compare the adjacent positions
|
||||||
//Swap the numbers
|
if (items[j] < items[j - 1]) {
|
||||||
let tmp = items[j];
|
// Swap the numbers
|
||||||
items[j] = items[j - 1];
|
const tmp = items[j]
|
||||||
items[j - 1] = tmp;
|
items[j] = items[j - 1]
|
||||||
}
|
items[j - 1] = tmp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Implementation of bubbleSort
|
// Implementation of bubbleSort
|
||||||
|
|
||||||
var ar=[5,6,7,8,1,2,12,14];
|
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
|
||||||
//Array before Sort
|
// Array before Sort
|
||||||
console.log("-----before sorting-----")
|
console.log('-----before sorting-----')
|
||||||
console.log(ar);
|
console.log(ar)
|
||||||
bubbleSort(ar);
|
bubbleSort(ar)
|
||||||
//Array after sort
|
// Array after sort
|
||||||
console.log("-----after sorting-----")
|
console.log('-----after sorting-----')
|
||||||
console.log(ar);
|
console.log(ar)
|
||||||
|
|
||||||
/*alternative implementation of bubble sort algorithm.
|
/* alternative implementation of bubble sort algorithm.
|
||||||
Using a while loop instead. For educational purposses only
|
Using a while loop instead. For educational purposses only
|
||||||
*/
|
*/
|
||||||
/*
|
/*
|
||||||
*In bubble sort, we keep iterating while something was swapped in
|
*In bubble sort, we keep iterating while something was swapped in
|
||||||
*the previous inner-loop iteration. By swapped I mean, in the
|
*the previous inner-loop iteration. By swapped I mean, in the
|
||||||
*inner loop iteration, we check each number if the number proceeding
|
*inner loop iteration, we check each number if the number proceeding
|
||||||
*it is greater than itself, if so we swap them.
|
*it is greater than itself, if so we swap them.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function bubbleSort(arr){
|
function alternativeBubbleSort (arr) {
|
||||||
let swapped = true;
|
let swapped = true
|
||||||
while(swapped){
|
while (swapped) {
|
||||||
swapped = false;
|
swapped = false
|
||||||
for(let i = 0; i < arr.length-1; i++){
|
for (let i = 0; i < arr.length - 1; i++) {
|
||||||
if(arr[i] > arr[i + 1]){
|
if (arr[i] > arr[i + 1]) {
|
||||||
let temp = arr[i];
|
const temp = arr[i]
|
||||||
arr[i] = arr[i + 1];
|
arr[i] = arr[i + 1]
|
||||||
arr[i + 1] = temp;
|
arr[i + 1] = temp
|
||||||
swapped = true;
|
swapped = true
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return arr;
|
}
|
||||||
|
return arr
|
||||||
}
|
}
|
||||||
|
|
||||||
//test
|
// test
|
||||||
console.log("-----before sorting-----")
|
console.log('-----before sorting-----')
|
||||||
var array = [10,5,3,8,2,6,4,7,9,1];
|
var array = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1]
|
||||||
console.log(array);
|
console.log(array)
|
||||||
console.log("-----after sorting-----")
|
console.log('-----after sorting-----')
|
||||||
console.log(bubbleSort(array));
|
console.log(alternativeBubbleSort(array))
|
||||||
|
|
||||||
|
|
||||||
|
@ -52,9 +52,11 @@ function bucketSort (list, size) {
|
|||||||
}
|
}
|
||||||
return sorted
|
return sorted
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Testing
|
||||||
const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14]
|
const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14]
|
||||||
// Array before Sort
|
// Array before Sort
|
||||||
console.log(arrOrignal)
|
console.log(arrOrignal)
|
||||||
arrSorted = bucketSort(arrOrignal)
|
const arrSorted = bucketSort(arrOrignal)
|
||||||
// Array after sort
|
// Array after sort
|
||||||
console.log(arrSorted)
|
console.log(arrSorted)
|
||||||
|
@ -14,6 +14,7 @@ elements goes down (for each iteration of outer loop) in steps of
|
|||||||
a "shrink factor" k: [ n/k, n/k2, n/k3, ..., 1 ].
|
a "shrink factor" k: [ n/k, n/k2, n/k3, ..., 1 ].
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function combSort (list) {
|
function combSort (list) {
|
||||||
if (list.length === 0) {
|
if (list.length === 0) {
|
||||||
return list
|
return list
|
||||||
@ -45,6 +46,6 @@ function combSort (list) {
|
|||||||
const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14]
|
const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14]
|
||||||
// Array before Sort
|
// Array before Sort
|
||||||
console.log(arrOrignal)
|
console.log(arrOrignal)
|
||||||
arrSorted = combSort(arrOrignal)
|
const arrSorted = combSort(arrOrignal)
|
||||||
// Array after sort
|
// Array after sort
|
||||||
console.log(arrSorted)
|
console.log(arrSorted)
|
||||||
|
@ -5,6 +5,7 @@ number of writes to the original array, unlike any other in-place sorting
|
|||||||
algorithm. It is based on the idea that the permutation to be sorted can
|
algorithm. It is based on the idea that the permutation to be sorted can
|
||||||
be factored into cycles, which can individually be rotated to give a sorted result.
|
be factored into cycles, which can individually be rotated to give a sorted result.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function cycleSort (list) {
|
function cycleSort (list) {
|
||||||
let writes = 0
|
let writes = 0
|
||||||
for (let cycleStart = 0; cycleStart < list.length; cycleStart++) {
|
for (let cycleStart = 0; cycleStart < list.length; cycleStart++) {
|
||||||
@ -18,11 +19,11 @@ function cycleSort (list) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// if its the same continue
|
// if its the same continue
|
||||||
if (position == cycleStart) {
|
if (position === cycleStart) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
while (value == list[position]) {
|
while (value === list[position]) {
|
||||||
position++
|
position++
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,14 +33,14 @@ function cycleSort (list) {
|
|||||||
writes++
|
writes++
|
||||||
|
|
||||||
// rotate the rest
|
// rotate the rest
|
||||||
while (position != cycleStart) {
|
while (position !== cycleStart) {
|
||||||
position = cycleStart
|
position = cycleStart
|
||||||
for (let i = cycleStart + 1; i < list.length; i++) {
|
for (let i = cycleStart + 1; i < list.length; i++) {
|
||||||
if (list[i] < value) {
|
if (list[i] < value) {
|
||||||
position++
|
position++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while (value == list[position]) {
|
while (value === list[position]) {
|
||||||
position++
|
position++
|
||||||
}
|
}
|
||||||
const oldValueCycle = list[position]
|
const oldValueCycle = list[position]
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
* key (the value) of node P is greater than the key of node C"
|
* key (the value) of node P is greater than the key of node C"
|
||||||
* Source: https://en.wikipedia.org/wiki/Heap_(data_structure)
|
* Source: https://en.wikipedia.org/wiki/Heap_(data_structure)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Array.prototype.heapify = function (index, heapSize) {
|
Array.prototype.heapify = function (index, heapSize) {
|
||||||
let largest = index
|
let largest = index
|
||||||
const leftIndex = 2 * index + 1
|
const leftIndex = 2 * index + 1
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
*In every iteration of selection sort, the minimum element (considering ascending order)
|
*In every iteration of selection sort, the minimum element (considering ascending order)
|
||||||
*from the unsorted subarray is picked and moved to the sorted subarray.
|
*from the unsorted subarray is picked and moved to the sorted subarray.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function selectionSort (items) {
|
function selectionSort (items) {
|
||||||
var length = items.length
|
var length = items.length
|
||||||
for (var i = 0; i < length - 1; i++) {
|
for (var i = 0; i < length - 1; i++) {
|
||||||
@ -17,7 +18,7 @@ function selectionSort (items) {
|
|||||||
min = j // Change the current min number position if a smaller num is found
|
min = j // Change the current min number position if a smaller num is found
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (min != i) {
|
if (min !== i) {
|
||||||
// After each pass, if the current min num != initial min num, exchange the position.
|
// After each pass, if the current min num != initial min num, exchange the position.
|
||||||
// Swap the numbers
|
// Swap the numbers
|
||||||
var tmp = items[i]
|
var tmp = items[i]
|
||||||
|
Reference in New Issue
Block a user