0
namespace cs19 {

void counting_sort(int *array, unsigned array_len) {
  int lowest, largest, array_range, placer, num;
  int q = 0;
  for(int i = 0; i < array_len; ++i) {
    if(i == 0) {
      lowest = array[i];
      largest = array[i];
    }
    if (largest < array[i]) largest = array[i];
    if (lowest > array[i]) lowest = array[i];
  }
  if(largest > -1 && lowest < 1) array_range = (largest - lowest) + 1;
  else {
    array_range = largest - lowest;
  }

Having trouble with the below part of my code I'm trying to make an array with a range of numbers from the smallest value to the largest. However, when I run my code it doesn't assign the numbers to the array. My teacher wants us to use pure c++ code without any other added functions or else I would of most likely used a vector.

  int *tally_array = new int[array_range];
  for (int j = lowest; j <= largest; ++j) {
    tally_array[q] = j;
    ++q;
  }
  num = 0;
  for (int g = 0; g < array_range; ++g) {
    for (int a = 0; a < array_len; ++a) {
      if(array[a] == tally_array[g]) {
        placer = array[num];
        array[num] = tally_array[g];
        array[g] = placer;
        ++num;
      }
    }
  }
  delete[] tally_array;
}

}
12
  • "pure C++" without using the other parts of C++, and using new[] and delete[]... sounds like you are in a very advanced C++ class. Getting down-and-dirty with the unseemly underbelly of C++ and all the fiddly gory details. 3rd year C++? 4th? Commented Apr 13, 2022 at 23:15
  • 1
    Is this assignment about creating a home-made vector class, or about implementing a counting sort? I really don't understand these nutty requirements that you can't use vector -- the counting sort isn't going to magically get written for you if you used vector. It's courses and teachers with restrictions like this is the reason so many new programmers drop C++ and go to other languages, such as Python. Commented Apr 13, 2022 at 23:18
  • I recommend bashing this into a real minimal reproducible example. Currently I have questions like, "In the second code block, was q initialized to 0?" that would be non-questions with a complete example. Commented Apr 13, 2022 at 23:18
  • 1
    Be worth your time looking into godbolt.org/z/jKGhY9esv Something marched out of range and trashed the array at tally_array. Commented Apr 13, 2022 at 23:32
  • 2
    My advice would be to start by getting the code working, using whatever would be most convenient (including vector). Then work on writing minimal replacements for those parts you're not allowed to use. As an aside, this code doesn't look very close to how I'd expect a counting sort to look. Commented Apr 13, 2022 at 23:41

1 Answer 1

1

It doesn't look to me like your code implements (at least what I'd normally think of as) a counting sort.

So, to give an idea of the general kind of thing you're trying to accomplish, here's a counting sort (that works), but using all sorts of stuff your teacher apparently won't accept, so you can't turn it in directly.

void counting_sort(int* array, unsigned array_len) {
    std::map<int, unsigned> tallies;

    for (unsigned i = 0; i < array_len; i++)
        ++tallies[array[i]];

    unsigned pos = 0;
    for (auto [value, count] : tallies)
        for (int j = 0; j < count; j++)
            array[pos++] = value;
}

We start by walking through the input array, incrementing a count for each value in the input (in this case, just using a map as a sparse array).

Then we walk through the array of counts, and write the appropriate number of each value into the array, in order.

Here's a quick test:

int main() {
    std::vector<int> inputs { 2, 1, 2, 1, 0, 3, 0 };

    counting_sort(inputs.data(), inputs.size());

    for (int i : inputs)
        std::cout << i << ' ';
}

...and here's output from that test:

0 0 1 1 2 2 3

To reiterate the basic idea of the algorithm: we start with an array of counts. We use that to count the number of times each value in the input array occurs. Since it contains counts, each value in it should start out at zero. We walk through values in the input array, and use those to index into the tally array, incrementing the location in the tally array by one when we encounter a value.

When we're done with that, the first item in the array should contain the number of times the lowest value in the input occurred, the second number of times lowest+1 occurred (which may easily be 0), and so on.

Then we walk through the array of tallies. The index we're using is the value that came from the input array, and the value in the tally is the number of times that value occurred. So, we write that value out the appropriate number of times, then proceed to the next. When we reach the end of the array, we're done.

The big difference in your case is how we store the tallies. I've used a map<int, unsigned>, which acts like a sparse array--that is, although it can store a value associated with any int, it only really stores the values we've written there, so any value that wasn't in the input array won't be in our tally either.

To satisfy your teacher's proclivities, when you write it you're apparently going to use new to allocate a block of data using new1. So you will store all the intermediate values. You want to start with them all set to 0. Then when you encounter an input value, you use it as an index, and increment that value in the tallies array.

Then you'll do just about like above, walking through the tallies array, and writing values back to the input array the proper number of times. Since you started each count at zero, any that weren't present will be written out zero times, which translates to them not being written at all.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.