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:
sliw
2018-02-04 03:59:16 +08:00
committed by Aditya Bhargava
parent 3474a1069f
commit f9497eb344
2 changed files with 50 additions and 32 deletions

View File

@@ -1,26 +1,35 @@
// Finds the smallest value in an array
const findSmallest = (arr) => {
let smallest = arr[0]; // Stores the smallest value
// Selection Sort - O(log n^2)
// Parameter:
// 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
for (let i = 1; i < arr.length; i += 1) {
if (arr[i] < smallest) {
smallest = arr[i];
for (let i = 1; i < array.length; i++) {
if (array[i] < smallestElement) {
smallestElement = array[i];
smallestIndex = i;
}
}
return smallestIndex;
};
// Sort array
const selectionSort = (arr) => {
const newArr = [];
const length = arr.length;
for (let i = 0; i < length; i += 1) {
// Finds the smallest element in the array and adds it to the new array
const smallest = findSmallest(arr);
newArr.push(arr.splice(smallest, 1)[0]);
// 2. Sorts the array
const selectionSort = (array) => {
const sortedArray = [];
const length = array.length;
for (let i = 0; i < length; i++) {
// Finds the smallest element in the given array
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]

View File

@@ -1,27 +1,36 @@
'use strict';
// Selection Sort - O(log n^2)
// Parameter:
// 1. random array
// Finds the smallest value in an array
function findSmallest(arr) {
let smallest = arr[0]; // Stores the smallest value
let smallest_index = 0; // Stores the index of the smallest value
for (let i = 1; i < arr.length; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
smallest_index = i;
// 1. Finds the smallest value in an array
function findSmallestIndex(array) {
var smallestElement = array[0]; // Stores the smallest value
var smallestIndex = 0; // Stores the index of the smallest value
for (var i = 0; i < array.length; i++) {
if (array[i] < smallestElement) {
smallestElement = array[i];
smallestIndex = i;
}
}
return smallest_index;
return smallestIndex;
}
// Sort array
function selectionSort(arr) {
const newArr = [];
for (let i = 0, length = arr.length; i < length; i++) {
// Finds the smallest element in the array and adds it to the new array
let smallest = findSmallest(arr);
newArr.push(arr.splice(smallest, 1)[0]);
// 2. Sort the array
function selectionSort(array) {
var sortedArray = [];
var length = array.length;
for (var i = 0; i < length; i++) {
// 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]