0% found this document useful (0 votes)
17 views3 pages

Count Sort - Radix Sort Time Complexity Analysis and Space Complexity Analysis

Uploaded by

ahmedthomas909
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Count Sort - Radix Sort Time Complexity Analysis and Space Complexity Analysis

Uploaded by

ahmedthomas909
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

2024

Count Sort and Radix Sort - Time and Space Complexity Analysis

SUPERVISOR: Eng/ Noor El-Deen Magdy

BY: Ahmed Mamdouh

Faculty of Engineering,
𝟐𝒏𝒅 Year Computer and Systems Department
Intro to count and radix:
 Count Sort: A non-comparative sorting algorithm that counts the
occurrences of each element in the input array. It works best when
sorting integers within a known range and is efficient when the range
(k) is not significantly larger than the number of elements (n).

 Radix Sort: A stable, non-comparative sorting algorithm that


processes each digit of the numbers individually. It sorts numbers digit-
by-digit, starting from the least significant digit (LSD Radix Sort) and
relies on a stable sort like Count Sort at each digit level. Making it
effective for large lists of integers.
*Example with Implementation:
 Quick Sort:
void countSort(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max) max = arr[i];

int* count = new int[max + 1]();


for (int i = 0; i < n; i++)
count[arr[i]]++;

int index = 0;
for (int i = 0; i <= max; i++) {
while (count[i] > 0) {
arr[index++] = i;
count[i]--;
}
}
delete[] count;
}

Radix Sort:
void countSortForRadix(int arr[], int n, int exp) {
int* output = new int[n];
int count[10] = {0};

for (int i = 0; i < n; i++)


count[(arr[i] / exp) % 10]++;

for (int i = 1; i < 10; i++)


count[i] += count[i - 1];

for (int i = n - 1; i >= 0; i--) {


output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}

for (int i = 0; i < n; i++)


arr[i] = output[i];

delete[] output;
}

void radixSort(int arr[], int n) {


int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max) max = arr[i];

for (int exp = 1; max / exp > 0; exp *= 10)


countSortForRadix(arr, n, exp);
}

*Complexity Analysis:
 Count Sort:
 Time Complexity: O(n+k), where n is the number of elements and k
is the range.

 Space Complexity:O(k), due to the auxiliary count array.

 Radix Sort:
 Time Complexity: O(d(n+b)), where d is the number of digits and b is
the base (usually 10).

 Space Complexity: O(n+b), for the output and count arrays.

*Use Cases:

 Count Sort: Ideal for sorting integers with a limited range (e.g.,
grading).

 Radix Sort: Useful for large integer lists when digit length is known
(e.g., sorting IDs).

You might also like