From 18edde797054a69d7ad6d50dd851924440afc457 Mon Sep 17 00:00:00 2001 From: mohmmadAyesh Date: Wed, 27 Mar 2024 19:53:18 +0200 Subject: [PATCH 1/2] fixing JSDOCS for Sorts Folder the folder that been modified are AlphaNumerical,BeadSort,BogoSort,BubbleSort,CocktailShakerSort,CountinfSort,FindSecondsLargestElementSort,FisherYatesShuffle --- Sorts/AlphaNumericalSort.js | 9 +++++++-- Sorts/BeadSort.js | 3 +++ Sorts/BogoSort.js | 3 +++ Sorts/BubbleSort.js | 4 ++++ Sorts/CocktailShakerSort.js | 3 +++ Sorts/CountingSort.js | 5 +++++ Sorts/FindSecondLargestElement.js | 6 +++++- Sorts/FisherYatesShuffle.js | 5 +++++ 8 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Sorts/AlphaNumericalSort.js b/Sorts/AlphaNumericalSort.js index fb6be8f424..9387bc0d78 100644 --- a/Sorts/AlphaNumericalSort.js +++ b/Sorts/AlphaNumericalSort.js @@ -17,9 +17,14 @@ 2. z11 P.S. use this function, as there are a lot of implementations on the stackoverflow and other forums, but many of them don't work correctly (can't pass all my tests) - + */ - +/** + * @param {string} a The first string to compare. + * @param {string} b The second string to compare. + * @returns {number} Returns a number indicating whether the first string comes before, after, or is the same as the second string in sort order. + * -1 if a comes before b, 1 if a comes after b, and 0 if they are the same. + */ const alphaNumericalSort = (a, b) => { /* https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare diff --git a/Sorts/BeadSort.js b/Sorts/BeadSort.js index 6a6f69398d..9a8fc2e075 100644 --- a/Sorts/BeadSort.js +++ b/Sorts/BeadSort.js @@ -7,6 +7,9 @@ * NOTE: It only works for arrays of positive integers. * * Wikipedia: https://en.wikipedia.org/wiki/Bead_sort + * @param {number[]} sequence An array of positive integers to be sorted using Bead Sort. + * @returns {number[]} Returns a sorted array of positive integers using Bead Sort. + * @throws {RangeError} Throws a RangeError if the input sequence contains any negative integers. */ export function beadSort(sequence) { /* Let's ensure our sequence has only Positive Integers */ diff --git a/Sorts/BogoSort.js b/Sorts/BogoSort.js index eeb4f7feeb..5b128616cc 100644 --- a/Sorts/BogoSort.js +++ b/Sorts/BogoSort.js @@ -1,5 +1,7 @@ /** * Checks whether the given array is sorted in ascending order. + * @param {number[]} array The array to be checked for sorted order. + * @returns {boolean} Returns true if the array is sorted in ascending order, false otherwise. */ export function isSorted(array) { const length = array.length @@ -13,6 +15,7 @@ export function isSorted(array) { /** * Shuffles the given array randomly in place. + * @param {any[]} array The array to be shuffled. */ function shuffle(array) { for (let i = array.length - 1; i; i--) { diff --git a/Sorts/BubbleSort.js b/Sorts/BubbleSort.js index 5571fac047..b5b8a0151e 100644 --- a/Sorts/BubbleSort.js +++ b/Sorts/BubbleSort.js @@ -15,6 +15,8 @@ /** * Using 2 for loops. + * @param {number[]} items The array to be sorted. + * @returns {number[]} The sorted array. */ export function bubbleSort(items) { const length = items.length @@ -42,6 +44,8 @@ export function bubbleSort(items) { /** * Using a while loop and a for loop. + * @param {number[]} arr The array to be sorted. + * @returns {number[]} The sorted array. */ export function alternativeBubbleSort(arr) { let swapped = true diff --git a/Sorts/CocktailShakerSort.js b/Sorts/CocktailShakerSort.js index 033c7c461a..fc79172980 100644 --- a/Sorts/CocktailShakerSort.js +++ b/Sorts/CocktailShakerSort.js @@ -7,6 +7,9 @@ * * Wikipedia (Cocktail Shaker Sort): https://en.wikipedia.org/wiki/Cocktail_shaker_sort * Wikipedia (Bubble Sort): https://en.wikipedia.org/wiki/Bubble_sort + * + * @param {number[]} items The array of numbers to be sorted. + * @returns {number[]} The sorted array. */ export function cocktailShakerSort(items) { for (let i = items.length - 1; i > 0; i--) { diff --git a/Sorts/CountingSort.js b/Sorts/CountingSort.js index c7d495d92f..1a2e54a408 100644 --- a/Sorts/CountingSort.js +++ b/Sorts/CountingSort.js @@ -6,6 +6,11 @@ * * Wikipedia: https://en.wikipedia.org/wiki/Counting_sort * Animated Visual: https://www.cs.usfca.edu/~galles/visualization/CountingSort.html + * + * @param {number[]} arr The array to be sorted. + * @param {number} min The minimum value in the array. + * @param {number} max The maximum value in the array. + * @returns {number[]} The sorted array. */ export const countingSort = (arr, min, max) => { diff --git a/Sorts/FindSecondLargestElement.js b/Sorts/FindSecondLargestElement.js index 504b7e1192..b7174085da 100644 --- a/Sorts/FindSecondLargestElement.js +++ b/Sorts/FindSecondLargestElement.js @@ -8,7 +8,11 @@ * Resources: * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set */ - +/** + * Finds the second largest element in an array of numbers while filtering out duplicate values. + * @param {number[]} array The array of numbers. + * @returns {number} The second largest element in the array. + */ const secondLargestElement = (array) => { const largestElement = Math.max(...array) let element = -Number.MAX_VALUE diff --git a/Sorts/FisherYatesShuffle.js b/Sorts/FisherYatesShuffle.js index 214cb5baa7..e584c0b8dc 100644 --- a/Sorts/FisherYatesShuffle.js +++ b/Sorts/FisherYatesShuffle.js @@ -1,3 +1,8 @@ +/** + * Shuffles the elements of the given array randomly in place. + * @param {Array} array The array to be shuffled. + * @returns {Array} The shuffled array. + */ export const shuffle = (array) => { let maxLength = array.length let temp From 756035711a5a234ee287b4c844d256a936f6d5d6 Mon Sep 17 00:00:00 2001 From: mohmmadAyesh Date: Wed, 27 Mar 2024 22:07:29 +0200 Subject: [PATCH 2/2] fixing or applying suggested changes --- Sorts/BeadSort.js | 4 ++-- Sorts/BogoSort.js | 4 ++-- Sorts/BubbleSort.js | 2 +- Sorts/CocktailShakerSort.js | 2 +- Sorts/FisherYatesShuffle.js | 2 -- 5 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Sorts/BeadSort.js b/Sorts/BeadSort.js index 9a8fc2e075..f4de1ac766 100644 --- a/Sorts/BeadSort.js +++ b/Sorts/BeadSort.js @@ -7,8 +7,8 @@ * NOTE: It only works for arrays of positive integers. * * Wikipedia: https://en.wikipedia.org/wiki/Bead_sort - * @param {number[]} sequence An array of positive integers to be sorted using Bead Sort. - * @returns {number[]} Returns a sorted array of positive integers using Bead Sort. + * @param {number[]} sequence An array of positive integers to be sorted. + * @returns {number[]} Returns a sorted array of positive integers. * @throws {RangeError} Throws a RangeError if the input sequence contains any negative integers. */ export function beadSort(sequence) { diff --git a/Sorts/BogoSort.js b/Sorts/BogoSort.js index 5b128616cc..313cacb591 100644 --- a/Sorts/BogoSort.js +++ b/Sorts/BogoSort.js @@ -1,6 +1,6 @@ /** * Checks whether the given array is sorted in ascending order. - * @param {number[]} array The array to be checked for sorted order. + * @param {String[]} * @returns {boolean} Returns true if the array is sorted in ascending order, false otherwise. */ export function isSorted(array) { @@ -15,7 +15,7 @@ export function isSorted(array) { /** * Shuffles the given array randomly in place. - * @param {any[]} array The array to be shuffled. + * @param {any[]} */ function shuffle(array) { for (let i = array.length - 1; i; i--) { diff --git a/Sorts/BubbleSort.js b/Sorts/BubbleSort.js index b5b8a0151e..40e9c2cb6e 100644 --- a/Sorts/BubbleSort.js +++ b/Sorts/BubbleSort.js @@ -44,7 +44,7 @@ export function bubbleSort(items) { /** * Using a while loop and a for loop. - * @param {number[]} arr The array to be sorted. + * @param {number[]} * @returns {number[]} The sorted array. */ export function alternativeBubbleSort(arr) { diff --git a/Sorts/CocktailShakerSort.js b/Sorts/CocktailShakerSort.js index fc79172980..2079b6753c 100644 --- a/Sorts/CocktailShakerSort.js +++ b/Sorts/CocktailShakerSort.js @@ -8,7 +8,7 @@ * Wikipedia (Cocktail Shaker Sort): https://en.wikipedia.org/wiki/Cocktail_shaker_sort * Wikipedia (Bubble Sort): https://en.wikipedia.org/wiki/Bubble_sort * - * @param {number[]} items The array of numbers to be sorted. + * @param {number[]} * @returns {number[]} The sorted array. */ export function cocktailShakerSort(items) { diff --git a/Sorts/FisherYatesShuffle.js b/Sorts/FisherYatesShuffle.js index e584c0b8dc..d892ff1449 100644 --- a/Sorts/FisherYatesShuffle.js +++ b/Sorts/FisherYatesShuffle.js @@ -1,6 +1,4 @@ /** - * Shuffles the elements of the given array randomly in place. - * @param {Array} array The array to be shuffled. * @returns {Array} The shuffled array. */ export const shuffle = (array) => {