Fixed formatting problems, example and JSDoc (#129)

This commit is contained in:
Alexandr Shulaev
2022-11-19 00:47:16 +04:00
committed by GitHub
parent 5f416d1129
commit c029201ab1
3 changed files with 76 additions and 26 deletions

View File

@@ -1,8 +1,8 @@
// Selection Sort - O(n^2)
// Parameter:
// 1. random array
// 1. Finds the smallest value in an array
/**
* 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
*/
const findSmallestIndex = (array) => {
let smallestElement = array[0]; // Stores the smallest value
let smallestIndex = 0; // Stores the index of the smallest value
@@ -17,23 +17,27 @@ const findSmallestIndex = (array) => {
return smallestIndex;
};
// 2. Sorts the array
/**
* Sort array by increment
* @param {Array} array Source array
* @returns {Array} New sorted array
*/
const selectionSort = (array) => {
//Copy values from array, because it must be immutable. Without that after call selectionSort origin array will become empty.
const sortingArray = [...array];
const sortedArray = [];
const length = sortingArray.length;
const copyArray = [...array];
for (let i = 0; i < length; i++) {
// Finds the smallest element in the given array
const smallestIndex = findSmallestIndex(sortingArray);
for (let i = 0; i < array.length; i++) {
// Finds the smallest element in the array
const smallestIndex = findSmallestIndex(copyArray);
// Adds the smallest element to new array
sortedArray.push(sortingArray.splice(smallestIndex, 1)[0]);
sortedArray.push(copyArray.splice(smallestIndex, 1)[0]);
}
return sortedArray;
};
const array = [5, 3, 6, 2, 10];
console.log(selectionSort(array)); // [2, 3, 5, 6, 10]
console.log(array); // [5, 3, 6, 2, 10]
const sourceArray = [5, 3, 6, 2, 10];
const sourtedArray = selectionSort([5, 3, 6, 2, 10]);
console.log("Source array - ", sourceArray); // [5, 3, 6, 2, 10]
console.log("New sorted array - ", sourtedArray); // [2, 3, 5, 6, 10]

View File

@@ -0,0 +1,36 @@
/**
* 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
*/
const findSmallestIndex = array => {
let smallestElement = array[0]; // Stores the smallest value
let smallestIndex = 0; // Stores the index of the smallest value
for (let i = 1; i < array.length; i++) {
if (array[i] < smallestElement) {
smallestElement = array[i];
smallestIndex = i;
}
}
return smallestIndex;
};
/**
* Sort array by increment
* @param {Array} array Source array
* @returns {Array} New sorted array
*/
const selectionSort = array => {
if (!array.length) return [];
const copyArray = [...array];
const smallest = copyArray.splice(findSmallestIndex(copyArray), 1);
return smallest.concat(selectionSort(copyArray));
};
const sourceArray = [5, 3, 6, 2, 10];
const sourtedArray = selectionSort([5, 3, 6, 2, 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,9 +1,10 @@
'use strict';
// Selection Sort - O(n^2)
// Parameter:
// 1. random array
"use strict";
// 1. Finds the smallest value in an array
/**
* 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
@@ -18,19 +19,28 @@ function findSmallestIndex(array) {
return smallestIndex;
}
// 2. Sort the array
/**
* 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(array);
// Finds the smallest element in the array
var smallestIndex = findSmallestIndex(copyArray);
// Adds the smallest element to new array
sortedArray.push(array.splice(smallestIndex, 1)[0]);
sortedArray.push(copyArray.splice(smallestIndex, 1)[0]);
}
return sortedArray;
}
console.log(selectionSort([5, 3, 6, 2, 10])); // [2, 3, 5, 6, 10]
const sourceArray = [5, 3, 6, 2, 10];
const sourtedArray = selectionSort([5, 3, 6, 2, 10]);
console.log("Source array - ", sourceArray); // [5, 3, 6, 2, 10]
console.log("New sorted array - ", sourtedArray); // [2, 3, 5, 6, 10]