Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6912647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:07:06+00:00 2026-05-27T09:07:06+00:00

I tried to write a countingsort, but there’s some problem with it. here’s the

  • 0

I tried to write a countingsort, but there’s some problem with it.

here’s the code:

int *countSort(int* start, int* end, int maxvalue)
{
    int *B = new int[(int)(end-start)];
    int *C = new int[maxvalue];

    for (int i = 0; i < maxvalue; i++) 
    { 
        *(C+i) = 0; 
    }
    for (int *i = start; i < end; i++) 
    { 
        *(C+*i) += 1; 
    }
    for (int i = 1; i < maxvalue-1 ; i++) 
    { 
        *(C+i) += *(C+i-1); 
    } 
    for (int *i = end-1; i > start-1; i--) 
    { 
        *(B+*(C+(*i))) = *i; 
        *(C+(*i)) -= 1; 
    }
    return B;   
}

In the last loop it throws an exception “Acces violation writing at location: -some ram address-“

Where did I go wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:07:07+00:00Added an answer on May 27, 2026 at 9:07 am
    for (int i = 1; i < maxvalue-1 ; i++) 
    

    That’s the incorrect upper bound. You want to go from 1 to maxvalue.

    for (int *i = end-1; i > start-1; i--) 
    { 
        *(B+*(C+(*i))) = *i; 
        *(C+(*i)) -= 1; 
    }
    

    This loop is also completely incorrect. I don’t know what it does, but a brief mental test shows that the first iteration sets the element of B at the index of the value of the last element in the array to the number of times it shows. I guarantee that that is not correct. The last loop should be something like:

    int* out = B;
    int j=0; 
    for (int i = 0; i < maxvalue; i++) {  //for each value
        for(j<C[i]; j++) {                //for the number of times its in the source
            *out = i;                     //add it to the output
            ++out;                        //in the next open slot
        }
    }
    

    As a final note, why are you playing with pointers like that?

    *(B + i)  //is the same as
    B[i]      //and people will hate you less
    
    *(B+*(C+(*i))) //is the same as
    B[C[*i]]  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've tried to write the smallest chunk of code to narrow down a problem.
I tried to write some sloppy jQuery code for an inline-confirm dialog. It works
I tried to write code using strrev() . I included <string.h> but still I'm
I've just tried to write line code to visualize a simple math; Here it
I tried to write some AR app. For now I wrote some code that
I tried to write a function to check key code with JavaScript. It's working
I tried to write a Neural Network system, but even running through simple AND/OR/NOR
I tried to write some text on a surface view I created. It works
I have a problem with my custom DataGridViewCell, indeed i tried to write to
I tried to write some javascript that takes the entries of an array and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.