Function of Algorithms: Count & Sort

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Algorithms Function
Click For Summary

Discussion Overview

The discussion revolves around understanding the function and implementation of two algorithms, specifically a counting sort algorithm and its application. Participants are exploring the mechanics of the code provided, including its behavior when executed with specific inputs.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant requests an explanation of the counting sort algorithm, indicating a need for clarification on its function.
  • Another participant suggests adding output statements to track the algorithm's execution, implying that understanding the output is crucial for grasping the algorithm's behavior.
  • A participant expresses confusion about the results of calling the function with specific parameters, noting an issue with accessing an invalid array index.
  • There is a request for clarification on how the accumulated counts are utilized to sort the array, indicating a lack of understanding of the overall sorting process.

Areas of Agreement / Disagreement

Participants appear to have varying levels of understanding regarding the counting sort algorithm, with some expressing confusion and seeking clarification. No consensus is reached on the correct interpretation or application of the algorithm.

Contextual Notes

Participants have not resolved the issues related to the invalid array index and the general sorting mechanism of the algorithm. There are also assumptions about the input array that have not been explicitly stated.

Who May Find This Useful

This discussion may be useful for individuals learning about sorting algorithms, particularly counting sort, and those seeking to understand algorithm implementation and debugging in programming.

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: 116
  • matrix.png
    matrix.png
    670 bytes · Views: 111
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)
 

Similar threads

  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K