Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
varun7654 committed May 6, 2024
1 parent 7334f81 commit b20b455
Show file tree
Hide file tree
Showing 40 changed files with 201 additions and 93 deletions.
2 changes: 1 addition & 1 deletion 404.html
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>
38 changes: 28 additions & 10 deletions asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
{
"files": {
"main.css": "/static/css/main.8bfa3784.css",
"main.js": "/static/js/main.b3c6d50b.js",
"main.css": "/static/css/main.fb5a12ad.css",
"main.js": "/static/js/main.4ee7ae0e.js",
"static/js/221.ed47e118.chunk.js": "/static/js/221.ed47e118.chunk.js",
"static/js/853.7ebfdb21.chunk.js": "/static/js/853.7ebfdb21.chunk.js",
"static/js/843.815f935c.chunk.js": "/static/js/843.815f935c.chunk.js",
"static/js/488.a8d036df.chunk.js": "/static/js/488.a8d036df.chunk.js",
"static/js/780.b463502c.chunk.js": "/static/js/780.b463502c.chunk.js",
"static/js/369.fd33c645.chunk.js": "/static/js/369.fd33c645.chunk.js",
"static/js/780.c1ef71a3.chunk.js": "/static/js/780.c1ef71a3.chunk.js",
"static/js/303.87c5ac7d.chunk.js": "/static/js/303.87c5ac7d.chunk.js",
"static/js/20.d5160684.chunk.js": "/static/js/20.d5160684.chunk.js",
"static/js/369.b56a4978.chunk.js": "/static/js/369.b56a4978.chunk.js",
"static/css/913.8137372e.chunk.css": "/static/css/913.8137372e.chunk.css",
"static/js/913.55d254bb.chunk.js": "/static/js/913.55d254bb.chunk.js",
"static/js/473.66bba9da.chunk.js": "/static/js/473.66bba9da.chunk.js",
"static/js/671.5426ab3a.chunk.js": "/static/js/671.5426ab3a.chunk.js",
"static/media/KaTeX_AMS-Regular.ttf": "/static/media/KaTeX_AMS-Regular.853be92419a6c3766b9a.ttf",
"static/media/KaTeX_Main-Regular.ttf": "/static/media/KaTeX_Main-Regular.9eba1d77abcf2aa6e94e.ttf",
"static/media/KaTeX_Main-Bold.ttf": "/static/media/KaTeX_Main-Bold.8169508bf58f8bd92ad8.ttf",
Expand Down Expand Up @@ -67,14 +76,23 @@
"static/media/KaTeX_Size3-Regular.woff2": "/static/media/KaTeX_Size3-Regular.e1951519f6f0596f7356.woff2",
"static/media/github-mark-white.svg": "/static/media/github-mark-white.ac7f5ce28096dc43a49cbedeabffa77b.svg",
"index.html": "/index.html",
"main.8bfa3784.css.map": "/static/css/main.8bfa3784.css.map",
"main.b3c6d50b.js.map": "/static/js/main.b3c6d50b.js.map",
"main.fb5a12ad.css.map": "/static/css/main.fb5a12ad.css.map",
"main.4ee7ae0e.js.map": "/static/js/main.4ee7ae0e.js.map",
"221.ed47e118.chunk.js.map": "/static/js/221.ed47e118.chunk.js.map",
"853.7ebfdb21.chunk.js.map": "/static/js/853.7ebfdb21.chunk.js.map",
"843.815f935c.chunk.js.map": "/static/js/843.815f935c.chunk.js.map",
"488.a8d036df.chunk.js.map": "/static/js/488.a8d036df.chunk.js.map",
"780.b463502c.chunk.js.map": "/static/js/780.b463502c.chunk.js.map",
"369.fd33c645.chunk.js.map": "/static/js/369.fd33c645.chunk.js.map"
"780.c1ef71a3.chunk.js.map": "/static/js/780.c1ef71a3.chunk.js.map",
"303.87c5ac7d.chunk.js.map": "/static/js/303.87c5ac7d.chunk.js.map",
"20.d5160684.chunk.js.map": "/static/js/20.d5160684.chunk.js.map",
"369.b56a4978.chunk.js.map": "/static/js/369.b56a4978.chunk.js.map",
"913.8137372e.chunk.css.map": "/static/css/913.8137372e.chunk.css.map",
"913.55d254bb.chunk.js.map": "/static/js/913.55d254bb.chunk.js.map",
"473.66bba9da.chunk.js.map": "/static/js/473.66bba9da.chunk.js.map",
"671.5426ab3a.chunk.js.map": "/static/js/671.5426ab3a.chunk.js.map"
},
"entrypoints": [
"static/css/main.8bfa3784.css",
"static/js/main.b3c6d50b.js"
"static/css/main.fb5a12ad.css",
"static/js/main.4ee7ae0e.js"
]
}
2 changes: 1 addition & 1 deletion index.html
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>
121 changes: 92 additions & 29 deletions problems/medium/mergesort.md
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
2 changes: 2 additions & 0 deletions static/css/913.8137372e.chunk.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions static/css/913.8137372e.chunk.css.map

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions static/css/main.8bfa3784.css

This file was deleted.

1 change: 0 additions & 1 deletion static/css/main.8bfa3784.css.map

This file was deleted.

Loading

0 comments on commit b20b455

Please sign in to comment.