Skip to content

Commit 8a6b431

Browse files
committed
chore: updated implmentation
1 parent b029831 commit 8a6b431

File tree

2 files changed

+79
-11
lines changed

2 files changed

+79
-11
lines changed

README.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
- [Stack](#stack)
2121
- [Popping element from stack](#popping-element-from-stack)
2222
- [Queue](#queue)
23-
- [Dequeue an element from Queue](#dequeue-an-element-from-queue)
2423
- [Enqueue an element in Queue](#enqueue-an-element-in-queue)
24+
- [Dequeue an element from Queue](#dequeue-an-element-from-queue)
2525
- [Tree](#tree)
2626
- [Binary Tree](#binary-tree)
2727
- [Binary Search Tree (BST)](#binary-search-tree-bst)
@@ -38,6 +38,7 @@
3838
- [Merge Sort Algorithm Simulator](#merge-sort-algorithm-simulator)
3939
- [Implement Merge Sort](#implement-merge-sort)
4040
- [Find Median Values (With Merge Sort Algorithm)](#find-median-values-with-merge-sort-algorithm)
41+
- [Quick Sort](#quick-sort)
4142
- [BFS (Breath First Search)](#bfs-breath-first-search)
4243
- [Math & Stats](#math--stats)
4344
- [Integer Division Without Using \* or /](#integer-division-without-using--or-)
@@ -238,6 +239,16 @@ stack.pop(); // 1 , stack []
238239

239240
Breadth First Search (BFS) uses a `queue` for storing the nodes that it is visiting.
240241

242+
#### Enqueue an element in Queue
243+
244+
```ts
245+
var queue = [];
246+
247+
queue.push(1); // queue [1]
248+
queue.push(2); // queue [1,2]
249+
queue.push(3); // queue [1,2,3]
250+
```
251+
241252
#### Dequeue an element from Queue
242253

243254
```ts
@@ -248,16 +259,6 @@ queue.shift(); // 3 , queue [4]
248259
queue.shift(); // 4 , queue []
249260
```
250261

251-
#### Enqueue an element in Queue
252-
253-
```ts
254-
var queue = [];
255-
256-
queue.unshift(1); // queue [1]
257-
queue.unshift(2); // queue [2,1]
258-
queue.unshift(3); // queue [3,2,1]
259-
```
260-
261262
### Tree
262263

263264
A tree has hierarchical data and it has nodes.
@@ -385,6 +386,13 @@ So in order to find median we can use the stich algorithm since arrays are alrea
385386

386387
[Exercise File](src/sorting/merge-sort/find-median-values.mjs)
387388

389+
### Quick Sort
390+
391+
![](https://i.imgur.com/LudZhvH.png)
392+
393+
- [Implement Quick Sort Question](https://codepen.io/roopkt/pen/NWpzMRv?editors=0010)
394+
- [Implement Quick Sort Answer](https://codepen.io/roopkt/pen/eYvKrvP?editors=0010)
395+
388396
### BFS (Breath First Search)
389397

390398
## Math & Stats
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* [3,53,5,23]
3+
*
4+
* p = 23
5+
* l = [3,5]
6+
* r = [53]
7+
* [3,5,23,53]
8+
*
9+
* arr [3,5], p=5
10+
* l = [3]
11+
* r = []
12+
* [3,5]
13+
*
14+
* r = 53
15+
*/
16+
17+
function quickSort(arr) {
18+
if (arr.length <= 1) {
19+
return arr;
20+
}
21+
const pivot = arr[arr.length - 1];
22+
const [left, right] = splitArray(arr, pivot);
23+
24+
const leftSorted = quickSort(left);
25+
const rightSorted = quickSort(right);
26+
27+
const finalSorted = [...leftSorted, pivot, ...rightSorted];
28+
29+
return finalSorted;
30+
}
31+
32+
function splitArray(arr, pivot) {
33+
const left = [];
34+
const right = [];
35+
36+
for (var i = 0; i < arr.length; i++) {
37+
let element = arr[i];
38+
if (element < pivot) {
39+
left.push(element);
40+
}
41+
42+
if (element > pivot) {
43+
right.push(element);
44+
}
45+
}
46+
return [left, right];
47+
}
48+
49+
function trace(result, expected) {
50+
console.log('result:' + result);
51+
console.log('expected:' + expected);
52+
}
53+
54+
// Test
55+
console.log(quickSort([2, 4, 12, 42, 11, 48, 9, 10], 10));
56+
57+
trace(
58+
quickSort([2, 4, 12, 42, 11, 48, 9, 10], 10),
59+
[2, 4, 9, 10, 11, 12, 42, 48]
60+
);

0 commit comments

Comments
 (0)