Update Js (ES5, ES6) to make them consistent and explicit (#54)
* Update the names to make ES5 and ES6 solutions more consistent I basically change the names to make the function more explicit and clear: 1. list to sortedList 2. mid to middle * Revert "Update the names to make ES5 and ES6 solutions more consistent" This reverts commit 46d7514636420eb3b3c581665eaa3c0aca9df99d. * [selection sort] update Js (ES5, ES6) to make them more consistent and explicit
This commit is contained in:
@@ -1,26 +1,35 @@
|
|||||||
// Finds the smallest value in an array
|
// Selection Sort - O(log n^2)
|
||||||
const findSmallest = (arr) => {
|
// Parameter:
|
||||||
let smallest = arr[0]; // Stores the smallest value
|
// 1. random array
|
||||||
|
|
||||||
|
// 1. Finds the smallest value in an array
|
||||||
|
const findSmallestIndex = (array) => {
|
||||||
|
let smallestElement = array[0]; // Stores the smallest value
|
||||||
let smallestIndex = 0; // Stores the index of the smallest value
|
let smallestIndex = 0; // Stores the index of the smallest value
|
||||||
for (let i = 1; i < arr.length; i += 1) {
|
|
||||||
if (arr[i] < smallest) {
|
for (let i = 1; i < array.length; i++) {
|
||||||
smallest = arr[i];
|
if (array[i] < smallestElement) {
|
||||||
|
smallestElement = array[i];
|
||||||
smallestIndex = i;
|
smallestIndex = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return smallestIndex;
|
return smallestIndex;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Sort array
|
// 2. Sorts the array
|
||||||
const selectionSort = (arr) => {
|
const selectionSort = (array) => {
|
||||||
const newArr = [];
|
const sortedArray = [];
|
||||||
const length = arr.length;
|
const length = array.length;
|
||||||
for (let i = 0; i < length; i += 1) {
|
|
||||||
// Finds the smallest element in the array and adds it to the new array
|
for (let i = 0; i < length; i++) {
|
||||||
const smallest = findSmallest(arr);
|
// Finds the smallest element in the given array
|
||||||
newArr.push(arr.splice(smallest, 1)[0]);
|
const smallestIndex = findSmallestIndex(array);
|
||||||
|
// Adds the smallest element to new array
|
||||||
|
sortedArray.push(array.splice(smallestIndex, 1)[0]);
|
||||||
}
|
}
|
||||||
return newArr;
|
|
||||||
|
return sortedArray;
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log(selectionSort([5, 3, 6, 2, 10])); // [2, 3, 5, 6, 10]
|
console.log(selectionSort([5, 3, 6, 2, 10])); // [2, 3, 5, 6, 10]
|
||||||
|
|||||||
@@ -1,27 +1,36 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
// Selection Sort - O(log n^2)
|
||||||
|
// Parameter:
|
||||||
|
// 1. random array
|
||||||
|
|
||||||
// Finds the smallest value in an array
|
// 1. Finds the smallest value in an array
|
||||||
function findSmallest(arr) {
|
function findSmallestIndex(array) {
|
||||||
let smallest = arr[0]; // Stores the smallest value
|
var smallestElement = array[0]; // Stores the smallest value
|
||||||
let smallest_index = 0; // Stores the index of the smallest value
|
var smallestIndex = 0; // Stores the index of the smallest value
|
||||||
for (let i = 1; i < arr.length; i++) {
|
|
||||||
if (arr[i] < smallest) {
|
for (var i = 0; i < array.length; i++) {
|
||||||
smallest = arr[i];
|
if (array[i] < smallestElement) {
|
||||||
smallest_index = i;
|
smallestElement = array[i];
|
||||||
|
smallestIndex = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return smallest_index;
|
|
||||||
|
return smallestIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort array
|
// 2. Sort the array
|
||||||
function selectionSort(arr) {
|
function selectionSort(array) {
|
||||||
const newArr = [];
|
var sortedArray = [];
|
||||||
for (let i = 0, length = arr.length; i < length; i++) {
|
var length = array.length;
|
||||||
// Finds the smallest element in the array and adds it to the new array
|
|
||||||
let smallest = findSmallest(arr);
|
for (var i = 0; i < length; i++) {
|
||||||
newArr.push(arr.splice(smallest, 1)[0]);
|
// Finds the smallest element in the array
|
||||||
|
var smallestIndex = findSmallestIndex(array);
|
||||||
|
// Adds the smallest element to new array
|
||||||
|
sortedArray.push(array.splice(smallestIndex, 1)[0]);
|
||||||
}
|
}
|
||||||
return newArr;
|
|
||||||
|
return sortedArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(selectionSort([5, 3, 6, 2, 10])); // [2, 3, 5, 6, 10]
|
console.log(selectionSort([5, 3, 6, 2, 10])); // [2, 3, 5, 6, 10]
|
||||||
|
|||||||
Reference in New Issue
Block a user