
Counting Sort is a non-comparison-based sorting algorithm. It sorts integers by counting the occurrences of each value in the dataset. This algorithm is particularly efficient when dealing with small, positive integers or data that can be mapped to integers in a limited range.
count array where each index represents a number, and its value represents the frequency of that number in the input array.count array to determine the position of each element in the sorted output.Count Occurrences:
count array: [0, 1, 2, 2, 1, 0, 0, 0, 1] Cumulative Count:
count array: [0, 1, 3, 5, 6, 6, 6, 6, 7] Sort:
count array to place elements in their correct positions in the output array: count array and the output array.import java.util.Arrays;
public class CountingSort {
public static void countingSort(int[] arr) {
if (arr.length == 0) {
return;
}
// Step 1: Find the range of the array
int max = Arrays.stream(arr).max().getAsInt();
int min = Arrays.stream(arr).min().getAsInt();
int range = max - min + 1;
// Step 2: Create count array and output array
int[] count = new int[range];
int[] output = new int[arr.length];
// Step 3: Count occurrences of each number
for (int num : arr) {
count[num - min]++;
}
// Step 4: Compute cumulative count
for (int i = 1; i < count.length; i++) {
count[i] += count[i - 1];
}
// Step 5: Place elements into output array in sorted order
for (int i = arr.length - 1; i >= 0; i--) {
output[count[arr[i] - min] - 1] = arr[i];
count[arr[i] - min]--;
}
// Step 6: Copy sorted array back to the original array
System.arraycopy(output, 0, arr, 0, arr.length);
}
public static void main(String[] args) {
int[] arr = {4, 2, 2, 8, 3, 3, 1};
System.out.println("Original Array: " + Arrays.toString(arr));
countingSort(arr);
System.out.println("Sorted Array: " + Arrays.toString(arr));
}
}
def counting_sort(arr):
if not arr:
return arr
# Step 1: Find the range of the array
min_val = min(arr)
max_val = max(arr)
range_val = max_val - min_val + 1
# Step 2: Create count array and output array
count = [0] * range_val
output = [0] * len(arr)
# Step 3: Count occurrences of each number
for num in arr:
count[num - min_val] += 1
# Step 4: Compute cumulative count
for i in range(1, len(count)):
count[i] += count[i - 1]
# Step 5: Place elements into output array in sorted order
for num in reversed(arr):
output[count[num - min_val] - 1] = num
count[num - min_val] -= 1
return output
# Example usage
if __name__ == "__main__":
arr = [4, 2, 2, 8, 3, 3, 1]
print("Original Array:", arr)
sorted_arr = counting_sort(arr)
print("Sorted Array:", sorted_arr)
Small Integer Ranges:
Real-Time Systems:
Special Cases:
Counting Sort is highly efficient in specific scenarios but requires careful consideration of its limitations.
When analyzing a stock, one of the first financial indicators you’ll encounter is EPS, or Earnings Per Share. It’s one… Read More
When you look at a stock’s profile on a financial website, one of the first things you’ll see is its… Read More
In the world of open-source software, simplicity and flexibility are often just as important as legal protection. That’s why the… Read More
If you want your software to be open source, but still compatible with commercial use—and not as restrictive as the… Read More
When it comes to open-source software, developers and businesses alike need licenses that balance freedom, legal clarity, and long-term security.… Read More
If you’re working on open-source projects or choosing third-party libraries for your software, understanding software licenses is essential. Among the… Read More