Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Arplight authored Jul 15, 2024
1 parent 721335c commit f8e12bf
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
23 changes: 23 additions & 0 deletions freeCodeCamp Algorithm scripting/implement-bubble-sort.txt
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 freeCodeCamp Algorithm scripting/implement-insertion-sort.txt
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 freeCodeCamp Algorithm scripting/implement-selection-sort.txt
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 freeCodeCamp Algorithm scripting/problem-1-multiples-of-3-or-5.txt
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);

0 comments on commit f8e12bf

Please sign in to comment.