Increase readability, update code style and optimise for modern JavaScript (#273)

* Remove loops on recursive example, increase readability and optimise

* Update style and optimise

* Update 01_loop_sum_reduce_version.js

Update style

* Update style

* Rename identificators

* Rename identificators

* Convert shift to splice
This commit is contained in:
Timur Sevimli
2024-12-07 16:48:14 +03:00
committed by GitHub
parent c06ad954f0
commit 45bf3c086d
4 changed files with 46 additions and 56 deletions

View File

@@ -1,46 +1,39 @@
"use strict";
'use strict';
/**
* Finds the index of the element with the smallest value in the array
* @param {Array} array Source array
* @returns {number} Index of the element with the smallest value
*/
function findSmallestIndex(array) {
var smallestElement = array[0]; // Stores the smallest value
var smallestIndex = 0; // Stores the index of the smallest value
for (var i = 1; i < array.length; i++) {
if (array[i] < smallestElement) {
smallestElement = array[i];
smallestIndex = i;
}
const findSmallest = (arr) => {
let [smallestElement] = arr;
let smallestIndex = 0;
for (let i = 1; i < arr.length; i++) {
const el = arr[i];
if (el >= smallestElement) continue;
smallestElement = el;
smallestIndex = i;
}
return smallestIndex;
}
};
/**
* Sort array by increment
* @param {Array} array Source array
* @returns {Array} New sorted array
*/
function selectionSort(array) {
var sortedArray = [];
var copyArray = array.slice();
var length = array.length;
for (var i = 0; i < length; i++) {
// Finds the smallest element in the array
var smallestIndex = findSmallestIndex(copyArray);
// Adds the smallest element to new array
sortedArray.push(copyArray.splice(smallestIndex, 1)[0]);
const selectionSort = (arr) => {
const size = arr.length;
const result = new Array(size).fill(0);
for (let i = 0; i < size; i++) {
const smallestIndex = findSmallest(arr);
const [smallestElement] = arr.splice(smallestIndex, 1);
result[i] = smallestElement;
}
return sortedArray;
}
return result;
};
const sourceArray = [5, 3, 6, 2, 10];
const sortedArray = selectionSort(sourceArray);
console.log("Source array - ", sourceArray); // [5, 3, 6, 2, 10]
console.log("New sorted array - ", sortedArray); // [2, 3, 5, 6, 10]
console.log('Source array - ', sourceArray); // [5, 3, 6, 2, 10]
console.log('New sorted array - ', sourtedArray); // [2, 3, 5, 6, 10]

View File

@@ -1,20 +1,17 @@
'use strict';
/**
* Finds smallest element of an aray
* @param {Array} arr array for searching
* @return {number} index of the smallest element in array
*/
const findSmallest = ( arr ) => {
let smallest = arr[0];
let smallestIndex = 0;
let arrLen = arr.length;
for ( let i = 0; i < arrLen; i++ ) {
if ( arr[i] < smallest ) {
smallest = arr[i];
smallestIndex = i;
}
}
return smallestIndex;
const findSmallest = (arr, smallest = arr[0], smallestIndex = 0, i = 1) => {
if (arr.length <= i) return smallestIndex;
const curr = arr[i];
if (curr < smallest) {
smallest = curr;
smallestIndex = i;
}
return findSmallest(arr, smallest, smallestIndex, i + 1);
};
/**
@@ -22,12 +19,15 @@ const findSmallest = ( arr ) => {
* @param {Array} arr An array of numbers
* @return {Array} New sorted array
*/
const selectionSort = ( arr ) => {
if ( !arr.length ) return [];
let smallest = arr.splice( findSmallest( arr ), 1 );
return smallest.concat( selectionSort( arr ) );
const selectionSort = (arr, result = []) => {
if (arr.length > 0) {
const smallestIndex = findSmallest(arr);
const [smallest] = arr.splice(smallestIndex, 1);
result.push(smallest);
return selectionSort(arr, result);
}
return result;
};
let arr = [5, 3, 6, 2, 10];
console.log( selectionSort(arr) );
const arr = [5, 3, 6, 2, 10];
console.log(selectionSort(arr));