0% found this document useful (0 votes)
7 views6 pages

Count Distribution Algorithm

The Distribution Counting Sort algorithm sorts an array of integers within a specified range in linear time by counting occurrences of each value. It involves initializing a frequency array, counting frequencies, computing cumulative counts, and placing elements in the sorted output array while maintaining stability. An example demonstrates the algorithm's steps with an input array and its resulting sorted array.

Uploaded by

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

Count Distribution Algorithm

The Distribution Counting Sort algorithm sorts an array of integers within a specified range in linear time by counting occurrences of each value. It involves initializing a frequency array, counting frequencies, computing cumulative counts, and placing elements in the sorted output array while maintaining stability. An example demonstrates the algorithm's steps with an input array and its resulting sorted array.

Uploaded by

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

Sure!

Let's walk through the Distribution Counting Sort algorithm step by step, with a clear
breakdown of what each part does.

🔹 Overview

This algorithm sorts an array of integers that lie within a specific range [l,u][l, u][l,u] (where
l≤A[i]≤ul \leq A[i] \leq ul≤A[i]≤u).

It works in linear time (O(n+k)O(n + k)O(n+k)) where k=u−l+1k = u - l + 1k=u−l+1, by


counting how many times each value occurs and using that information to place elements in
the correct position.

🧠 Variables:

 A[0..n-1]: Input array to be sorted.


 l, u: Lower and upper bounds of the values in A.
 D: A counting/distribution array of size u - l + 1.
 S[0..n-1]: Output array, sorted version of A.

✅ Step-by-step Explanation:

Step 1: Initialize Frequencies

text
CopyEdit
for j ← 0 to u−l do
D[j] ← 0

 This creates the D array with size equal to the number of possible distinct values:
u−l+1u - l + 1u−l+1.
 All counts are initialized to zero.

Step 2: Count Frequencies

text
CopyEdit
for i ← 0 to n−1 do
D[A[i] − l] ← D[A[i] − l] + 1

 For each value in A, increment its corresponding count in D.


 A[i] - l gives the index in D (since array values range from l to u).
 After this step, D[j] holds the number of occurrences of the value j + l in A.

Step 3: Compute Cumulative Counts (Distribution)

text
CopyEdit
for j ← 1 to u−l do
D[j] ← D[j − 1] + D[j]

 Convert the frequency array into a distribution array.


 Now D[j] tells how many elements are less than or equal to the value j + l.
 This helps in determining the final position of each value in the sorted array.

Step 4: Place Elements in Sorted Position (Right to Left)

text
CopyEdit
for i ← n−1 downto 0 do
j ← A[i] − l
S[D[j] − 1] ← A[i]
D[j] ← D[j] − 1

 We iterate backward through A to maintain stability (preserving relative order of


duplicates).
 D[j] - 1 tells the correct index in the sorted array S where A[i] should go.
 After placing it, we decrement D[j] to prepare for the next occurrence of that value.

Step 5: Return Sorted Array

text
CopyEdit
return S

 Finally, we return the sorted array S.

📌 Example:

Let’s take:

 A = [4, 6, 4, 5, 6]
 l = 4, u = 6

Then:
 Range size = 6 - 4 + 1 = 3, so D = [0, 0, 0] (for values 4, 5, 6)

Frequency count:

 D[0] = 2 (for value 4)


 D[1] = 1 (for value 5)
 D[2] = 2 (for value 6)

After cumulative sum:

 D = [2, 3, 5]

Now place elements from right to left using this distribution array.

Resulting sorted array: S = [4, 4, 5, 6, 6]

Let me know if you want this in pseudocode, a visualization, or a Python version!

4o

You said:

13 11 12 13 12 12 explain this example by using above algorithm


ChatGPT said:

Great! Let's walk through the Distribution Counting Sort algorithm step by step using your
example input:

✅ Input:
ini
CopyEdit
A = [13, 11, 12, 13, 12, 12]
l = 11
u = 13

We are sorting integers in the range 11 to 13.

📌 Step 1: Initialize D with zeros

Size of D is u - l + 1 = 13 - 11 + 1 = 3

So initially:
makefile
CopyEdit
D = [0, 0, 0] // represents counts for 11, 12, 13
index: 0 1 2
value: 11 12 13

📌 Step 2: Count frequencies

We count each element in A using D[A[i] - l]++

Process each element:

 A[0] = 13 → D[13 - 11] = D[2] → D[2] = 1


 A[1] = 11 → D[11 - 11] = D[0] → D[0] = 1
 A[2] = 12 → D[12 - 11] = D[1] → D[1] = 1
 A[3] = 13 → D[2] = 2
 A[4] = 12 → D[1] = 2
 A[5] = 12 → D[1] = 3

Now:

ini
CopyEdit
D = [1, 3, 2] // frequencies of 11, 12, 13

📌 Step 3: Compute cumulative (distribution) counts


text
CopyEdit
D[1] = D[0] + D[1] = 1 + 3 = 4
D[2] = D[1] + D[2] = 4 + 2 = 6

Now:

ini
CopyEdit
D = [1, 4, 6]

Interpretation:

 Value 11 goes to index < 1 → only 1 value


 Value 12 goes to index < 4 → positions 1, 2, 3
 Value 13 goes to index < 6 → positions 4, 5

📌 Step 4: Place elements into output array S

Initialize S = [_, _, _, _, _, _]
We process from right to left to maintain stability.

i = 5 → A[5] = 12 → j = 12 - 11 = 1

mathematica
CopyEdit
D[1] = 4 → place 12 at S[3], then D[1]--
S = [_, _, _, 12, _, _]
D = [1, 3, 6]

i = 4 → A[4] = 12 → j = 1

mathematica
CopyEdit
D[1] = 3 → place 12 at S[2], then D[1]--
S = [_, _, 12, 12, _, _]
D = [1, 2, 6]

i = 3 → A[3] = 13 → j = 2

mathematica
CopyEdit
D[2] = 6 → place 13 at S[5], then D[2]--
S = [_, _, 12, 12, _, 13]
D = [1, 2, 5]

i = 2 → A[2] = 12 → j = 1

mathematica
CopyEdit
D[1] = 2 → place 12 at S[1], then D[1]--
S = [_, 12, 12, 12, _, 13]
D = [1, 1, 5]

i = 1 → A[1] = 11 → j = 0

mathematica
CopyEdit
D[0] = 1 → place 11 at S[0], then D[0]--
S = [11, 12, 12, 12, _, 13]
D = [0, 1, 5]

i = 0 → A[0] = 13 → j = 2

mathematica
CopyEdit
D[2] = 5 → place 13 at S[4], then D[2]--
S = [11, 12, 12, 12, 13, 13]
D = [0, 1, 4]

✅ Final Sorted Array:


ini
CopyEdit
S = [11, 12, 12, 12, 13, 13]

You might also like