Skip to content

Commit a4cdf89

Browse files
committed
Adding Merge and Quick Sort
1 parent ba69d9d commit a4cdf89

File tree

2 files changed

+130
-74
lines changed

2 files changed

+130
-74
lines changed

Sorting/README.md

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
</a>
77
</p>
88

9+
<p align="center">
10+
<a href="https://youtu.be/4n90F6PCQ6A">
11+
<img src="https://img.youtube.com/vi/4n90F6PCQ6A/0.jpg" alt="Sorting In JavaScript" />
12+
</a>
13+
</p>
14+
915
### Sort an Array
1016
```javascript
1117
const arr = [-2, -7, 1000, 5]
@@ -76,4 +82,127 @@ const insertionSort = (arr) => {
7682
}
7783

7884
console.log(insertionSort(arr)) // -7, -2 , 5, 1000
79-
```
85+
```
86+
87+
### Merge Sort in JavaScript
88+
```javascript
89+
const mergeSort = (arr) => {
90+
if (arr.length < 2) {
91+
return arr;
92+
}
93+
let mid = Math.floor(arr.length / 2);
94+
let left = mergeSort(arr.slice(0, mid))
95+
let right = mergeSort(arr.slice(mid))
96+
return merge(left, right)
97+
}
98+
99+
const merge = (left, right) => {
100+
const result = []
101+
let leftIndex = 0, rightIndex = 0;
102+
while (leftIndex < left.length && rightIndex < right.length) {
103+
if (left[leftIndex] < right[rightIndex]) {
104+
result.push(left[leftIndex])
105+
leftIndex++;
106+
}
107+
else {
108+
result.push(right[rightIndex])
109+
rightIndex++;
110+
}
111+
}
112+
113+
while (leftIndex < left.length) {
114+
result.push(left[leftIndex])
115+
leftIndex++;
116+
}
117+
118+
while (rightIndex < right.length) {
119+
result.push(right[rightIndex])
120+
rightIndex++;
121+
}
122+
123+
return result;
124+
}
125+
126+
const arr1 = [29, 10, 8, 16, 37, 14, 4, 45]
127+
console.log(mergeSort(arr1))
128+
```
129+
### Merge Sort in JavaScript (Space Optimised)
130+
131+
```javascript
132+
const mergeSortInplace = (arr, low, high) => {
133+
if (low < high) {
134+
let mid = Math.floor((low + high) / 2);
135+
mergeSortInplace(arr, low, mid)
136+
mergeSortInplace(arr, mid + 1, high)
137+
mergeInplace(arr, low, mid, high)
138+
}
139+
}
140+
141+
const mergeInplace = (arr, low, mid, high) => {
142+
const result = []
143+
let leftIndex = low, rightIndex = mid + 1;
144+
while (leftIndex <= mid && rightIndex <= high) {
145+
if (arr[leftIndex] < arr[rightIndex]) {
146+
result.push(arr[leftIndex])
147+
leftIndex++;
148+
}
149+
else {
150+
result.push(arr[rightIndex])
151+
rightIndex++;
152+
}
153+
}
154+
155+
while (leftIndex <= mid) {
156+
result.push(arr[leftIndex])
157+
leftIndex++;
158+
}
159+
160+
while (rightIndex <= high) {
161+
result.push(arr[rightIndex])
162+
rightIndex++;
163+
}
164+
165+
for (let i = low; i <= high; i++) {
166+
arr[i] = result[i - low];
167+
}
168+
}
169+
170+
const arr1 = [29, 10, 8, 16, 37, 14, 4, 45]
171+
console.log(mergeSortInplace(arr1, 0, arr.length - 1))
172+
console.log(arr1)
173+
```
174+
175+
### Quick Sort in JavaScript
176+
177+
```javascript
178+
const quickSort = (arr) => {
179+
if(arr.length < 2){
180+
return arr;
181+
}
182+
let pivotIndex = Math.floor(Math.random() * arr.length);
183+
let left = [], right = [];
184+
for(let i=0; i<arr.length; i++){
185+
if(i === pivotIndex)
186+
continue;
187+
188+
if(arr[i] < arr[pivotIndex]){
189+
left.push(arr[i])
190+
}
191+
else{
192+
right.push(arr[i])
193+
}
194+
}
195+
196+
return [...quickSort(left), arr[pivotIndex], ...quickSort(right)]
197+
}
198+
199+
console.log(quickSort(arr1))
200+
```
201+
202+
## Practice Question
203+
204+
- [How Many Numbers are smaller than the current number](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number)
205+
- [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array)
206+
- [Sort an Array](https://leetcode.com/problems/sort-an-array)
207+
- [Largest Number](https://leetcode.com/problems/largest-number)
208+
- [Sort Color](https://leetcode.com/problems/sort-colors)

Sorting/index.js

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)