Skip to content

Commit 11a43e4

Browse files
authored
Merge pull request #1 from WaithakaGuru/SelectionSort-Edit
Refactor the code and removed lazy comments and unnecessary functions
2 parents 63ddf96 + d9d920a commit 11a43e4

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

bubbleSort.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// on 26th August 2024 [12:06 PM ]
2-
//function to perform bubble sort
32

43
// a simple function to return the length of an iterable eg an ARRAY, STRING, e.t.c
54
function count(iterable){
@@ -10,7 +9,6 @@ function count(iterable){
109
}
1110

1211
/* the bubble sort logic function*/
13-
1412
function bubbleSort(array){
1513
let iterations = sort(array), iterationsCount =[];
1614
while(iterations.swaps > 0){
@@ -31,8 +29,11 @@ function bubbleSort(array){
3129
}
3230
}
3331

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
32+
/**
33+
* Testing the Algo with a normal unordered, ordered, reverse ordered lists
34+
* and a list with only one misplaced element
35+
*/
36+
console.log(bubbleSort([22,3,44,5,6,8,7,10,9,15]));
37+
console.log(bubbleSort([1,3,2,4,4,5,6,7,8,9]));
38+
console.log(bubbleSort([13,12,11,9,8,7,6,5,3,2]));
39+
console.log(bubbleSort([10,11,22,33,44,55,66,77,88,99]));

insertionSort.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// On the evening of 26th August 2024
2-
// creating an insertion Sort algorithm
32

43
// a function to get the length of an iterable eg ARRAY, STRING e.t.c
54
function count (iterable){
@@ -8,6 +7,7 @@ function count (iterable){
87
for(const item of iterable) i++;
98
return i;
109
}
10+
1111
// the Insertion sort logic
1212
function insertionSort(array){
1313
let sorted= [], len = count(array), temp, swaps = 0, swapsCount= [], iterations = 0;

quickSort.js

+11-16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
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)
1+
// fn to calc the length of a list of items
52
function count(iterable){
63
if(!isNaN(iterable)) iterable = `${iterable}`;
74
let i = 0;
85
for(const item of iterable) i++;
96
return i;
107
}
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
8+
9+
// quick sort Algorithm (it's a recursive one) On 28th August 2024 [ 10:47 PM ]
1810
function quickSort(list=[], start =0 ,end= count(list) - 1){
1911
if(start < end){
2012
let pivotIndex = partition(list, start, end);
@@ -35,8 +27,11 @@ function partition(list, start, end){
3527
return i+1;
3628
}
3729

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
30+
/**
31+
* Testing the Algo with a normal unordered, ordered, reverse ordered lists
32+
* and a list with only one misplaced element
33+
*/
34+
console.log(quickSort([1,3,2,4,4,5,6,7,8,9]));
35+
console.log(quickSort([13,12,11,9,8,7,6,5,3,2]));
36+
console.log(quickSort([10,11,22,33,44,55,66,77,88,99]));
37+
console.log(quickSort([22,3,44,5,6,8,7,10,9,15]));

selectionSort.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
console.log("Still on 31st July" + " Let's continue coding");
2-
/* --SELECTION SORT--
3-
*
4-
*
5-
*/
1+
// --SELECTION SORT-
62

7-
// fn to give the length of the List of items (i.e no of items)
83
function count(iterable){
94
if(!isNaN(iterable)) iterable = `${iterable}`;
105
let i = 0;
@@ -28,7 +23,11 @@ function selectionSort (list){
2823
}
2924
return [list, `No of checks done per iteration: ${checksCount}`, `No of iterations for complete sort ${iterations}`]
3025
}
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
26+
/**
27+
* Testing the Algo with a normal unordered, ordered, reverse ordered lists
28+
* and a list with only one misplaced element
29+
*/
30+
console.log(selectionSort([1,3,2,4,4,5,6,7,8,9]));
31+
console.log(selectionSort([13,12,11,9,8,7,6,5,3,2]));
32+
console.log(selectionSort([10,11,22,33,44,55,66,77,88,99]));
33+
console.log(selectionSort([22,3,44,5,6,8,7,10,9,15]));

0 commit comments

Comments
 (0)