Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5f9d85b

Browse files
committed
Q 95 - quicksort and recursive
1 parent cabf8b6 commit 5f9d85b

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

‎README.md‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ Of the three, only `bind()` can be executed after binding. We can create a varia
682682

683683
Note that `window.window.window.score` or `window.score` or simply `score` do the same thing. It points to the `score()` function in the global scope.
684684

685-
The correct anwser is D. The `score()` and `getAge()` functions are nothing special. The only tricky part is that this.age is incremented each time.
685+
The correct anwser is D. The `score()` and `getAge()` functions are nothing special. The only tricky part is that `this.age` is incremented each time you call the funtion `getAge()`;
686686

687687
</p>
688688
</details>
@@ -3949,7 +3949,60 @@ The `pipe` function can receive an unlimited number of arguments/parameters than
39493949
39503950
Please call these functions, which are passed to `pipe`, are child functions. They are then looped and executed one by one with `reduce` method, no matter how many functions you attempt to pass to `pipe`. `v` in the code is simply the argument defined in each child function.
39513951
3952-
So first we have 1, then by executing `plusFour` it becomes 5. When `multiplyBySix` is called, the output turns to 30. It becomes 15 when we call `divideByTwo`. Finally, it becomes 90 as we multiply 15 * 6 when the function `multiplyBySix` is called again.
3952+
So first we have 1, then by executing `plusFour` it becomes 5. When `multiplyBySix` is called, the output turns to 30. It becomes 15 when we call `divideByTwo`. Finally, it becomes 90 as we multiply 15 \* 6 when the function `multiplyBySix` is called again.
3953+
3954+
So B is the correct answer.
3955+
3956+
<!-- Credit: https://www.freecodecamp.org/news/pipe-and-compose-in-javascript-5b04004ac937/ -->
3957+
3958+
#### Answer: B
3959+
3960+
</p>
3961+
</details>
3962+
3963+
###### 95. What's the output ?
3964+
3965+
```javascript
3966+
const quickSortRecursive = function (arrayInput) {
3967+
if (!Array.isArray(arrayInput)) {
3968+
console.log("The input data is not an array");
3969+
return arrayInput;
3970+
}
3971+
const pivotIndex = arrayInput.length - 1;
3972+
const pivot = arrayInput[pivotIndex];
3973+
const left = [];
3974+
const right = [];
3975+
let currentItem;
3976+
for (let i = 0; i < pivotIndex; i++) {
3977+
currentItem = arrayInput[i];
3978+
if (currentItem < pivot) {
3979+
left.push(currentItem);
3980+
} else {
3981+
right.push(currentItem);
3982+
}
3983+
}
3984+
3985+
return [...quickSortRecursive(left), pivot, ...quickSortRecursive(right)];
3986+
};
3987+
3988+
console.log(quickSortRecursive([1, 100, 8, 19, 8, 6]));
3989+
```
3990+
3991+
- A: [1, 100, 8, 19, 8, 6]
3992+
- B: [1, 6, 8, 8, 19, 100]
3993+
- C: [100, 19, 8, 8, 6, 1]
3994+
- D: 6
3995+
3996+
<details><summary><b>Answer</b></summary>
3997+
<p>
3998+
3999+
You might see a commonly used algorithm here in the code challenge called "quicksort" in which we apply the strategy "divide and conquer". We also use the "recursive" method when we want to recall the function until it meets our expectations. You might also need to know about the "rest parameter" in JavaScript, as shown by the three dots (...) above.
4000+
4001+
The code above helps us to arrange an array in such a way that the value will increase from left to right. Using the quicksort method, we need to create a pivot (likely the first item from right to left or the first item from left to right). First, we divide the original array into two parts: left and right, depending on the value compared to the pivot.
4002+
4003+
Next, by calling the function recursively, we keep creating new pivots for the right and left arrays created above for the purpose of sorting value.
4004+
4005+
Finally, the original array is sorted from left to right depending on the value.
39534006
39544007
So B is the correct answer.
39554008

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /