Skip to content

Commit

Permalink
Bug Fix: Updating my previously merged PR -> counting-sort.md (#6242)
Browse files Browse the repository at this point in the history
* Update counting-sort.md

* Fix lint issue

---------
  • Loading branch information
JustinhSE authored Mar 2, 2025
1 parent 611687c commit 6c0bd6c
Showing 1 changed file with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,38 @@ CatalogContent:

## Explanation

1. Find the maximum value:
### Find the maximum value

- Iterate through the input array `data` to find the largest element `max`.
- This determines the range of values needed to be counted.

1. Create the count array:
### Create the count array

- Initialize a new array `count` with a `max + 1` size to hold each value's frequency.
- The size is `max + 1` so that the `max` element is accounted for.
- Set all elements in `count` to _0_ initially.

1. Count the occurrences:
### Count the occurrences

- Loop through the `data` array again.
- For each element `data[i]`, increment the corresponding count in the `count` array (`count[data[i]]++`).
- This is to document the frequency distribution of elements in `data`.

1. Create the start array:
### Create the start array

- Initialize a new array `start` of the same size as `count`.
- Set `start[0]` to _0_.
- Based on the frequency distribution found in `count`, the starting indexes can be determined and stored in the `start` array (`start[j] = start[j-1] + count[j-1]` for `j=1` to `max`).

1. Place elements in sorted order:
### Place elements in sorted order

- Iterate through the `data` array again.
- For each element `data[a]`:
- Retrieve its corresponding position to the `temp` [variable](https://www.codecademy.com/resources/docs/java/variables) from the `start` array (`temp = start[data[a]]`).
- Place the element in the `result` array at that position (`result[temp] = data[a]`).
- Increment the `start` value for the next element with the same value (`start[data[a]]++`).

1. Return the sorted array:
### Return the sorted array

- The `result` array now contains the elements of the original `data` array in sorted order.
- Return the `result` array.
Expand Down

0 comments on commit 6c0bd6c

Please sign in to comment.