Skip to content

Commit ba69d9d

Browse files
committed
Adding Bubble, Insertion & Selection Sort
1 parent 7534860 commit ba69d9d

File tree

3 files changed

+154
-1
lines changed

3 files changed

+154
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@
2929
- [String](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/String/README.md)
3030
- [Recursion](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Recursion/README.md)
3131
- [Linear & Binary Search](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Searching%20Algorthims/README.md)
32-
- [Objects](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Objects/README.md)
32+
- [Objects](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Objects/README.md)
33+
- [Sorting](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Sorting/README.md)

Sorting/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Sorting In JavaScript
2+
3+
<p align="center">
4+
<a href="https://youtube.com/live/FAPg2VM6N9I">
5+
<img src="https://img.youtube.com/vi/FAPg2VM6N9I/0.jpg" alt="Sorting In JavaScript" />
6+
</a>
7+
</p>
8+
9+
### Sort an Array
10+
```javascript
11+
const arr = [-2, -7, 1000, 5]
12+
console.log(arr.sort()) // -2, -7, 1000, 5
13+
console.log(arr.sort((a, b) => a - b)) // -7, -2 , 5, 1000
14+
console.log(arr.sort((a, b) => b - a)) // 1000, 5, -2, -7
15+
16+
const strArr = ["mango", "apple", "banana"]
17+
console.log(strArr.sort()) // "apple", "banana", "mango"
18+
```
19+
20+
### Sort a String
21+
```javascript
22+
const str = "Vishal"
23+
console.log(str.split("").sort().join("")) // "Vahils
24+
```
25+
26+
### Bubble Sort In JavaScript
27+
```javascript
28+
const bubbleSort = (arr) => {
29+
let swapped;
30+
do {
31+
swapped = false;
32+
for (let i = 0; i < arr.length - 1; i++) {
33+
if (arr[i] > arr[i + 1]) {
34+
[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]
35+
swapped = true;
36+
}
37+
}
38+
} while (swapped)
39+
return arr;
40+
}
41+
42+
console.log(bubbleSort(arr)) // -7, -2 , 5, 1000
43+
```
44+
45+
### Selection Sort in JavaScript
46+
```javascript
47+
const selectionSort = (arr) => {
48+
for (let i = 0; i < arr.length - 1; i++) {
49+
let minIndex = i;
50+
for (let j = i + 1; j < arr.length; j++) {
51+
if (arr[j] < arr[minIndex]) {
52+
minIndex = j;
53+
}
54+
}
55+
[arr[minIndex], arr[i]] = [arr[i], arr[minIndex]]
56+
}
57+
return arr;
58+
}
59+
60+
console.log(selectionSort(arr)) // -7, -2 , 5, 1000
61+
```
62+
63+
### Insertion Sort In JavaScript
64+
```javascript
65+
const insertionSort = (arr) => {
66+
for(let i=1; i<arr.length; i++){
67+
let current = arr[i];
68+
let j = i-1;
69+
while(j >= 0 && arr[j] > current){
70+
arr[j+1] = arr[j];
71+
j--;
72+
}
73+
arr[j+1] = current;
74+
}
75+
return arr;
76+
}
77+
78+
console.log(insertionSort(arr)) // -7, -2 , 5, 1000
79+
```

Sorting/index.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Sorting In JavaScript
2+
3+
// Sort an Array
4+
const arr = [-2, -7, 1000, 5]
5+
console.log(arr.sort((a, b) => a - b))
6+
console.log(arr.sort((a, b) => b - a))
7+
8+
const strArr = ["mango", "apple", "banana"]
9+
console.log(strArr.sort())
10+
11+
// Sort a String
12+
const str = "Vishal"
13+
14+
// Ascii value of A = 65 & a = 97
15+
console.log(str.split("").sort().join(""))
16+
17+
// Bubble Sort In JavaScript
18+
19+
const bubbleSort = (arr) => {
20+
let swapped, swapCount = 0;
21+
do {
22+
swapped = false;
23+
for (let i = 0; i < arr.length - 1; i++) {
24+
if (arr[i] > arr[i + 1]) {
25+
// let temp = arr[i];
26+
// arr[i] = arr[i+1];
27+
// arr[i+1] = temp;
28+
[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]
29+
swapped = true;
30+
swapCount++;
31+
}
32+
}
33+
} while (swapped)
34+
console.log(swapCount)
35+
return arr;
36+
}
37+
38+
console.log(bubbleSort(arr))
39+
40+
// Selection sort in JavaScript
41+
42+
const selectionSort = (arr) => {
43+
for (let i = 0; i < arr.length - 1; i++) {
44+
let minIndex = i;
45+
for (let j = i + 1; j < arr.length; j++) {
46+
if (arr[j] < arr[minIndex]) {
47+
minIndex = j;
48+
}
49+
}
50+
[arr[minIndex], arr[i]] = [arr[i], arr[minIndex]]
51+
}
52+
return arr;
53+
}
54+
55+
console.log(selectionSort(arr))
56+
57+
// Insertion Sort In JavaScript
58+
59+
const insertionSort = (arr) => {
60+
for(let i=1; i<arr.length; i++){
61+
let current = arr[i];
62+
let j = i-1;
63+
while(j >= 0 && arr[j] > current){
64+
arr[j+1] = arr[j];
65+
j--;
66+
}
67+
arr[j+1] = current;
68+
}
69+
return arr;
70+
}
71+
72+
console.log(insertionSort(arr))
73+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy