Movatterモバイル変換


[0]ホーム

URL:


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

Commitba69d9d

Browse files
committed
Adding Bubble, Insertion & Selection Sort
1 parent7534860 commitba69d9d

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+
<palign="center">
4+
<ahref="https://youtube.com/live/FAPg2VM6N9I">
5+
<imgsrc="https://img.youtube.com/vi/FAPg2VM6N9I/0.jpg"alt="Sorting In JavaScript" />
6+
</a>
7+
</p>
8+
9+
###Sort an Array
10+
```javascript
11+
constarr= [-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+
conststrArr= ["mango","apple","banana"]
17+
console.log(strArr.sort())// "apple", "banana", "mango"
18+
```
19+
20+
###Sort a String
21+
```javascript
22+
conststr="Vishal"
23+
console.log(str.split("").sort().join(""))// "Vahils
24+
```
25+
26+
###Bubble Sort In JavaScript
27+
```javascript
28+
constbubbleSort= (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+
constselectionSort= (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+
constinsertionSort= (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+
constarr=[-2,-7,1000,5]
5+
console.log(arr.sort((a,b)=>a-b))
6+
console.log(arr.sort((a,b)=>b-a))
7+
8+
conststrArr=["mango","apple","banana"]
9+
console.log(strArr.sort())
10+
11+
// Sort a String
12+
conststr="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+
constbubbleSort=(arr)=>{
20+
letswapped,swapCount=0;
21+
do{
22+
swapped=false;
23+
for(leti=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+
returnarr;
36+
}
37+
38+
console.log(bubbleSort(arr))
39+
40+
// Selection sort in JavaScript
41+
42+
constselectionSort=(arr)=>{
43+
for(leti=0;i<arr.length-1;i++){
44+
letminIndex=i;
45+
for(letj=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+
returnarr;
53+
}
54+
55+
console.log(selectionSort(arr))
56+
57+
// Insertion Sort In JavaScript
58+
59+
constinsertionSort=(arr)=>{
60+
for(leti=1;i<arr.length;i++){
61+
letcurrent=arr[i];
62+
letj=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+
returnarr;
70+
}
71+
72+
console.log(insertionSort(arr))
73+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp