From 451b9cebbf8e54f2cb4b15faa728b6af04219d50 Mon Sep 17 00:00:00 2001 From: adis21104 <210305124162@paruluniversity.ac.in> Date: Sat, 28 Oct 2023 13:39:41 +0530 Subject: [PATCH] Comments added about all sorting algo --- DSA/Arrays/BubbleSort.js | 8 +++++++- DSA/Arrays/BucketSort.js | 8 ++++++++ DSA/Arrays/Maximumsubarray.js | 7 +++++++ DSA/Arrays/RadixSort.js | 9 ++++++++- DSA/Arrays/SelectionSort.js | 8 +++++++- .../SpecialArrayWithXElementsGreaterThanEqualToX.js | 7 ++++++- 6 files changed, 43 insertions(+), 4 deletions(-) diff --git a/DSA/Arrays/BubbleSort.js b/DSA/Arrays/BubbleSort.js index c37a9d6..f687949 100644 --- a/DSA/Arrays/BubbleSort.js +++ b/DSA/Arrays/BubbleSort.js @@ -21,4 +21,10 @@ var arr = [234, 43, 55, 63, 5, 6, 235, 547]; // call the function -bblSort(arr); \ No newline at end of file +bblSort(arr); + +// In bubble sort we compare the adjacent elements and swap them if they are in wrong order. +// The above code will sort the array in ascending order. +// Time Complexity: O(n2) +// Space Complexity: O(1) +// In bubble sort, n-1 comparisons will be done in the 1st pass, n-2 in 2nd pass, n-3 in 3rd pass and so on. \ No newline at end of file diff --git a/DSA/Arrays/BucketSort.js b/DSA/Arrays/BucketSort.js index 52ad644..0f865b1 100644 --- a/DSA/Arrays/BucketSort.js +++ b/DSA/Arrays/BucketSort.js @@ -46,3 +46,11 @@ const insertion = (arr) => { return arr; }; console.log(bucketSort(arr)); + +// In bucket sort, we distribute the elements of the array into a number of buckets. +// Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. +// The above code will sort the array in ascending order. +// Time Complexity: O(n+k) +// Space Complexity: O(n+k) +// where n is the number of elements in the array and k is the number of buckets. +// In bucket sort, n elements are distributed into k buckets. Then, the elements of each bucket are sorted using any other algorithm. Finally, the elements are gathered from the buckets. diff --git a/DSA/Arrays/Maximumsubarray.js b/DSA/Arrays/Maximumsubarray.js index fc2247e..769fda6 100644 --- a/DSA/Arrays/Maximumsubarray.js +++ b/DSA/Arrays/Maximumsubarray.js @@ -10,3 +10,10 @@ var maxSubArray = function(nums) { } return max; }; + + +// In the above code, we are using Kadane's algorithm to find the maximum sum of contiguous subarray. +// Time Complexity: O(n) +// Space Complexity: O(1) +// where n is the number of elements in the array. +// The idea is to maintain a contiguous sum of the array and update the maximum sum obtained so far. diff --git a/DSA/Arrays/RadixSort.js b/DSA/Arrays/RadixSort.js index e4f1b5b..e726647 100644 --- a/DSA/Arrays/RadixSort.js +++ b/DSA/Arrays/RadixSort.js @@ -40,4 +40,11 @@ const radixSort = (arr, size = arr.length) => { } } radixSort(arr); -console.log(arr); \ No newline at end of file +console.log(arr); + +// In counting sort, we count the number of elements having distinct key values (smaller than some integer k) and use arithmetic to determine the position of each key value in the output sequence. +// The above code will sort the array in ascending order. +// Time Complexity: O(n+k) +// Space Complexity: O(n+k) +// where n is the number of elements in the array and k is the range of input. +// In counting sort, k is the range of input. So, it is not suitable for sorting large integers. \ No newline at end of file diff --git a/DSA/Arrays/SelectionSort.js b/DSA/Arrays/SelectionSort.js index 76f4cbb..3699675 100644 --- a/DSA/Arrays/SelectionSort.js +++ b/DSA/Arrays/SelectionSort.js @@ -15,4 +15,10 @@ function selectionSort(arr) { } let arr = [4, 12, 10, 15, 2] -console.log(selectionSort(arr)) \ No newline at end of file +console.log(selectionSort(arr)); + +// In selection sort, we find the minimum element and place it in the beginning of the array. +// The above code will sort the array in descending order. +// Time Complexity: O(n2) +// Space Complexity: O(1) +// where n is the number of elements in the array. \ No newline at end of file diff --git a/DSA/Arrays/SpecialArrayWithXElementsGreaterThanEqualToX.js b/DSA/Arrays/SpecialArrayWithXElementsGreaterThanEqualToX.js index 5e995a2..38819ae 100644 --- a/DSA/Arrays/SpecialArrayWithXElementsGreaterThanEqualToX.js +++ b/DSA/Arrays/SpecialArrayWithXElementsGreaterThanEqualToX.js @@ -16,4 +16,9 @@ } return -1 -}; \ No newline at end of file +}; + +// complexities for this special array problem: + +// Time Complexity: O(n2) +// Space Complexity: O(1) \ No newline at end of file