Professional Documents
Culture Documents
Selection Vs Counting 133 135 139 169 175 179
Selection Vs Counting 133 135 139 169 175 179
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)
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
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
To Summarize