diff --git a/search/jump_search.cpp b/search/jump_search.cpp index f7b100a4e03..5fb0822e911 100644 --- a/search/jump_search.cpp +++ b/search/jump_search.cpp @@ -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) @@ -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 @@ -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; diff --git a/sorting/merge_sort.cpp b/sorting/merge_sort.cpp index 80409c79074..42b22d0ab57 100644 --- a/sorting/merge_sort.cpp +++ b/sorting/merge_sort.cpp @@ -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 #include