Skip to content

Add Counting Sort implementation in Java#175

Merged
admirerr merged 1 commit into
admirerr:mainfrom
Pasan11504:feature/add-counting-sort-java
Oct 9, 2025
Merged

Add Counting Sort implementation in Java#175
admirerr merged 1 commit into
admirerr:mainfrom
Pasan11504:feature/add-counting-sort-java

Conversation

@Pasan11504
Copy link
Copy Markdown
Contributor

Add Counting Sort Implementation in Java

Problem: Counting Sort Algorithm

This PR adds a comprehensive Counting Sort implementation to Java/sorting/.

Description

Counting Sort is a non-comparison based sorting algorithm that sorts elements by counting the number of occurrences of each unique element. It's particularly efficient for sorting integers with a small range.

Implementation Details

Algorithm Steps:

  1. Find the range of input data (max and min values)
  2. Create a count array to store frequency of each element
  3. Modify count array by adding previous counts (cumulative count)
  4. Build output array by placing elements at their correct positions
  5. Copy output array back to original array

Features:

  • Two implementations:
    • countingSort() - for non-negative integers
    • countingSortWithNegatives() - handles negative numbers using offset
  • Stable sorting - maintains relative order of equal elements
  • Comprehensive comments - every step explained clearly
  • Problem name mentioned in file header
  • 10 test cases - covering all scenarios and edge cases
  • Error handling - validates input for negative numbers

Complexity Analysis

  • Time Complexity: O(n + k) where n = number of elements, k = range of input
  • Space Complexity: O(n + k)

Why This is Valuable

  • Missing algorithm: Java/sorting/ only had 6 sorting algorithms, Python had 16 including Counting Sort
  • Fundamental algorithm: Important non-comparison based sorting technique
  • Educational value: Well-documented with clear explanations
  • Production ready: Includes error handling and comprehensive testing

Test Results

All 10 test cases passed:
✅ Standard array sorting
✅ Already sorted array
✅ Reverse sorted array
✅ Array with many duplicates
✅ Single element array
✅ All same elements
✅ Large numbers
✅ Array with zeros
✅ Negative numbers (alternative method)
✅ Error handling for invalid input

Guidelines Followed

  • Problem name clearly mentioned in comments
  • Code well-commented
  • Proper file naming: CountingSort.java
  • Correct directory: Java/sorting/
  • No duplicate (verified existing files)
  • Compiled and tested successfully

Ready for Hacktoberfest 2025! 🎃

- Non-comparison based sorting algorithm
- Time Complexity: O(n + k)
- Includes two versions: standard and with negative numbers support
- Clear comments explaining each step
- Problem name mentioned in header
- 10 comprehensive test cases
- Proper error handling
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Oct 8, 2025

🎉 Welcome to Hacktoberfest 2025, @Pasan11504! 🎃

Thank you for your first contribution to our DSA repository! Here's what happens next:

🔍 Automatic Checks

  • Code Validation: Passed
  • 🧪 Compilation Tests: Passed

📋 Next Steps

🎯 Great job! Your code compiled successfully. Maintainers @admirerr and @kartikey2991 will review your PR soon.

🎁 What You Get

  • 🏆 Hacktoberfest Credit: This PR counts toward your 6 PR goal for exclusive T-shirt + Tree!
  • 🌟 Hall of Fame: You'll be featured in our contributors list
  • 📚 Learning: Code review feedback from experienced developers

💡 Tips for Success

  • Follow our Contributing Guidelines
  • Add comments explaining your algorithm
  • Include time/space complexity analysis
  • Test your code before submitting

Welcome to the community! 🚀

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Oct 8, 2025

🤖 Automated PR Status

🔍 Code Validation

Passed - File naming and structure look good!

🧪 Compilation Tests

Passed - All code compiles successfully!

📋 Overall Status

🎉 Ready for Review - Your PR has passed all automated checks!
👥 Maintainers have been notified for review.


This comment was generated automatically. Checks will re-run when you push new commits.

@admirerr
Copy link
Copy Markdown
Owner

admirerr commented Oct 9, 2025

Approved

@admirerr admirerr merged commit 86ebecc into admirerr:main Oct 9, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants