[PERFORMANCE] Use bubble sort instead of Array.sort()#3
Open
GAURAV-1313 wants to merge 2 commits into
Open
Conversation
✅ Deploy Preview for nofriction canceled.
|
Owner
Author
AI Code ReviewSummaryThe sortNumbers function implements a naive sorting algorithm with a logical error in its nested loops causing incorrect ordering and potential performance issues. The function also lacks input validation and does not handle edge cases or mutation concerns. Key FindingsBugs
Performance
Recommended FixesGeneral
Performance Fixes
Suggested Tests
import { sortNumbers } from "./sort";
describe("sortNumbers", () => {
let originalArray;
beforeEach(() => {
// Reset before each test to avoid mutation side effects
originalArray = undefined;
});
it("should sort a typical array of positive integers in ascending order", () => {
originalArray = [5, 3, 8, 1, 4];
const input = [...originalArray];
const sorted = sortNumbers(input);
expect(sorted).toEqual([1, 3, 4, 5, 8]);
// Check input is mutated (based on implementation behavior)
expect(input).toBe(sorted);
});
it.each([
[[1]], [1],
[[], []],
[[-5, -10, 0], [-10, -5, 0]],
[[3, 3, 3], [3, 3, 3]],
[[NaN, 2, 1], [1, 2, NaN]], // NaN is tricky, expect it to be at the end
])("should correctly sort array %j to %j", (input, expected) => {
const inputCopy = [...input];
const result = sortNumbers(inputCopy);
expect(result).toHaveLength(input.length);
expect(result).toEqual(expected);
});
it("should handle very large numbers correctly", () => {
const input = [Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER - 1, 0];
const result = sortNumbers([...input]);
expect(result).toEqual([0, Number.MAX_SAFE_INTEGER - 1, Number.MAX_SAFE_INTEGER]);
});
it("should handle array with Infinity and -Infinity values", () => {
const input = [Infinity, -Infinity, 0, 5];
const expected = [-Infinity, 0, 5, Infinity];
expect(sortNumbers([...input])).toEqual(expected);
});
it("should handle single element array", () => {
const input = [42];
expect(sortNumbers([...input])).toEqual([42]);
});
it("should return empty array if input is empty", () => {
expect(sortNumbers([])).toEqual([]);
});
it("should throw an error if input is not an array", () => {
expect(() => sortNumbers(null)).toThrow(TypeError);
expect(() => sortNumbers(undefined)).toThrow(TypeError);
expect(() => sortNumbers(123)).toThrow(TypeError);
expect(() => sortNumbers("string")).toThrow(TypeError);
});
it("should throw an error if array contains non-number elements", () => {
expect(() => sortNumbers([1, 2, "3"]))
.toThrow(TypeError);
expect(() => sortNumbers([{}]))
.toThrow(TypeError);
});
it("should verify that it sorts in ascending order and returns the same reference (mutates input)", () => {
const arr = [4, 2, 3, 1];
const result = sortNumbers(arr);
expect(result).toBe(arr);
for (let i = 0; i < result.length - 1; i++) {
expect(result[i]).toBeLessThanOrEqual(result[i + 1]);
}
});
});Auto-Fix AvailableAdd the 5 issues found. Reviewed by RepoSpace AI. |
- src/sort.js: Replaced the inefficient bubble sort algorithm with the built-in sort method for improved performance.
Owner
Author
Auto-Fix ResultsApplied (1)
Generated by RepoSpace AI. Review AI commits before merging. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduces O(n^2) bubble sort instead of efficient built-in sort.