MHB Function of Algorithms: Count & Sort

AI Thread Summary
The discussion centers on understanding the function of the `countSort` algorithm, which is a variation of the counting sort. The algorithm initializes a count array to track occurrences of each element in the input array, then accumulates these counts to determine the positions of elements in the sorted output. The second function, `sort`, calls `countSort` twice with different parameters, which is causing confusion regarding its implementation.A user expresses concern about an output issue when calling `countSort(arr, n, n)`, where an attempt to access an invalid index in the output array results in an error. This raises questions about the correct usage of the algorithm and how the accumulated counts are applied to sort the array. The discussion highlights the need for clarity on the algorithm's logic and proper index management to avoid out-of-bounds errors.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Wave)

Could you explain me the function of the following two algorithms? (Thinking)

Code:
int countSort(int arr[], int n, int exp)
{
    int output[n]; 
    int i, count[n] ;
    for (int i=0; i < n; i++)
       count[i] = 0;
    for (i = 0; i < n; i++)
        count[ (arr[i]/exp)%n ]++;
    for (i = 1; i < n; i++)
        count[i] += count[i - 1];
    for (i = n - 1; i >= 0; i--)
    {
        output[count[ (arr[i]/exp)%n] - 1] = arr[i];
        count[(arr[i]/exp)%n]--;
    }
    for (i = 0; i < n; i++)
        arr[i] = output[i];
}

Code:
void sort(int arr[], int n)
{
    countSort(arr, n, 1);
    countSort(arr, n, n);
}
 
Technology news on Phys.org
evinda said:
Hello! (Wave)

Could you explain me the function of the following two algorithms? (Thinking)

What happens if you compile and run it? (Wasntme) Add output statements in between to keep track of what the algorithm is doing.

Given that the name of the function is "countSort", my guess is that it's probably related to: Counting sort - Wikipedia, the free encyclopedia
 
PvsNP said:
What happens if you compile and run it? (Wasntme) Add output statements in between to keep track of what the algorithm is doing.

Given that the name of the function is "countSort", my guess is that it's probably related to: Counting sort - Wikipedia, the free encyclopedia

I wanted to apply the algorithm at this array:

View attachment 3716

After calling the function [m] countSort(arr, n, 1) [/m], we get this:

View attachment 3717

When I call then the function [m] countSort(arr, n, n) [/m], at this for loop:

[m]for (i = n - 1; i >= 0; i--)
{
output[count[ (arr/exp)%n] - 1] = arr;
count[(arr/exp)%n]--;
}
[/m]I get [m] output[-1]=arr[4] [/m].

But the array doesn't have such a position... (Worried)

Have I done something wrong? (Sweating)
 

Attachments

  • matrix.png
    matrix.png
    656 bytes · Views: 99
  • matrix.png
    matrix.png
    670 bytes · Views: 98
At the code, we count the number of occurences of each value and then the counts are accumulated..
How do we use the latter, in order to sort the array we are looking at?
I haven't understood the general idea...
Could you explain it to me? (Worried)
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top