Fix sorting algorithm bugs and improve metrics tracking#1
Open
shubhpatel9 wants to merge 2 commits intomasterfrom
Open
Fix sorting algorithm bugs and improve metrics tracking#1shubhpatel9 wants to merge 2 commits intomasterfrom
shubhpatel9 wants to merge 2 commits intomasterfrom
Conversation
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
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.
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>)vec[i] += vec[i+1]; ...) withstd::swap(vec[i], vec[i+1])for correctness and clarityaccesses += 3toaccesses += 2to accurately count only the two write operations (reads already counted in comparison)Bubble Sort (
list<int>)return 0toreturn trueto indicate successful completionInsertion Sort (
vector<int>)while (i >= 0 && vec[i] > key)to separate the boundary check and comparison for clearer metrics attributionMerge Sort (
vector<int>)metrics.accessesandmetrics.comparisonsto 0 at the start ofmerge_sort()to ensure clean state before recursive callsQuicksort (
vector<int>)swap(i, j)toswap(vec[i], vec[j])to actually swap array elements instead of just swapping index variablesquicksort_helper()that does NOT reset metrics, allowing totals to accumulate across all recursive calls. Publicquicksort()entry point resets metrics once before delegating to helperDocumentation
Notable Implementation Details
SortMetricstracking for fair algorithm comparisonsorting/directory files are kept in sync with identical changeshttps://claude.ai/code/session_01Bq4tyZTqt1W7VNi9hLe6Fk