Skip to content

Commit 63ddf96

Browse files
committed
Add a Folder with Sorting algorithms in js
- add a bubble sort algorithm - add an insertion sort algorithm - add a quick sort algorithm - add a selection sort algorithm
0 parents  commit 63ddf96

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

bubbleSort.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// on 26th August 2024 [12:06 PM ]
2+
//function to perform bubble sort
3+
4+
// a simple function to return the length of an iterable eg an ARRAY, STRING, e.t.c
5+
function count(iterable){
6+
if(!isNaN(iterable)) iterable = `${iterable}`;
7+
let i =0;
8+
for(const item of iterable) i++;
9+
return i;
10+
}
11+
12+
/* the bubble sort logic function*/
13+
14+
function bubbleSort(array){
15+
let iterations = sort(array), iterationsCount =[];
16+
while(iterations.swaps > 0){
17+
iterationsCount.push(iterations.swaps);
18+
iterations = sort(iterations.array);
19+
}
20+
return [iterations.array, `Nos. of swaps made per Iteration: ${iterationsCount}`,
21+
`Total iterations(Sort Pass) made to sort the List: ${count(iterationsCount)}`, `List of ${count(array)} elements`];
22+
function sort (array){
23+
let len = count(array), c=0;
24+
for(let i=len; i>=0; i--){
25+
if(array[i]<array[i-1]){
26+
[array[i], array[i-1]] = [array[i-1], array[i]]; // swapping the items positions
27+
c++;
28+
}
29+
}
30+
return {swaps:c, array};
31+
}
32+
}
33+
34+
//testing the function
35+
console.log(bubbleSort([22,3,44,5,6,8,7,10,9,15]));// normal list
36+
console.log(bubbleSort([1,3,2,4,4,5,6,7,8,9])); // list with only one misplaced element
37+
console.log(bubbleSort([13,12,11,9,8,7,6,5,3,2])); // a reverse-ordered list
38+
console.log(bubbleSort([10,11,22,33,44,55,66,77,88,99])); // a sorted list

insertionSort.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// On the evening of 26th August 2024
2+
// creating an insertion Sort algorithm
3+
4+
// a function to get the length of an iterable eg ARRAY, STRING e.t.c
5+
function count (iterable){
6+
if(!isNaN(iterable))iterable = `${iterable}`;
7+
let i= 0;
8+
for(const item of iterable) i++;
9+
return i;
10+
}
11+
// the Insertion sort logic
12+
function insertionSort(array){
13+
let sorted= [], len = count(array), temp, swaps = 0, swapsCount= [], iterations = 0;
14+
sorted[0] = array[0];
15+
for(let i = 1; i<len; i++){
16+
for(let j = 0; j<count(sorted); j++){
17+
if(array[i]<sorted[j]){
18+
[array[i], sorted[j]] = [sorted[j], array[i]];
19+
swaps++;
20+
}
21+
}
22+
sorted.push(array[i]);
23+
swapsCount.push(swaps);
24+
iterations ++;
25+
}
26+
return[sorted, `No of swaps per iteration ${swapsCount}`, `No of iterations made to complete the sort ${iterations}`]
27+
}
28+
29+
console.log(insertionSort([22,3,44,5,6,8,7,10,9,15]));// normal list
30+
console.log(insertionSort([1,3,2,4,4,5,6,7,8,9])); // list with only one misplaced element
31+
console.log(insertionSort([13,12,11,9,8,7,6,5,3,2])); // a reverse-ordered list
32+
console.log(insertionSort([10,11,22,33,44,55,66,77,88,99])); // a sorted list

quickSort.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Now into Advanced sorting algorithms am going to code the
2+
// quick sort Algorithm (it's a recursive one) On 28th August 2024 [ 10:47 PM ]
3+
import { quickSort } from "./allSortAlogs";
4+
// fn to calc the length of a list of items ie(no of items in a list)
5+
function count(iterable){
6+
if(!isNaN(iterable)) iterable = `${iterable}`;
7+
let i = 0;
8+
for(const item of iterable) i++;
9+
return i;
10+
}
11+
function rangeCount(iterable, start, end){
12+
if(!isNaN(iterable)) iterable = `${iterable}`;
13+
let i = 0;
14+
for( j = start; j<=end; j++) i++;
15+
return i;
16+
}
17+
// quick sort logic
18+
function quickSort(list=[], start =0 ,end= count(list) - 1){
19+
if(start < end){
20+
let pivotIndex = partition(list, start, end);
21+
quickSort(list, start, pivotIndex-1);
22+
quickSort(list, pivotIndex+1, end);
23+
}
24+
return list;
25+
}
26+
function partition(list, start, end){
27+
let pivot = list[end], i = start -1;
28+
for(let k = start; k<end; k++){
29+
if(list[k] < pivot){
30+
i++;
31+
[list[i], list[k]] = [list[k], list[i]];
32+
}
33+
}
34+
[list[i+1], list[end]] = [list[end],list[i+1]];
35+
return i+1;
36+
}
37+
38+
// testing the function
39+
console.log(quickSort([1,3,2,4,4,5,6,7,8,9])); // list with only one misplaced element
40+
console.log(quickSort([13,12,11,9,8,7,6,5,3,2])); // a reverse-ordered list
41+
console.log(quickSort([10,11,22,33,44,55,66,77,88,99])); // a sorted list
42+
console.log(quickSort([22,3,44,5,6,8,7,10,9,15]));// normal list

selectionSort.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
console.log("Still on 31st July" + " Let's continue coding");
2+
/* --SELECTION SORT--
3+
*
4+
*
5+
*/
6+
7+
// fn to give the length of the List of items (i.e no of items)
8+
function count(iterable){
9+
if(!isNaN(iterable)) iterable = `${iterable}`;
10+
let i = 0;
11+
for(const item of iterable) i++;
12+
return i;
13+
}
14+
15+
// selection sort logic
16+
function selectionSort (list){
17+
let posOfLargest, len = count(list), checks = 0, iterations = 0, checksCount= [];
18+
for(let i = len-1; i>0; i--){
19+
posOfLargest = 0;
20+
for(let j = 0; j<=i; j++){
21+
if(list[j]>list[posOfLargest]) {
22+
posOfLargest = j; checks++;
23+
}
24+
}
25+
checksCount.push(checks);
26+
[list[i], list[posOfLargest]] = [list[posOfLargest], list[i]];
27+
iterations ++;
28+
}
29+
return [list, `No of checks done per iteration: ${checksCount}`, `No of iterations for complete sort ${iterations}`]
30+
}
31+
console.log(selectionSort([1,3,2,4,4,5,6,7,8,9])); // list with only one misplaced element
32+
console.log(selectionSort([13,12,11,9,8,7,6,5,3,2])); // a reverse-ordered list
33+
console.log(selectionSort([10,11,22,33,44,55,66,77,88,99])); // a sorted list
34+
console.log(selectionSort([22,3,44,5,6,8,7,10,9,15]));// normal list

0 commit comments

Comments
 (0)