Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Selection Vs Counting Sort

Prothom Antor Banik


Md. Shifat Alam
Arafat Hossain
Asraful Islam
Shadman Saleheen
Ridwanul Islam Pavel

January 2024
Introduction

helo
Background Study

Selection Sort
Selection sort operates by repeatedly selecting the minimum element from an
unsorted portion of the dataset and placing it at the beginning.

Algorithm:

1: for j ← 1 to n − 1 do
2: smallest ← j
3: for k ← j + 1 to n do
4: if A[k] < A[min] then
5: min ← k
6: end if
7: end for
8: Swap A[min] and A[j]
9: end for
Selection Sort (Example)

The algorithm repeatedly selects the smallest (or largest) element from the unsorted
portion of the list and swaps it with the first element of the unsorted part.
Background Study

Counting Sort
Counting sort is a non comparison based linear time sorting Algorithm.

Algorithm:
1: Counting-Sort(A, B, k):
2: for i ← 0 to k do
3: C[i] ← 0
4: end for
5: for j ← 1 to A.length do
6: C[A[j]] ← C[A[j]] + 1
7: end for
8: for i ← 1 to k do
9: C[i] ← C[i] + C[i − 1]
10: end for
11: for j ← A.length down to 1 do
12: B[C[A[j]]] ← A[j]
13: C[A[j]] ← C[A[j]] − 1
14: end for
Counting Sort (Example)

It is a non comparison based sorting algorithm. It operates by counting the frequency


of the elements and using Arithmetic operations, finds the position of them.[5]

4 2 2 1 0 1 2 0 1
(a) Data (b) Frequency

0 1 3 3 4 1 2 2 4
(c) Cumulative Frequency (d) Sorted Data
Result and Analysis

Topic Selection Sort Counting Sort


Algorithm Type Comparison-based Non-comparison-based
Stability Not stable Stable
Time Complexity O(n2 ) O(n + k)
Space Complexity O(1) O(k)
Applicability Often not the first choice Particularly efficient when
for large datasets due to its the range of input values is
quadratic time complexity. It is not significantly larger than
more suitable for small datasets. the number of elements. It is
suitable for sorting integers
or objects with discrete keys.
Table: Summary of differences.
Time Complexity Analysis

Comparative Analysis for n ≈ k


Counting sort excels in efficiency when the number of elements in the array n equals
the range of values k.

Figure: Comparative runtime when k ≈ n.


Time Complexity Analysis
Comparative Analysis for k >> n
When dealing with a significantly large value for k, counting sort transitions from
being the best to the worst choice. Consequently, selection sort emerges as a more
efficient alternative. [8]

Figure: Comparative runtime when k >> n.


Space Complexity Analysis

In-place Sorting
In-place sorting algorithms are algorithms that don’t require extra space proportional
to the input size. They sort the data using only a constant amount of extra space.

Selection Sort:
Counting Sort:
Swapping is done directly within the
As two extra arrays of size n and k is used,
original array without the need for
the space complexity of Counting Sort
additional memory. Hence, the space
algorithm is O(n + k).
complexity is O(1).
Stability

Stability Analysis of the Algorithms

Selection Sort: Counting Sort:


Selection sort works by repeatedly The algorithm first counts the number
finding the minimum (or maximum) of occurrences of each unique
element from the unsorted portion of element in the input array and uses
the array and swapping it with the this information for determining the
first unsorted element, leading to correct position of each element.
instability. Therefore, this algorithm is stable [2].
Conclusion

To Summarize

Selection Sort is a comparison-based, Counting Sort is a


in-place, and unstable sorting non-comparison-based, stable sorting
algorithm with quadratic time algorithm with linear time complexity
complexity, making it efficient for under certain conditions, making it
small datasets. efficient for specific scenarios.
References I

Khalid Suleiman Al-Kharabsheh, Ibrahim Mahmoud AlTurani, Abdallah


Mahmoud Ibrahim AlTurani, and Nabeel Imhammed Zanoon.
Review on sorting algorithms a comparative study.
International Journal of Computer Science and Security (IJCSS), 7(3):120–126, 2013.
Htwe Htwe Aung.
Analysis and comparative of sorting algorithms.
International Journal of Trend in Scientific Research and Development (IJTSRD),
3(5):1049–1053, 2019.
Ahmad H Elkahlout and Ashraf YA Maghari.
A comparative study of sorting algorithms comb, cocktail and counting sorting.
International Research Journal of Engineering and Technology (IRJET), 4(01), 2017.
References II

Mahesh Goyani, Mohammad Chharchhodawala, and Bhargav Mendapara.


Min-max selection sort algorithm–improved version of selection sort.
Int. J. Adv. Res. Comput. Sci. Softw. Eng, 6, 2013.
JB Hayfron-Acquah, Obed Appiah, and K Riverson.
Improved selection sort algorithm.
International Journal of Computer Applications, 110(5), 2015.
Sultanullah Jadoon, Salman Faiz Solehria, and Mubashir Qayum.
Optimized selection sort algorithm is faster than insertion sort algorithm: a
comparative study.
International Journal of Electrical & Computer Sciences IJECS-IJENS, 11(02):19–24,
2011.
Aditya Dev Mishra and Deepak Garg.
Selection of best sorting algorithm.
International Journal of intelligent information Processing, 2(2):363–368, 2008.
References III

Hridoy Roy, Md Shafiuzzaman, and Md Samsuddoha.


Srcs: A new proposed counting sort algorithm based on square root method.
In 2019 22nd International Conference on Computer and Information Technology
(ICCIT), pages 1–6. IEEE, 2019.

You might also like