Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 69 additions & 8 deletions 04week/algorithms.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,83 @@ function getRandomInt(min, max) {
}

let arr = [];
const len = arr.length;

for (let i = 0; i < 1000; i++) {
for (let i = 0; i < 10; i++) {
arr.push(getRandomInt(0, 1000));
};

function bubbleSort(arr){
let len = arr.length;
for (let i = len-1; i>=0; i--){
for(let j = 1; j<=i; j++){
if(arr[j-1]>arr[j]){
var temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renata, I see where your going here, however, I would like for you to avoid using two loops and use a recursive function instead.

}
}
return arr;
}

function bubbleSort(arr) {
// Your code here
console.log(bubbleSort(arr));

function mergeSort(arr){
var len = arr.length;
if(len <2)
return arr;
var mid = Math.floor(len/2),
left = arr.slice(0,mid),
right =arr.slice(mid);
//send left and right to the mergeSort to broke it down into pieces
//then merge those
return merge(mergeSort(left),mergeSort(right));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice

}

function mergeSort(arr) {
// Your code here
function merge(left, right){
var result = [],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use var

lLen = left.length,
rLen = right.length,
l = 0,
r = 0;
while(l < lLen && r < rLen){
if(left[l] < right[r]){
result.push(left[l]);
l++;
}
else{
result.push(right[r]);
r++;
}
}
return result.concat(left.slice(l)).concat(right.slice(r));
}

function binarySearch(arr, item) {
// Your code here
console.log(mergeSort(arr));

let arrSorted = mergeSort(arr);

function binarySearch(arr, value){
let firstIndex = 0;
let lastIndex = arr.length - 1;
let middle = Math.floor((firstIndex + lastIndex)/2);

while(arr[middle] != value && firstIndex < lastIndex){
if (value < arr[middle]){
lastIndex = middle - 1;
} else if (value > arr[middle]){
firstIndex = middle + 1;
}
middle = Math.floor((firstIndex + lastIndex)/2);
}
if (arr[middle] != value) {
return false;
} else {
return true;
}
}
console.log(arrSorted);
console.log(binarySearch(arrSorted, 4));

// Tests

Expand Down
36 changes: 36 additions & 0 deletions 04week/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<html>
<script>
function mergeSort(arr){
var len = arr.length;
if(len <2)
return arr;
var mid = Math.floor(len/2),
left = arr.slice(0,mid),
right =arr.slice(mid);
//send left and right to the mergeSort to broke it down into pieces
//then merge those
return merge(mergeSort(left),mergeSort(right));
}

function merge(left, right){
var result = [],
lLen = left.length,
rLen = right.length,
l = 0,
r = 0;
while(l < lLen && r < rLen){
if(left[l] < right[r]){
result.push(left[l++]);
}
else{
result.push(right[r++]);
}
}
console.log(r);
//remaining part needs to be addred to the result
return result.concat(left.slice(l)).concat(right.slice(r));
}

console.log(mergeSort([5,4,3]));
</script>
</html>