Skip to content
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
7 changes: 4 additions & 3 deletions search/jump_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
*/
int jumpSearch(int arr[], int x, int n) {
// Finding block size to be jumped
if(n <= 0) {return -1;}
int step = std::sqrt(n);

// Finding the block where element is
// present (if it is present)
int prev = 0;
while (arr[std::min(step, n) - 1] < x) {
while (step < n && arr[std::min(step, n) - 1] < x) {
prev = step;
step += std::sqrt(n);
if (prev >= n)
Expand All @@ -25,7 +26,7 @@ int jumpSearch(int arr[], int x, int n) {

// Doing a linear search for x in block
// beginning with prev.
while (arr[prev] < x) {
while (prev <= std::min(step, n) && arr[prev] < x) {
prev++;

// If we reached next block or end of
Expand All @@ -34,7 +35,7 @@ int jumpSearch(int arr[], int x, int n) {
return -1;
}
// If element is found
if (arr[prev] == x)
if (prev < n && arr[prev] == x)
return prev;

return -1;
Expand Down
5 changes: 3 additions & 2 deletions sorting/merge_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
* based sorting algorithm.
* Merge Sort is a divide and conquer algorithm
* Time Complexity: O(n log n)
* Merge Sort is a stable sorting algorithm (preserves relative order of equal elements).
* It is same for all best case, worst case or average case
* Merge Sort is very efficient when for the small data.
* In built-in sort function merge sort along with quick sort is used.
* Merge Sort performs efficiently for large datasets due to its O(nlogn) complexity.
* Many standard libraries use hybrid sorting algorithms (e.g., introsort).
*/
#include <iostream>
#include <vector>
Expand Down