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

CS213 DS2023 Lec 24 NonComparativeSort

The document discusses counting sort and radix sort, two non-comparative sorting algorithms. It explains that counting sort works by counting the number of objects with each key value and sorting in linear time for small key ranges. Radix sort sorts data by grouping keys based on their digit values from least to most significant, often using counting sort as a subroutine. The document provides pseudocode and examples of applying both algorithms to integer arrays.

Uploaded by

Mohamed Mostafa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

CS213 DS2023 Lec 24 NonComparativeSort

The document discusses counting sort and radix sort, two non-comparative sorting algorithms. It explains that counting sort works by counting the number of objects with each key value and sorting in linear time for small key ranges. Radix sort sorts data by grouping keys based on their digit values from least to most significant, often using counting sort as a subroutine. The document provides pseudocode and examples of applying both algorithms to integer arrays.

Uploaded by

Mohamed Mostafa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

CS213 – Data Structures

Lecture 24: Non-comparative


Sorting
Counting Sort
Radix Sort
Counting Sort
• Counting sort is an algorithm for sorting a collection
of objects according to keys that are small integers
• It operates by creating an array for the output sequence
where its indices run from 0 to the largest integer in the
sequence. So, for example, if our numbers range from 0-
1000, then our output array out will have a size of 1001.
• The output array is then initialized to zeros.
• Next, the array a is passed and each element k
encountered increments out at index k.
• So, if the value 78 is encountered in the array, then the
output array out[78] is incremented by 1.

Data Structures and Algorithms in C++, Fourth Edition 2


Counting Sort
• Its running time is linear in the number of items and
the maximum key value O(n+k) where k is the maximum
number in the array.
• It is only suitable for use in situations where the keys are
not significantly larger than the number of items.
• If it can be guaranteed that the array numbers are small
in range (small k), then counting sort is very efficient
even for very large arrays (large n).
• However, it is often used as a subroutine in another
sorting algorithm, radix sort, that can handle larger keys
more efficiently.

Data Structures and Algorithms in C++, Fourth Edition 3


Counting Sort
• While examining the algorithm, we use the array of
counters, count[], to track the number of times each
value occurs in data[]
• The count[] array is indexed with the values from data

• Then, count is iterated to populate the sorted array into


data

• The algorithm is also designed to deal with multiple


occurrences of any value in data[]

Data Structures and Algorithms in C++, Fourth Edition 4


Counting Sort
count[ ]
0 1 2 3 4 5 6 7
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 1
0 2 0 0 0 0 0 1
0 2 0 1 0 0 0 1
1 2 0 1 0 0 0 1
1 2 0 1 0 0 0 2
1 2 0 1 0 1 0 2
1 2 0 1 0 2 0 2
1 2 0 1 0 2 0 3
1 2 0 2 0 2 0 3

Data Structures and Algorithms in C++, Fourth Edition 5


Counting Sort
count[ ] data[ ]
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 8 9
1 2 0 2 0 2 0 3 0
0 2 0 2 0 2 0 3 0 1
0 1 0 2 0 2 0 3 0 1 1
0 0 0 2 0 2 0 3 0 1 1 3
0 0 0 1 0 2 0 3 0 1 1 3 3
0 0 0 0 0 2 0 3 0 1 1 3 3 5
0 0 0 0 0 1 0 3 0 1 1 3 3 5 5
0 0 0 0 0 0 0 3 0 1 1 3 3 5 5 7
0 0 0 0 0 0 0 2 0 1 1 3 3 5 5 7 7
0 0 0 0 0 0 0 1 0 1 1 3 3 5 5 7 7 7
0 0 0 0 0 0 0 0

Data Structures and Algorithms in C++, Fourth Edition


Choosing Sorting Algorithm
• Should we use heapsort or counting sort if we are
sorting an array with 100 elements, where keys range
from 0 -8000 ?

7
Radix Sort
• Radix sort is a non-comparative sorting algorithm that
sorts data with integer keys by grouping keys by the individual
digits which share the same significant position and value.
• Radix sort dates back as far as 1887 to the work of Herman
Hollerith.
• This type of sort is used frequently in everyday applications.
• For example, to sort library cards by author, we can create a
separate pile for each card based on the first letter of the
name.
• Each pile is then further sorted into smaller piles based on the
second letter of the name

Data Structures and Algorithms in C++, Fourth Edition 8


Radix Sort
• This process continues until the piles have been
subdivided as many times as the number of letters in the
longest name.
• For integer numbers, the numbers are padded on the
left with zeros to make them same length, so [23 123
234 567 3] becomes [023 123 234 567 003]
• For numbers, we start dividing the numbers into piles
starting with the right-most digit (least significant bit) to
the left-most digit (most significant bit)

Data Structures and Algorithms in C++, Fourth Edition 9


Radix Sort
• This process is described by the following pseudocode:
Algorithm RADIX_SORT(A,n)
// A is an array to be sorted
// d is the number of digits in the largest element in A
for L ← 1 to d do //numbering from the right-most digit
Apply a stable sort (such as counting sort) on digit
of array A
end
• If we organize the piles as queues, then the order of the
elements in the list is retained
• Thus, when the integers are sorted by digit L, within each pile
they will be sorted by each digit from 1 to L - 1

Data Structures and Algorithms in C++, Fourth Edition 10


Radix Sort
• Sort the array [655,530,88,500,2,11]
• Pad the numbers with zeros to be of equal length
• [655,530,088,500,002,011]

Data Structures and Algorithms in C++, Fourth Edition 11


Radix Sort
• Array [655, 530, 088, 500, 002, 011]
0 530,500
1 011
2 002
3
4 [530, 500, 011, 002, 655, 088]
5 655
6
7
8 088
9

Data Structures and Algorithms in C++, Fourth Edition 12


Radix Sort
• Array [530, 500, 011, 002, 655, 088]
0 500,002
1 011
2
3 530
4 [500, 002, 011, 530, 655, 088]
5 655
6
7
8 088
9

Data Structures and Algorithms in C++, Fourth Edition 13


Radix Sort
• Array [500, 002, 011, 530, 655, 088]
0 002,011,088
1
2
3
4 [002, 011, 088, 500, 530, 655]
Sorted!
5 500,530
6 655
7
8
9

Data Structures and Algorithms in C++, Fourth Edition 14


Radix Sort

Data Structures and Algorithms in C++, Fourth Edition 15


Complexity of Radix Sort
• Given n is number of elements in the array to be
sorted and d is the number of digits of the maximum
number, then complexity is O(n.d).
• Radix sort has a linear time complexity since d is a
constant.
• But radix sort can perform poorly if n is relatively low,
while d is high.
• On the other hand, if d is less than logN, then Radix
sort is better than sub-quadratic sort algorithms.

16
Notes about Counting and Radix Sorting

• Both algorithms need extra space to sort.


• Both cannot sort negative numbers.

17

You might also like