diff --git a/bubbleSort.js b/bubbleSort.js index 9a93a8e..1bd7f80 100644 --- a/bubbleSort.js +++ b/bubbleSort.js @@ -1,5 +1,4 @@ // on 26th August 2024 [12:06 PM ] -//function to perform bubble sort // a simple function to return the length of an iterable eg an ARRAY, STRING, e.t.c function count(iterable){ @@ -10,7 +9,6 @@ function count(iterable){ } /* the bubble sort logic function*/ - function bubbleSort(array){ let iterations = sort(array), iterationsCount =[]; while(iterations.swaps > 0){ @@ -31,8 +29,11 @@ function bubbleSort(array){ } } -//testing the function -console.log(bubbleSort([22,3,44,5,6,8,7,10,9,15]));// normal list -console.log(bubbleSort([1,3,2,4,4,5,6,7,8,9])); // list with only one misplaced element -console.log(bubbleSort([13,12,11,9,8,7,6,5,3,2])); // a reverse-ordered list -console.log(bubbleSort([10,11,22,33,44,55,66,77,88,99])); // a sorted list +/** + * Testing the Algo with a normal unordered, ordered, reverse ordered lists + * and a list with only one misplaced element + */ +console.log(bubbleSort([22,3,44,5,6,8,7,10,9,15])); +console.log(bubbleSort([1,3,2,4,4,5,6,7,8,9])); +console.log(bubbleSort([13,12,11,9,8,7,6,5,3,2])); +console.log(bubbleSort([10,11,22,33,44,55,66,77,88,99])); diff --git a/insertionSort.js b/insertionSort.js index ce4a5f8..12b3b21 100644 --- a/insertionSort.js +++ b/insertionSort.js @@ -1,5 +1,4 @@ // On the evening of 26th August 2024 -// creating an insertion Sort algorithm // a function to get the length of an iterable eg ARRAY, STRING e.t.c function count (iterable){ @@ -8,6 +7,7 @@ function count (iterable){ for(const item of iterable) i++; return i; } + // the Insertion sort logic function insertionSort(array){ let sorted= [], len = count(array), temp, swaps = 0, swapsCount= [], iterations = 0; diff --git a/quickSort.js b/quickSort.js index c93a49d..d2619ef 100644 --- a/quickSort.js +++ b/quickSort.js @@ -1,20 +1,12 @@ -// Now into Advanced sorting algorithms am going to code the -// quick sort Algorithm (it's a recursive one) On 28th August 2024 [ 10:47 PM ] -import { quickSort } from "./allSortAlogs"; -// fn to calc the length of a list of items ie(no of items in a list) +// fn to calc the length of a list of items function count(iterable){ if(!isNaN(iterable)) iterable = `${iterable}`; let i = 0; for(const item of iterable) i++; return i; } -function rangeCount(iterable, start, end){ - if(!isNaN(iterable)) iterable = `${iterable}`; - let i = 0; - for( j = start; j<=end; j++) i++; - return i; -} -// quick sort logic + +// quick sort Algorithm (it's a recursive one) On 28th August 2024 [ 10:47 PM ] function quickSort(list=[], start =0 ,end= count(list) - 1){ if(start < end){ let pivotIndex = partition(list, start, end); @@ -35,8 +27,11 @@ function partition(list, start, end){ return i+1; } -// testing the function -console.log(quickSort([1,3,2,4,4,5,6,7,8,9])); // list with only one misplaced element -console.log(quickSort([13,12,11,9,8,7,6,5,3,2])); // a reverse-ordered list -console.log(quickSort([10,11,22,33,44,55,66,77,88,99])); // a sorted list -console.log(quickSort([22,3,44,5,6,8,7,10,9,15]));// normal list +/** + * Testing the Algo with a normal unordered, ordered, reverse ordered lists + * and a list with only one misplaced element + */ +console.log(quickSort([1,3,2,4,4,5,6,7,8,9])); +console.log(quickSort([13,12,11,9,8,7,6,5,3,2])); +console.log(quickSort([10,11,22,33,44,55,66,77,88,99])); +console.log(quickSort([22,3,44,5,6,8,7,10,9,15])); diff --git a/selectionSort.js b/selectionSort.js index 7d5355f..e229709 100644 --- a/selectionSort.js +++ b/selectionSort.js @@ -1,10 +1,5 @@ -console.log("Still on 31st July" + " Let's continue coding"); -/* --SELECTION SORT-- - * - * - */ +// --SELECTION SORT- -// fn to give the length of the List of items (i.e no of items) function count(iterable){ if(!isNaN(iterable)) iterable = `${iterable}`; let i = 0; @@ -28,7 +23,11 @@ function selectionSort (list){ } return [list, `No of checks done per iteration: ${checksCount}`, `No of iterations for complete sort ${iterations}`] } -console.log(selectionSort([1,3,2,4,4,5,6,7,8,9])); // list with only one misplaced element -console.log(selectionSort([13,12,11,9,8,7,6,5,3,2])); // a reverse-ordered list -console.log(selectionSort([10,11,22,33,44,55,66,77,88,99])); // a sorted list -console.log(selectionSort([22,3,44,5,6,8,7,10,9,15]));// normal list \ No newline at end of file +/** + * Testing the Algo with a normal unordered, ordered, reverse ordered lists + * and a list with only one misplaced element + */ +console.log(selectionSort([1,3,2,4,4,5,6,7,8,9])); +console.log(selectionSort([13,12,11,9,8,7,6,5,3,2])); +console.log(selectionSort([10,11,22,33,44,55,66,77,88,99])); +console.log(selectionSort([22,3,44,5,6,8,7,10,9,15])); \ No newline at end of file