-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Added an optimized bubble sort in sorts #3004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
realstealthninja
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow the documentation guidelines, The algorithm is good, you need to add documentation, remove the using namespace line.
thank you for raising this pr
|
This pull request has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Description of Change
Added an Optimized Bubble Sort implementation in the sorting section.
This version improves efficiency over the standard Bubble Sort by introducing a swap flag:
During each pass, adjacent elements are compared and swapped if out of order.
A flag is set whenever a swap occurs.
If no swaps happen in a pass, the algorithm terminates early since the array is already sorted.
Key Points:
Best Case (already sorted): O(n)
Average / Worst Case: O(n²)
Space Complexity: O(1) (in-place)
Stable Sort: Yes
This optimization reduces the number of passes in average and best cases, making it more efficient than the naive approach.
Checklist
Notes: