forked from AustinCodingAcademy/javascript-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Class7algorithms #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
renataestes
wants to merge
8
commits into
gh-pages
Choose a base branch
from
class7algorithms
base: gh-pages
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -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; | ||
| } | ||
| } | ||
| } | ||
| 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)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = [], | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
|
|
||
This file contains hidden or 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,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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.