Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
921adba
commit 9260d39
Showing
11 changed files
with
115 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
import { S1 } from "./S1.js"; | ||
import { S2 } from "./S2.js"; | ||
import { selectionSort } from "./selectionSort.js"; | ||
|
||
/** | ||
* Sorting Algorithms | ||
* | ||
* @see {@link https://pt.wikipedia.org/wiki/Algoritmo_de_ordena%C3%A7%C3%A3o} | ||
* @see {@link https://www.geeksforgeeks.org/sorting-algorithms/} | ||
* | ||
* @namespace SortingAlgorithms | ||
*/ | ||
export { | ||
S1, | ||
S2, | ||
selectionSort, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Sort given array using the Selection Sort algorithm | ||
* @see {@link https://www.geeksforgeeks.org/selection-sort/} | ||
* | ||
* @param {number[]} arr - The array to sort | ||
* @returns {number[]} the given array, sorted using the Selection Sort algorithm | ||
* @function selectionSort | ||
* @memberof SortingAlgorithms | ||
*/ | ||
export function selectionSort(arr) { | ||
for (let i = 0; i < arr.length; i++) { | ||
let low = arr[i]; | ||
let index = i; | ||
|
||
for (let j = i + 1; j < arr.length; j++) { | ||
if (arr[j] < low) { | ||
low = arr[j]; | ||
index = j; | ||
} | ||
} | ||
|
||
if (low !== arr[i]) { | ||
arr[index] = arr[i]; | ||
arr[i] = low; | ||
} | ||
} | ||
|
||
return arr; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Check if all Elements of the array are equal | ||
* | ||
* @example | ||
* arrayAllEqual([0,0,0,0]) // true | ||
* arrayAllEqual([0,0,0,1]) // false | ||
* arrayAllEqual([0,"a","a"]) // false | ||
* arrayAllEqual([[1,1],[1,1]]) // true | ||
* arrayAllEqual([[1,1],[1,0]]) // false | ||
* arrayAllEqual([{a:"b",c:1},{a:"b"}]) // false | ||
* arrayAllEqual([{a:"b",c:1},{a:"b",c:1}]) // true | ||
* arrayAllEqual([{a:"b",c:1},{a:"b",c:1},{a:"c",c:1}]) // false | ||
* arrayAllEqual([{a:"b",c:1},{a:"b",c:1},{a:"b",c:1}]) // true | ||
* | ||
* @param {any} arr - the array to check all elements | ||
* @returns {boolean} true if all elements of the array ara equal | ||
*/ | ||
export function arrayAllEqual(arr) { return arr.every(v => JSON.stringify(v) === JSON.stringify(arr[0])); }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Check if the given array is sorted fom lowest to highest | ||
* | ||
* @example | ||
* arrayIsSorted([]) // true | ||
* arrayIsSorted([0,0,0,0]) // true | ||
* arrayIsSorted([2,1,4]) // false | ||
* arrayIsSorted([3,2,1]) // false | ||
* arrayIsSorted([1,2,3]) // true | ||
* arrayIsSorted(["B","A","D"]) // false | ||
* arrayIsSorted(["A","B","C"]) // true | ||
* | ||
* @param {any} arr - the array to check | ||
* @returns {boolean} true if the array is sorted | ||
*/ | ||
export function arrayIsSorted(arr = []) { | ||
for (let i = 0; i < arr.length - 1; i++) { | ||
if (arr[i] > arr[i + 1]) return false; | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* This function creates a new sorted array using the Selection Sort algorithm | ||
* Don't change the original array. | ||
* @see {@link https://www.geeksforgeeks.org/selection-sort/} | ||
* | ||
* @example | ||
* arrayIsSorted([3, 5, 1, 6, 10, 7, 2, 6]) // [1,2,3,5,6,6,7,10] | ||
* arrayIsSorted([0,0,0,0]) // [0,0,0,0] | ||
* arrayIsSorted([0,1,2,3]) // [0,1,2,3] | ||
* | ||
* @param {number[]} arr - The array to sort | ||
* @returns {number[]} a new array with sorted elements | ||
*/ | ||
export function arrayNewSort(arr) { | ||
const copyArray = [...arr]; | ||
const sortedArr = []; | ||
|
||
while (copyArray.length) { | ||
let low = Infinity; | ||
let index = 0; | ||
|
||
for (let i = 0; i < copyArray.length; i++) { | ||
if (copyArray[i] < low) { | ||
low = copyArray[i]; | ||
index = i; | ||
} | ||
} | ||
|
||
sortedArr.push(low); | ||
copyArray.splice(index, 1); | ||
} | ||
|
||
return sortedArr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters