Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comments added about all sorting algo #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion DSA/Arrays/BubbleSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@
var arr = [234, 43, 55, 63, 5, 6, 235, 547];

// call the function
bblSort(arr);
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.
8 changes: 8 additions & 0 deletions DSA/Arrays/BucketSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
7 changes: 7 additions & 0 deletions DSA/Arrays/Maximumsubarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
9 changes: 8 additions & 1 deletion DSA/Arrays/RadixSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ const radixSort = (arr, size = arr.length) => {
}
}
radixSort(arr);
console.log(arr);
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.
8 changes: 7 additions & 1 deletion DSA/Arrays/SelectionSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ function selectionSort(arr) {
}

let arr = [4, 12, 10, 15, 2]
console.log(selectionSort(arr))
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.
7 changes: 6 additions & 1 deletion DSA/Arrays/SpecialArrayWithXElementsGreaterThanEqualToX.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@
}
return -1

};
};

// complexities for this special array problem:

// Time Complexity: O(n2)
// Space Complexity: O(1)