Skip to content

Commit f9497eb

Browse files
sliwegonSchiele
authored andcommitted
Update Js (ES5, ES6) to make them consistent and explicit (egonSchiele#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 46d7514. * [selection sort] update Js (ES5, ES6) to make them more consistent and explicit
1 parent 3474a10 commit f9497eb

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed
Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
1-
// Finds the smallest value in an array
2-
const findSmallest = (arr) => {
3-
let smallest = arr[0]; // Stores the smallest value
1+
// Selection Sort - O(log n^2)
2+
// Parameter:
3+
// 1. random array
4+
5+
// 1. Finds the smallest value in an array
6+
const findSmallestIndex = (array) => {
7+
let smallestElement = array[0]; // Stores the smallest value
48
let smallestIndex = 0; // Stores the index of the smallest value
5-
for (let i = 1; i < arr.length; i += 1) {
6-
if (arr[i] < smallest) {
7-
smallest = arr[i];
9+
10+
for (let i = 1; i < array.length; i++) {
11+
if (array[i] < smallestElement) {
12+
smallestElement = array[i];
813
smallestIndex = i;
914
}
1015
}
16+
1117
return smallestIndex;
1218
};
1319

14-
// Sort array
15-
const selectionSort = (arr) => {
16-
const newArr = [];
17-
const length = arr.length;
18-
for (let i = 0; i < length; i += 1) {
19-
// Finds the smallest element in the array and adds it to the new array
20-
const smallest = findSmallest(arr);
21-
newArr.push(arr.splice(smallest, 1)[0]);
20+
// 2. Sorts the array
21+
const selectionSort = (array) => {
22+
const sortedArray = [];
23+
const length = array.length;
24+
25+
for (let i = 0; i < length; i++) {
26+
// Finds the smallest element in the given array
27+
const smallestIndex = findSmallestIndex(array);
28+
// Adds the smallest element to new array
29+
sortedArray.push(array.splice(smallestIndex, 1)[0]);
2230
}
23-
return newArr;
31+
32+
return sortedArray;
2433
};
2534

2635
console.log(selectionSort([5, 3, 6, 2, 10])); // [2, 3, 5, 6, 10]
Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
'use strict';
2+
// Selection Sort - O(log n^2)
3+
// Parameter:
4+
// 1. random array
25

3-
// Finds the smallest value in an array
4-
function findSmallest(arr) {
5-
let smallest = arr[0]; // Stores the smallest value
6-
let smallest_index = 0; // Stores the index of the smallest value
7-
for (let i = 1; i < arr.length; i++) {
8-
if (arr[i] < smallest) {
9-
smallest = arr[i];
10-
smallest_index = i;
6+
// 1. Finds the smallest value in an array
7+
function findSmallestIndex(array) {
8+
var smallestElement = array[0]; // Stores the smallest value
9+
var smallestIndex = 0; // Stores the index of the smallest value
10+
11+
for (var i = 0; i < array.length; i++) {
12+
if (array[i] < smallestElement) {
13+
smallestElement = array[i];
14+
smallestIndex = i;
1115
}
1216
}
13-
return smallest_index;
17+
18+
return smallestIndex;
1419
}
1520

16-
// Sort array
17-
function selectionSort(arr) {
18-
const newArr = [];
19-
for (let i = 0, length = arr.length; i < length; i++) {
20-
// Finds the smallest element in the array and adds it to the new array
21-
let smallest = findSmallest(arr);
22-
newArr.push(arr.splice(smallest, 1)[0]);
21+
// 2. Sort the array
22+
function selectionSort(array) {
23+
var sortedArray = [];
24+
var length = array.length;
25+
26+
for (var i = 0; i < length; i++) {
27+
// Finds the smallest element in the array
28+
var smallestIndex = findSmallestIndex(array);
29+
// Adds the smallest element to new array
30+
sortedArray.push(array.splice(smallestIndex, 1)[0]);
2331
}
24-
return newArr;
32+
33+
return sortedArray;
2534
}
2635

2736
console.log(selectionSort([5, 3, 6, 2, 10])); // [2, 3, 5, 6, 10]

0 commit comments

Comments
 (0)