-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
freeCodeCamp Algorithm scripting/implement-bubble-sort.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
function bubbleSort(arr) { | ||
let isSorted = false; | ||
|
||
for (let x = 0; isSorted === false; x++) { | ||
let swapsCount = 0; | ||
|
||
for (let i = 0; i < arr.length - 1; i++) { | ||
if (arr[i] > arr[i + 1]) { | ||
let numRef = arr[i]; | ||
arr[i] = arr[i + 1]; | ||
arr[i + 1] = numRef; | ||
swapsCount++; | ||
} | ||
|
||
if (swapsCount === 0 && (i + 2 === arr.length)) { | ||
isSorted = true; | ||
return arr; | ||
} | ||
} | ||
} | ||
} | ||
|
||
bubbleSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]); |
15 changes: 15 additions & 0 deletions
15
freeCodeCamp Algorithm scripting/implement-insertion-sort.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function insertionSort(arr) { | ||
for(let i = 0; i < arr.length; i++){ | ||
let arr2 = arr.slice(0,i); | ||
let currentNum = arr[i]; | ||
|
||
for(let x = 0; x < arr2.length; x++){ | ||
if(arr[i] < arr2[x]){ | ||
arr.splice(i,1); | ||
arr = [arr.slice(0, x), currentNum, arr.slice(x)].flat(); | ||
} | ||
} | ||
} | ||
return arr; | ||
} | ||
insertionSort([5, 4, 33, 2, 8]) |
22 changes: 22 additions & 0 deletions
22
freeCodeCamp Algorithm scripting/implement-selection-sort.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
function selectionSort(arr) { | ||
for(let i = 0; i < arr.length - 1; i++){ | ||
let arr2 = arr.slice(i+1) | ||
let leastRef = arr2[0]; | ||
let leastIndex = 0; | ||
|
||
for(let x = 0; x < arr2.length; x++){ | ||
if(arr2[x] <= leastRef){ | ||
leastRef = arr2[x]; | ||
leastIndex = (arr.length - arr2.length) + x; | ||
} | ||
} | ||
if(leastRef < arr[i]){ | ||
arr[leastIndex] = arr[i]; | ||
arr[i] = leastRef; | ||
} | ||
} | ||
|
||
return arr | ||
} | ||
|
||
selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]) |
16 changes: 16 additions & 0 deletions
16
freeCodeCamp Algorithm scripting/problem-1-multiples-of-3-or-5.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function multiplesOf3Or5(number) { | ||
let multiplesArr = []; | ||
let testArr = [3, 5]; | ||
|
||
for(let i = 1; i < number; i++){ | ||
testArr.forEach(x => { | ||
const multiply = x*i; | ||
!multiplesArr.includes(multiply) && multiply < number && multiplesArr.push(multiply); | ||
}) | ||
} | ||
|
||
const result = multiplesArr.reduce((a,b) => a+b); | ||
return result | ||
} | ||
|
||
multiplesOf3Or5(49); |