Skip to content

Commit

Permalink
add some array function
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed Oct 10, 2022
1 parent 921adba commit 9260d39
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 83 deletions.
1 change: 0 additions & 1 deletion src/Games/rockPaperScissor.js
Expand Up @@ -23,7 +23,6 @@
* @param {string} player2 - the player 2 option
* @returns {number}
*/

export function rockPaperScissor(player1, player2) {
if (player1 === player2) return 0;
else if (
Expand Down
26 changes: 0 additions & 26 deletions src/SortingAlgorithms/S1.js

This file was deleted.

31 changes: 0 additions & 31 deletions src/SortingAlgorithms/S2.js

This file was deleted.

7 changes: 3 additions & 4 deletions src/SortingAlgorithms/index.js
@@ -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,
};
29 changes: 29 additions & 0 deletions src/SortingAlgorithms/selectionSort.js
@@ -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;
}
18 changes: 0 additions & 18 deletions src/allEqual.js

This file was deleted.

18 changes: 18 additions & 0 deletions src/arrayAllEqual.js
@@ -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])); };
21 changes: 21 additions & 0 deletions src/arrayIsSorted.js
@@ -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;
}
34 changes: 34 additions & 0 deletions src/arrayNewSort.js
@@ -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;
}
2 changes: 1 addition & 1 deletion src/getVersion.js
Expand Up @@ -6,7 +6,7 @@ import pck from "../package.json";
* Returns the current version of the library
*
* @example
* getVersion(); // "1.2.4
* getVersion(); // "1.2.10"
*
* @returns {String}
*/
Expand Down
11 changes: 9 additions & 2 deletions src/index.js
@@ -1,13 +1,16 @@
import * as DOM from "./DOM/index.js";
import * as Games from "./Games/index.js";
import * as SortingAlgorithms from "./SortingAlgorithms/index.js";

import { allCharactersSame } from "./allCharactersSame.js";
import { allEqual } from "./allEqual.js";
import { and } from "./and.js";
import { arrayAllEqual } from "./arrayAllEqual.js";
import { arrayChoice } from "./arrayChoice.js";
import { arrayFindBigObject } from "./arrayFindBigObject.js";
import { arrayFindLowObject } from "./arrayFindLowObject.js";
import { arrayIsSorted } from "./arrayIsSorted.js";
import { arrayMoveLeft, arrayMoveRight } from "./arrayMove.js";
import { arrayNewSort } from "./arrayNewSort.js";
import { arrayShuffle } from "./arrayShuffle.js";
import { arraySortAscending } from "./arraySortAscending.js";
import { arraySortAscendingObject } from "./arraySortAscendingObject.js";
Expand Down Expand Up @@ -67,13 +70,17 @@ import { xor } from "./xor.js";
export {
DOM,
Games,
SortingAlgorithms,

allCharactersSame,
allEqual,
and,
arrayAllEqual,
arrayChoice,
arrayFindBigObject,
arrayFindLowObject,
arrayIsSorted,
arrayMoveLeft, arrayMoveRight,
arrayNewSort,
arrayShuffle,
arraySortAscending, arraySortAscendingObject,
arraySortDescending, arraySortDescendingObject,
Expand Down

0 comments on commit 9260d39

Please sign in to comment.