0% found this document useful (0 votes)
2 views

Non-comparative Sorting Algorithms

The document explains two non-comparative sorting algorithms: Radix Sort and Counting Sort. Radix Sort sorts numbers by processing individual digits from least to most significant, while Counting Sort organizes elements by counting their occurrences, making it efficient for small integer ranges. Both algorithms have a time complexity of O(nk) for Radix Sort and O(n+k) for Counting Sort, where n is the number of elements and k is the range of values.

Uploaded by

almudharisaleh8
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)
2 views

Non-comparative Sorting Algorithms

The document explains two non-comparative sorting algorithms: Radix Sort and Counting Sort. Radix Sort sorts numbers by processing individual digits from least to most significant, while Counting Sort organizes elements by counting their occurrences, making it efficient for small integer ranges. Both algorithms have a time complexity of O(nk) for Radix Sort and O(n+k) for Counting Sort, where n is the number of elements and k is the range of values.

Uploaded by

almudharisaleh8
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/ 5

Non-comparative Sorting Algorithms

Radix Sort Explanation


Radix Sort is a non-comparative, stable sorting algorithm that sorts numbers (or
strings) by processing individual digits. It works by distributing elements into buckets
based on each digit, starting from the least significant digit (LSD) to the most
significant digit (MSD).
It is mainly used for sorting numbers but can be adapted for strings. It is efficient when
dealing with large numbers, especially when the range of numbers is significantly
larger than the number of elements.
Time Complexity
• Best case: O(nk)
• Worst case: O(nk)
• Average case: O(nk)
where n is the number of elements and k is the number of digits in the
maximum number.
Working Mechanism of Radix Sort
Radix Sort follows these steps:
1. Find the maximum number to determine the number of digits (d).
2. Sort based on each digit, from the least significant to the most significant, using
a stable sorting method like Counting Sort.
3. Repeat the process for each digit place value until all digits are processed.

Non-comparative Sorting Algorithms 1 Dr. Rafat Alhanani


Example 1: Sorting an Array of Integers
Given Input Array:
[1536, 7, 4351, 400, 13, 2,75, 37]
Step-by-Step Execution
Step 1: Sort by Least Significant Digit (1s place)
• Buckets:
37
400 4351 2 13 75 1536 7
0 1 2 3 4 5 6 7 8 9

• After sorting: [400, 4351, 2, 13, 75, 1536, 7, 37]


Step 2: Sort by 10s place
• Buckets:
7
2 37
400 13 1536 4351 75
0 1 2 3 4 5 6 7 8 9

• After sorting: [400, 2, 7, 13, 1536, 37, 4351, 75]


Step 3: Sort by 100s place
• Buckets:
75
37
13
7
2 4351 400 1536
0 1 2 3 4 5 6 7 8 9

• After sorting: [ 2, 7, 13, 37, 75, 4351, 400, 1536]


Step 4: Sort by 1000s place
• Buckets:
Non-comparative Sorting Algorithms 2 Dr. Rafat Alhanani
400
75
37
13
7
2 1536 4351
0 1 2 3 4 5 6 7 8 9

• Final sorted array: [2, 7, 13, 37, 75, 400, 1536, 4351]

Counting Sort Explanation


Counting Sort is a non-comparative, stable sorting algorithm that sorts elements by
counting their occurrences. It works well for small integer ranges and is especially
useful when the range of values is not significantly larger than the number of
elements.

How Counting Sort Works


1. Find the maximum element to determine the range of values.
2. Create the Count array that stores the frequency of each element, and initialize
the Count Array of length k+1 with all elements 0.
3. Modify the count array to store cumulative counts, which determine positions
in the sorted array.
4. For each value of the original array arr[i], find the match index z in the modified
count array MC[z] where z= arr[i], then store this element arr[i] or z in the index
w of sorted array S[w], where w= MC[z]-1.

i 0 1 2 3 4 5 6 7 8 9 10 11
arr[i] 4 1 2 3 2 1 0 5 4 1 6 1

0 1 2 3 4 5 6
Count 1 4 2 1 2 1 1

Non-comparative Sorting Algorithms 3 Dr. Rafat Alhanani


z 0 1 2 3 4 5 6
Modified Count MC[z] 1 5 7 8 10 11 12
Modified Count MC[z] 1 5 7 8 9 11 12

w 0 1 2 3 4 5 6 7 8 9 10 11
Sorted array S[w] 4

5. Build the output array by placing elements at their correct positions.


6. Copy the sorted elements back to the original array.
Time Complexity
• Best case: O(n+k)
• Worst case: O(n+k)
• Average case: O(n+k)
where n is the number of elements and k is the range of input values.
Space Complexity
• O(k) for the count array
• O(n) for the output array

Example 1: Sorting an Array of Integers


Given Input Array:
array 6 1 7 8 3 7 2 8 3 7 1 0

Step-by-Step Execution
Step 1: Find Maximum Element
• N = 12
• Maximum element, k = 8
Step 2: Create and Initialize Count Array of length k+1 with all elements 0
• Count array of length k+1=9, (index represents the number, value represents
frequency)

Non-comparative Sorting Algorithms 4 Dr. Rafat Alhanani


index 0 1 2 3 4 5 6 7 8
count 1 2 1 2 0 0 1 3 2

Step 3.1: create Sorted Array by using count array:


Sorted array 0 1 1 2 3 3 6 7 7 7 8 8

Or Step 3.2: create Sorted Array by creating Modified Count Array


index 0 1 2 3 4 5 6 7 8
M.Count 1 3 4 6 6 6 7 10 12

index 0 1 2 3 4 5 6 7 8
M.Count 1 3 4 6 6 6 6 10 12

index 0 1 2 3 4 5 6 7 8
M.Count 1 2 4 6 6 6 6 10 11

index 0 1 2 3 4 5 6 7 8
M.Count 1 2 4 6 6 6 6 9 11

0 1 2 3 4 5 6 7 8 9 10 11
Sorted array 1 6 7

Non-comparative Sorting Algorithms 5 Dr. Rafat Alhanani

You might also like