From f9497eb34482397cc2444d36b0c02f16bf1de515 Mon Sep 17 00:00:00 2001 From: sliw <35519787+sliw@users.noreply.github.com> Date: Sun, 4 Feb 2018 03:59:16 +0800 Subject: [PATCH] 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 --- 02_selection_sort/ES6/01_selection_sort.js | 39 ++++++++++------- .../javascript/01_selection_sort.js | 43 +++++++++++-------- 2 files changed, 50 insertions(+), 32 deletions(-) diff --git a/02_selection_sort/ES6/01_selection_sort.js b/02_selection_sort/ES6/01_selection_sort.js index 3d7d06f..31b6a8c 100644 --- a/02_selection_sort/ES6/01_selection_sort.js +++ b/02_selection_sort/ES6/01_selection_sort.js @@ -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] diff --git a/02_selection_sort/javascript/01_selection_sort.js b/02_selection_sort/javascript/01_selection_sort.js index 1ece0be..28ed1e9 100644 --- a/02_selection_sort/javascript/01_selection_sort.js +++ b/02_selection_sort/javascript/01_selection_sort.js @@ -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]