From 75f6c8921cb9ff8ebae55dc4cc08999872acb222 Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Thu, 3 Aug 2017 06:10:59 -0500 Subject: [PATCH 1/8] Algorithm Assignment. Initial Push... Bubble sort whiteboard --- 04week/algorithms.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/04week/algorithms.js b/04week/algorithms.js index 5759b960e..dc716fdba 100644 --- a/04week/algorithms.js +++ b/04week/algorithms.js @@ -15,7 +15,20 @@ for (let i = 0; i < 1000; i++) { } function bubbleSort(arr) { - // Your code here + // Using bubble sort alrgorithm to sort passed array. + // set flip flag equal to false + // Loop through array + // Compare array[n] to array[n+1] + // if array[n] is larger, then flip + // set flip flag equal to true. + // End Loop + // if flip flag = true + // set flip flag back to false + // return bubbleSort(arr) + // else // array should be sorted + // return arr + // end if + } function mergeSort(arr) { From c3a3055930bb2d658bd3690d38fb6c3fdf0e153a Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Thu, 3 Aug 2017 17:38:43 -0500 Subject: [PATCH 2/8] Added merge Sort whiteboard --- 04week/algorithms.js | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/04week/algorithms.js b/04week/algorithms.js index dc716fdba..d4821d8e7 100644 --- a/04week/algorithms.js +++ b/04week/algorithms.js @@ -9,12 +9,13 @@ function getRandomInt(min, max) { } let arr = []; +let flip = false; -for (let i = 0; i < 1000; i++) { +for (let i = 0; i < 10; i++) { arr.push(getRandomInt(0, 1000)); } -function bubbleSort(arr) { +function bubbleSort(ary) { // Using bubble sort alrgorithm to sort passed array. // set flip flag equal to false // Loop through array @@ -28,13 +29,38 @@ function bubbleSort(arr) { // else // array should be sorted // return arr // end if - + for (let i = 0; i< ary.length; i++){ + if (ary[i] > ary[i + 1]) { + flip = true; + // using the swap without temp algorithm! Ex: ary[i] = 10, ary[i+1] = 6 + ary[i+1] = ary[i+1] - ary[i]; //Ex. 6 - 10 = -4 + ary[i] = ary[i] + ary[i+1]; //Ex. 10 + (-4) = 6 + ary[i+1] = ary[i] - ary[i+1]; //Ex. 6 - (-4) = 10 + } + } + // flip? (flip = false, return bubbleSort(ary)) : return(ary); // doesn't work. + if (flip) { + flip = false; // setting my check + return bubbleSort(ary); + } else { + return ary; + } } -function mergeSort(arr) { - // Your code here +console.log(arr); +console.log(bubbleSort(arr)); + +function mergeSort(ary) { + // Merge sort is a recursive algorithm. + // It is a divide in half approach. Keep dviding until you get to + // only 1 element on either the left of rigt side. + // Now call merge. Merge compares the two sub arrays and orders them. + // Ultimately, you get back to 2 sorted sub-arrays. Merge will finally order them. } +console.log(arr); +console.log(mergeSort(arr)); + function binarySearch(arr, item) { // Your code here } From c97924758b7dcead67459bbe3ee1a8d48949a266 Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Thu, 3 Aug 2017 17:39:46 -0500 Subject: [PATCH 3/8] Added merge sort code --- 04week/algorithms.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/04week/algorithms.js b/04week/algorithms.js index d4821d8e7..556d9be3a 100644 --- a/04week/algorithms.js +++ b/04week/algorithms.js @@ -56,6 +56,20 @@ function mergeSort(ary) { // only 1 element on either the left of rigt side. // Now call merge. Merge compares the two sub arrays and orders them. // Ultimately, you get back to 2 sorted sub-arrays. Merge will finally order them. + if (ary.length < 2) return ary; + + let middle = Math.floor(ary.length / 2); + let leftSide = ary.slice(0, middle); + let rightSide = ary.slice(middle); + + return merge(mergeSort(leftSide), mergeSort(rightSide)); +} + +function merge (left, right) { + let result = []; + while (left.length && right.length) + result.push(left[0] < right[0]? left.shift() : right.shift()); + return result.concat(left.length? left : right); } console.log(arr); From 21155c04753d783f67ff81537a0836a6ada8ee86 Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Thu, 3 Aug 2017 20:27:06 -0500 Subject: [PATCH 4/8] Adding skeleton code for binary sort --- 04week/algorithms.js | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/04week/algorithms.js b/04week/algorithms.js index 556d9be3a..9b2e54c43 100644 --- a/04week/algorithms.js +++ b/04week/algorithms.js @@ -16,6 +16,7 @@ for (let i = 0; i < 10; i++) { } function bubbleSort(ary) { + /*********************** WHITE BOARD NOTES ************************** // Using bubble sort alrgorithm to sort passed array. // set flip flag equal to false // Loop through array @@ -29,6 +30,8 @@ function bubbleSort(ary) { // else // array should be sorted // return arr // end if + ********************************************************************/ + for (let i = 0; i< ary.length; i++){ if (ary[i] > ary[i + 1]) { flip = true; @@ -51,12 +54,15 @@ console.log(arr); console.log(bubbleSort(arr)); function mergeSort(ary) { + /*********************** WHITE BOARD NOTES ************************** // Merge sort is a recursive algorithm. // It is a divide in half approach. Keep dviding until you get to - // only 1 element on either the left of rigt side. + // only 1 element on either the left or right side. // Now call merge. Merge compares the two sub arrays and orders them. // Ultimately, you get back to 2 sorted sub-arrays. Merge will finally order them. if (ary.length < 2) return ary; + ********************************************************************/ + let middle = Math.floor(ary.length / 2); let leftSide = ary.slice(0, middle); @@ -75,8 +81,36 @@ function merge (left, right) { console.log(arr); console.log(mergeSort(arr)); -function binarySearch(arr, item) { - // Your code here +function binarySearch(ary, item) { + /*********************** WHITE BOARD NOTES ************************** + // Assumptions... + // You are given a sorted data array to search on. + // Given a value to search for... + // + // Binary Search. + // Locate the middle of the data array + // Compare search value with middle. + // If search value = middle value then + // Congrats youfound it! + // return index position in array. + // else if search value < middle value + // Change array to be subArray[0 - middle value-1] + // return binarySearch[subArray] + // else if search value > middle value then + // Change array to be subArray[0 - middle value-1] + // Change array to be subArray[middle value+1 - endArray.] + // return binarySearch[subArray] + // end if + // return -1 // did not find the search value in the data array + ********************************************************************/ + + let middle = Math.floor(ary.length / 2); + if (item === ary[middle]) return middle; + else if (item < ary[middle]) return binarySearch(ary.slice(0, middle-1), item); + else if (item > ary[middle]) return binarySearch(ary.slice(middle+1), item); + return -1; + + } } // Tests From d62e836b167db6658b3edb57127cd3eba8e9230e Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Sat, 5 Aug 2017 14:36:54 -0500 Subject: [PATCH 5/8] Completed binary search --- 04week/algorithms.js | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/04week/algorithms.js b/04week/algorithms.js index 9b2e54c43..031ad42db 100644 --- a/04week/algorithms.js +++ b/04week/algorithms.js @@ -81,12 +81,12 @@ function merge (left, right) { console.log(arr); console.log(mergeSort(arr)); -function binarySearch(ary, item) { +function binarySearch(ary, item, start=0, end = ary.length-1) { /*********************** WHITE BOARD NOTES ************************** // Assumptions... // You are given a sorted data array to search on. // Given a value to search for... - // + // ****** DID NOT WORK! ARRAY IS BROKEN DOWN UNTIL VALUE IS 0 ELEMENT ***** // Binary Search. // Locate the middle of the data array // Compare search value with middle. @@ -102,14 +102,30 @@ function binarySearch(ary, item) { // return binarySearch[subArray] // end if // return -1 // did not find the search value in the data array + // *** RESEARCHED. FOUND RECURSIVE EXAMPLE PASSING IN START, END ********* + // Keep array intact (no splice, slice). Pass in start, end search elements. + // Locate the middle of the data array + // Compare search value with middle. + // If search value = middle value then + // Congrats youfound it! + // return index position in array. + // else if start element = end element then + // return false. Value not in array + // else if search value < middle value + // Change end search = middle-1 + // return binarySearch(array, item, start end) + // else if search value > middle value then + // Change start search = middle+1 + // return binarySearch(array, item, start end) + // end if ********************************************************************/ - let middle = Math.floor(ary.length / 2); - if (item === ary[middle]) return middle; - else if (item < ary[middle]) return binarySearch(ary.slice(0, middle-1), item); - else if (item > ary[middle]) return binarySearch(ary.slice(middle+1), item); - return -1; - + let middle = Math.floor(start + (end-start)/2); + console.log(start, end, middle, ary[middle]); + if (item == ary[middle]) return middle; + else if (start>=end) return false; + else if (item < ary[middle]) return binarySearch(ary, item, start, middle-1); + else if (item > ary[middle]) return binarySearch(ary, item, middle+1, end); } } From 55b2d3db526b09763d40c8648434e73abe5bf8a0 Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Sun, 6 Aug 2017 00:35:36 -0500 Subject: [PATCH 6/8] Fixed bug in mergeSort. Added more comments and console.log stmts --- 04week/algorithms.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/04week/algorithms.js b/04week/algorithms.js index 031ad42db..d4744213f 100644 --- a/04week/algorithms.js +++ b/04week/algorithms.js @@ -12,7 +12,7 @@ let arr = []; let flip = false; for (let i = 0; i < 10; i++) { - arr.push(getRandomInt(0, 1000)); + arr.push(getRandomInt(0, 100)); } function bubbleSort(ary) { @@ -50,6 +50,7 @@ function bubbleSort(ary) { } } +console.log('Bubble Sort'); console.log(arr); console.log(bubbleSort(arr)); @@ -60,9 +61,8 @@ function mergeSort(ary) { // only 1 element on either the left or right side. // Now call merge. Merge compares the two sub arrays and orders them. // Ultimately, you get back to 2 sorted sub-arrays. Merge will finally order them. - if (ary.length < 2) return ary; ********************************************************************/ - + if (ary.length < 2) return ary; let middle = Math.floor(ary.length / 2); let leftSide = ary.slice(0, middle); @@ -78,8 +78,14 @@ function merge (left, right) { return result.concat(left.length? left : right); } +// Adding 10 more random values to array. +for (let i = 0; i < 10; i++) { + arr.push(getRandomInt(0, 100)); +} +console.log('Merge Sort'); +console.log(arr); +arr = mergeSort(arr); console.log(arr); -console.log(mergeSort(arr)); function binarySearch(ary, item, start=0, end = ary.length-1) { /*********************** WHITE BOARD NOTES ************************** @@ -120,15 +126,19 @@ function binarySearch(ary, item, start=0, end = ary.length-1) { // end if ********************************************************************/ - let middle = Math.floor(start + (end-start)/2); - console.log(start, end, middle, ary[middle]); - if (item == ary[middle]) return middle; - else if (start>=end) return false; - else if (item < ary[middle]) return binarySearch(ary, item, start, middle-1); - else if (item > ary[middle]) return binarySearch(ary, item, middle+1, end); - } + let middle = Math.floor(start + (end-start)/2); // find the halfway position in array + // console.log(start, end, middle, ary[middle]); + if (item == ary[middle]) return middle; // Congrats. you found the item. + else if (start>=end) return false; // item not in array + else if (item < ary[middle]) return binarySearch(ary, item, start, middle-1); // item in lower half + else if (item > ary[middle]) return binarySearch(ary, item, middle+1, end); // item in upper half } +console.log('Binary Search'); +console.log(arr); +console.log(`Looking for 42. It is here: ${binarySearch(arr, 42)}`); +// console.log(binarySearch(arr, 42)); + // Tests if (typeof describe === 'function') { From 84ba68b55a8f984e5f438fa942c27b55b0dd3ea9 Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Tue, 15 Aug 2017 06:47:54 -0500 Subject: [PATCH 7/8] Changed all let vars to const where allowable. In mergeSort and bubbleSort. --- 04week/algorithms.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/04week/algorithms.js b/04week/algorithms.js index d4744213f..6f4e8f672 100644 --- a/04week/algorithms.js +++ b/04week/algorithms.js @@ -64,9 +64,9 @@ function mergeSort(ary) { ********************************************************************/ if (ary.length < 2) return ary; - let middle = Math.floor(ary.length / 2); - let leftSide = ary.slice(0, middle); - let rightSide = ary.slice(middle); + const middle = Math.floor(ary.length / 2); + const leftSide = ary.slice(0, middle); + const rightSide = ary.slice(middle); return merge(mergeSort(leftSide), mergeSort(rightSide)); } @@ -126,7 +126,7 @@ function binarySearch(ary, item, start=0, end = ary.length-1) { // end if ********************************************************************/ - let middle = Math.floor(start + (end-start)/2); // find the halfway position in array + const middle = Math.floor(start + (end-start)/2); // find the halfway position in array // console.log(start, end, middle, ary[middle]); if (item == ary[middle]) return middle; // Congrats. you found the item. else if (start>=end) return false; // item not in array @@ -174,8 +174,7 @@ if (typeof describe === 'function') { }); }); -} else { - - console.log('Run the tests!') - } +// else { +// console.log('Run the tests!') +// } From 2c5a645bbef712afa401d2b6a314926426e7010d Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Tue, 15 Aug 2017 06:51:47 -0500 Subject: [PATCH 8/8] Removed the final else. It was for debugging purposes. --- 04week/algorithms.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/04week/algorithms.js b/04week/algorithms.js index 6f4e8f672..72ac62846 100644 --- a/04week/algorithms.js +++ b/04week/algorithms.js @@ -175,6 +175,3 @@ if (typeof describe === 'function') { }); } -// else { -// console.log('Run the tests!') -// }