-
-
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
40 changed files
with
201 additions
and
93 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1 +1 @@ | ||
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>WeCode</title><script defer="defer" src="/static/js/main.b3c6d50b.js"></script><link href="/static/css/main.8bfa3784.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html> | ||
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>WeCode</title><script defer="defer" src="/static/js/main.4ee7ae0e.js"></script><link href="/static/css/main.fb5a12ad.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html> |
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
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 |
---|---|---|
@@ -1 +1 @@ | ||
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>WeCode</title><script defer="defer" src="/static/js/main.b3c6d50b.js"></script><link href="/static/css/main.8bfa3784.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html> | ||
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>WeCode</title><script defer="defer" src="/static/js/main.4ee7ae0e.js"></script><link href="/static/css/main.fb5a12ad.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html> |
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 |
---|---|---|
@@ -1,51 +1,114 @@ | ||
# Merge Sort | ||
|
||
## Context | ||
Merge Sort is a fundamental algorithm in computer science. It is a divide and conquer algorithm that was invented by John von Neumann in 1945. Merge Sort is efficient, stable and works well for large datasets. | ||
|
||
## Example | ||
Consider an unsorted array: `[38, 27, 43, 3, 9, 82, 10]`. After applying the merge sort algorithm, the array becomes: `[3, 9, 10, 27, 38, 43, 82]`. | ||
The merge sort algorithm is a divide and conquer algorithm that works by dividing the input array into two halves, | ||
sorting the two halves, and then merging them back together. | ||
|
||
## Description | ||
Write a function that sorts an array of numbers using the merge sort algorithm. The function should take an unsorted array as input and return a new array that contains all the elements from the input array, in sorted order. | ||
# Description | ||
|
||
## It is a recursive algorithm that uses the following steps: | ||
|
||
1. Divide the array into two halves. | ||
2. Recursively sort the two halves. | ||
- If the array has only one element, return the array. (Base case) | ||
- Otherwise, divide the array into two halves and recursively sort each half | ||
3. Merge the two halves back together in sorted order. | ||
- Note you'll probablt want to write a helper function to merge the two halves together. | ||
You will need to define this function inside the `mergeSort` function. | ||
- The `merge` function should take two arrays as arguments and return a single sorted array. | ||
- How can you use the fact that the two halves are already sorted to merge them together efficiently? | ||
4. Return the sorted array. | ||
|
||
## Useful Information: | ||
|
||
- You can use the `slice` method to divide the array into two halves. | ||
- Example: `[1, 2, 3, 4, 5].slice(0, 3)` returns `[1, 2, 3]`. | ||
- You can use the `concat` method to merge two arrays together. | ||
- Example: `[1, 2].concat([3, 4])` returns `[1, 2, 3, 4]`. | ||
|
||
<p> | ||
|
||
**Write a function that sorts an array of numbers using the merge sort algorithm.** | ||
|
||
# Problem | ||
|
||
## Problem | ||
```javascript | ||
function mergeSort(arr) { | ||
// Your code here | ||
} | ||
``` | ||
|
||
# Solution | ||
|
||
```javascript | ||
function mergeSort(arr) { | ||
if (arr.length <= 1) { | ||
return arr; | ||
} | ||
function merge(left, right) { | ||
let result = []; | ||
let leftIndex = 0; | ||
let rightIndex = 0; | ||
|
||
while (leftIndex < left.length && rightIndex < right.length) { | ||
if (left[leftIndex] < right[rightIndex]) { | ||
result.push(left[leftIndex]); | ||
leftIndex++; | ||
} else { | ||
result.push(right[rightIndex]); | ||
rightIndex++; | ||
} | ||
} | ||
|
||
return result.concat(left.slice(leftIndex)).concat(right.slice(rightIndex)); | ||
} | ||
|
||
if (arr.length <= 1) { | ||
return arr; | ||
} | ||
|
||
const mid = Math.floor(arr.length / 2); | ||
const left = arr.slice(0, mid); | ||
const right = arr.slice(mid); | ||
|
||
return merge(mergeSort(left), mergeSort(right)); | ||
let leftArr = mergeSort(left); | ||
let rightArr = mergeSort(right); | ||
|
||
return merge(leftArr, rightArr); | ||
} | ||
``` | ||
|
||
function merge(left, right) { | ||
let result = [], leftIndex = 0, rightIndex = 0; | ||
# Test Cases | ||
|
||
while (leftIndex < left.length && rightIndex < right.length) { | ||
if (left[leftIndex] < right[rightIndex]) { | ||
result.push(left[leftIndex]); | ||
leftIndex++; | ||
} else { | ||
result.push(right[rightIndex]); | ||
rightIndex++; | ||
} | ||
} | ||
```javascript | ||
let arr = [38, 27, 43, 3, 9, 82, 10]; | ||
mergeSort(arr) | ||
``` | ||
|
||
return result.concat(left.slice(leftIndex)).concat(right.slice(rightIndex)); | ||
} | ||
console.log(mergeSort([38, 27, 43, 3, 9, 82, 10])); // [3, 9, 10, 27, 38, 43, 82] | ||
console.log(mergeSort([5, 3, 8, 4, 2, 6, 1, 7])); // [1, 2, 3, 4, 5, 6, 7, 8] | ||
console.log(mergeSort([1, 2, 3, 4, 5, 6, 7, 8])); // [1, 2, 3, 4, 5, 6, 7, 8] | ||
console.log(mergeSort([8, 7, 6, 5, 4, 3, 2, 1])); // [1, 2, 3, 4, 5, 6, 7, 8] | ||
```javascript | ||
let arr = [-12, 3, 0, 9, 1, 2, 5, 4, 6, 8, 7, 10, 11, 13, 12, 15, 14]; | ||
mergeSort(arr); | ||
``` | ||
|
||
```javascript | ||
let arr = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; | ||
mergeSort(arr) | ||
``` | ||
|
||
```javascript | ||
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
mergeSort(arr) | ||
``` | ||
|
||
```javascript | ||
let arr = [1000, 354, 23, 565, 10, -20, 3, 4, 6, 1004, 35, 453, 14, 657]; | ||
mergeSort(arr) | ||
``` | ||
|
||
# Hidden Test Cases | ||
|
||
```javascript | ||
let len = Math.floor(Math.random() * 25 + 1); | ||
let arr = Array.from({length: len}, () => Math.floor(Math.random() * 1000)); | ||
mergeSort(arr); | ||
``` | ||
|
||
let random = Math.floor(Math.random() * 100) - 50; | ||
console.log(mergeSort(Array.from({length: random}, () => Math.floor(Math.random() * 100)))); | ||
repeat = 1 |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.