Skip to content

Fix sorting algorithm bugs and improve metrics tracking#1

Open
shubhpatel9 wants to merge 2 commits intomasterfrom
claude/add-claude-documentation-3aX9F
Open

Fix sorting algorithm bugs and improve metrics tracking#1
shubhpatel9 wants to merge 2 commits intomasterfrom
claude/add-claude-documentation-3aX9F

Conversation

@shubhpatel9
Copy link
Owner

Summary

This PR fixes critical bugs in the sorting algorithm implementations and improves operation metrics tracking for more accurate efficiency analysis. The changes address incorrect swap operations, incomplete bubble sort for lists, and metrics reset issues in quicksort.

Key Changes

Bubble Sort (vector<int>)

  • Fixed swap operation: Replaced arithmetic swap (vec[i] += vec[i+1]; ...) with std::swap(vec[i], vec[i+1]) for correctness and clarity
  • Corrected metrics: Changed accesses += 3 to accesses += 2 to accurately count only the two write operations (reads already counted in comparison)

Bubble Sort (list<int>)

  • Implemented proper bubble sort: Replaced incomplete selection-sort-style nested loop with correct bubble sort using iterator-based swaps and early-exit optimization
  • Fixed return value: Changed return 0 to return true to indicate successful completion
  • Improved metrics tracking: Properly count reads and writes for list element comparisons and swaps

Insertion Sort (vector<int>)

  • Refined metrics tracking: Separated read and write operations in the while loop for more granular operation counting
  • Improved loop logic: Changed condition from while (i >= 0 && vec[i] > key) to separate the boundary check and comparison for clearer metrics attribution

Merge Sort (vector<int>)

  • Added metrics reset: Initialize metrics.accesses and metrics.comparisons to 0 at the start of merge_sort() to ensure clean state before recursive calls

Quicksort (vector<int>)

  • Fixed critical swap bug: Changed swap(i, j) to swap(vec[i], vec[j]) to actually swap array elements instead of just swapping index variables
  • Refactored metrics handling: Extracted recursive logic into quicksort_helper() that does NOT reset metrics, allowing totals to accumulate across all recursive calls. Public quicksort() entry point resets metrics once before delegating to helper
  • Added swap metrics: Count 2 accesses for each element swap operation
  • Code cleanup: Fixed typos and improved comments

Documentation

  • Added CLAUDE.md: Comprehensive project documentation including repository structure, source file roles, implemented algorithms, known issues, build instructions, and development workflow

Notable Implementation Details

  • Metrics now accurately distinguish between read and write operations
  • Quicksort metrics now correctly accumulate across all recursive frames instead of only reporting the final frame
  • All sorting implementations maintain consistent SortMetrics tracking for fair algorithm comparison
  • Both root-level and sorting/ directory files are kept in sync with identical changes

https://claude.ai/code/session_01Bq4tyZTqt1W7VNi9hLe6Fk

claude added 2 commits March 12, 2026 05:46
Documents project structure, all source file roles, implemented sorting
algorithms (bubble, insertion, merge, quicksort), STL container coverage,
known bugs/stubs, build instructions for VS2019 and g++, and key
conventions for AI assistants working in this repo.

https://claude.ai/code/session_01Bq4tyZTqt1W7VNi9hLe6Fk
… all algorithms

- quicksort: fix swap(i,j) → swap(vec[i],vec[j]) so elements are actually reordered
- quicksort: extract quicksort_helper() so metrics reset only at the public entry point,
  not on every recursive call (previously only the last frame's counts were reported)
- bubble_sort (vector): replace fragile arithmetic swap with std::swap; fix access count
- bubble_sort (list): rewrite as true adjacent-element bubble sort (was selection sort);
  fix return value from 0 (false) to true
- insertion_sort: count comparisons and accesses inside the while loop on every
  iteration, including when the condition terminates the loop (was 0 comparisons)
- merge_sort: add metrics.accesses/comparisons reset at public entry point; fix merge()
  to count comparisons outside the if/else so both branches are counted symmetrically

https://claude.ai/code/session_01Bq4tyZTqt1W7VNi9hLe6Fk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants