{"record":{"id":"c5a9008486e0f40c","repo":"TheAlgorithms/C-Sharp","slug":"sortedarray","errorCode":null,"errorMessage":"sortedArray","messagePattern":"sortedArray","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Algorithms/Search/JumpSearcher.cs","lineNumber":22,"sourceCode":"///     Jump Search checks fewer elements by jumping ahead by fixed steps.\n///     The optimal steps to jump is √n, where n refers to the number of elements in the array.\n///     Time Complexity: O(√n)\n///     Note: The array has to be sorted beforehand.\n/// </summary>\n/// <typeparam name=\"T\">Type of the array element.</typeparam>\npublic class JumpSearcher<T> where T : IComparable<T>\n{\n    /// <summary>\n    ///     Find the index of the item searched for in the array.\n    /// </summary>\n    /// <param name=\"sortedArray\">Sorted array to be search in. Cannot be null.</param>\n    /// <param name=\"searchItem\">Item to be search for. Cannot be null.</param>\n    /// <returns>If item is found, return index. If array is empty or item not found, return -1.</returns>\n    public int FindIndex(T[] sortedArray, T searchItem)\n    {\n        if (sortedArray is null)\n        {\n            throw new ArgumentNullException(\"sortedArray\");\n        }\n\n        if (searchItem is null)\n        {\n            throw new ArgumentNullException(\"searchItem\");\n        }\n\n        int jumpStep = (int)Math.Floor(Math.Sqrt(sortedArray.Length));\n        int currentIndex = 0;\n        int nextIndex = jumpStep;\n\n        if (sortedArray.Length != 0)\n        {\n            while (sortedArray[nextIndex - 1].CompareTo(searchItem) < 0)\n            {\n                currentIndex = nextIndex;\n                nextIndex += jumpStep;\n","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Search/JumpSearcher.cs#L4-L40","documentation":"JumpSearcher.FindIndex requires a non-null sorted array and throws ArgumentNullException(\"sortedArray\") when it is null. Jump search computes step sizes from array.Length, so a null array is rejected immediately as a caller contract violation.","triggerScenarios":"Passing a null array reference to FindIndex — uninitialized fields, methods that return null instead of empty arrays, or data not yet loaded from I/O.","commonSituations":"Cache misses returning null; JSON/API deserialization leaving arrays null; forgetting to sort-and-assign before the lookup step in a pipeline.","solutions":["Coalesce at the call site: sortedArray ?? Array.Empty<T>() (FindIndex then legitimately returns -1 for empty input).","Initialize the array in the constructor/field initializer so it is never null.","Make the upstream loader return an empty array on failure instead of null.","Catch ArgumentNullException only when null is a recoverable, expected state."],"exampleFix":"// before\nvar idx = jumper.FindIndex(loadedArray, key);\n// after\nvar idx = jumper.FindIndex(loadedArray ?? Array.Empty<int>(), key);","handlingStrategy":"validation","validationCode":"if (sortedArray is null) return -1;","typeGuard":"bool HasData<T>(T[]? arr) => arr is not null;","tryCatchPattern":"try { return jumper.FindIndex(sortedArray, searchItem); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"sortedArray\") { return -1; }","preventionTips":["Coalesce null arrays with ?? Array.Empty<T>() before search calls.","Keep data-loading code contract: return empty, never null.","Sort and assign the array in one step so it can never be left null.","Run unit tests that exercise the empty/null input paths."],"tags":["null-argument","search","argument-validation"],"backgroundTag":"null-argument","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}