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 8822407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:06:04+00:00 2026-06-14T06:06:04+00:00

I have a problem with Counting sort. When i will sort table sometimes it

  • 0

I have a problem with Counting sort. When i will sort table sometimes it work but sometimes no (It doesn’t work when k == size) and i don’t know why. Please help. This is my code:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAX 100000
int tabA[MAX];
int tabB[MAX];
int tabC[MAX];
int k;

void scope(int size){
int i, max = 0, min;

min = tabA[1];

    for(i = 1; i <= size; i++){
        if(tabA[i] > max)
            max = tabA[i];
        if(tabA[i] < min)
            min = tabA[i];
    }

    k = max - min + 1;
}

void rand1(int size){
    int i;
    srand(time(NULL));

    for(i = 0; i <= size; i++)
        tabA[i] = rand() % 8;
} 

void countingsort(int size){
    int i;

    for(i = 0; i < k; i++)
        tabC[i] = 0;

    for(i = 0; i < size; i++)
        tabC[tabA[i + 1]] = tabC[tabA[i + 1]] + 1;

    for(i = 1; i <= k; i++)
        tabC[i] = tabC[i] + tabC[i - 1];

    for(i = size; i >= 1; i--){
        tabB[tabC[tabA[i]]] = tabA[i];
        tabC[tabA[i]] = tabC[tabA[i]] - 1;
    }   
}

int main(void){
    int size, i;

    printf("Please write how many numbers you wont to sort: ");
    scanf("%d", &size);

    rand1(size);
    scope(size);
    countingsort(size);

    printf("Numbers to sort:\n");
    for(i = 1; i <= size; i++)
        printf("%d\n", tabA[i]);

    printf("\nK: %d\n\n", k);

    printf("Sorting numbers:\n");
    for(i = 1; i <= size; i++)
        printf("%d\n", tabB[i]);

    return 0;
}

Thank’s for all help.

  • 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-06-14T06:06:05+00:00Added an answer on June 14, 2026 at 6:06 am

    At least this code:

    for(i = 0; i < k; i++)
        tabC[i] = 0;
    
    for(i = 0; i < size; i++)
        tabC[tabA[i + 1]] = tabC[tabA[i + 1]] + 1;
    
    for(i = 1; i <= k; i++)
        tabC[i] = tabC[i] + tabC[i - 1];
    

    Has the problem, that in the first, intialization loop i goes 0..k-1, but in the last loop i goes from 1..k, while assuming tabC is initialized. Also, middle loop assumes range of tabA values is 0..k, which it might not be (looking at randomization function, it actually probably is, but this is just co-incidence I suppose).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem counting a table joined several times. The question table :
I am having a problem counting my rows in Konkurrancers table. I have tried
So... I have a problem with counting all ccurrences of preg_match and summing/addition first
I have problem SIMILAR to preventing form data reposting, but not quite the same
I have a problem about counting the length of a string. I always get
While my initial problem counting the running number of rows in a table has
I have problem with a counting column in my view. SELECT ColumnC, ColumnA %
I have problem with joining tables, here are example tables: Table A: ( 30
Based off this SO question about counting forgein keys , I have a problem.
I need some advice. I know how to solve this problem clumsily, but I

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.