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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:56:48+00:00 2026-05-26T22:56:48+00:00

I am using counting sort to sort a selection of numbers read from a

  • 0

I am using counting sort to sort a selection of numbers read from a file. This appears to work fine for files without duplicate entries, but once there are duplicates lots of zeros start to appear.

Do you have any idea why this is happening and how to fix it?

Examples:

Input (without duplicates):

1612 1894 3018 4212 6046 12894 13379 14408 14615 16394 17982 23004
27588 31393 33195 39526 54326 54566 67926 72479 90466 157832 703908

Output:

0: 1612 1: 1894 2: 3018 3: 4212 4: 6046 5: 12894 6: 13379 7: 14408 8:
14615 9: 16394 10: 17982 11: 23004 12: 27588 13: 31393 14: 33195 15:
39526 16: 54326 17: 54566 18: 67926 19: 72479 20: 90466 21: 157832 22:
703908

Input (with duplicates):

1612 1894 3018 4212 6046 12894 13379 14408 14615 16394 17982 23004
27588 31393 33195 39526 54326 54566 60000 60000 60000 60000 703908

Output (see 18++):

0: 1612 1: 1894 2: 3018 3: 4212 4: 6046 5: 12894 6: 13379 7: 14408 8:
14615 9: 16394 10: 17982 11: 23004 12: 27588 13: 31393 14: 33195 15:
39526 16: 54326 17: 54566 18: 0 19: 0 20: 0 21: 60000 22: 60000

Input:

2 2 2 2 2 3 3 3 3 3

Output:

0: 119 1: 110 2: 2025792 3: 3249376 4: 56 5: 2 6: 2 7: 2 8: 2 9: 2

Snippet of code being used to produce this output:

int *numbers = (int *) calloc(rows, sizeof(int));

int i = 0;
int max = -1; //min
int min = 500000000; //max
while(fscanf(inputPtr, "%d", &numbers[i]) != EOF) {
  //printf("%d\n", numbers[i]);
  if(numbers[i]>max) max = (int) numbers[i];
  if(numbers[i]<min) min = (int) numbers[i];
  i++;
}

countingSort(numbers, rows, max, min);

void countingSort(int array[], const int end, const int max, const int min) {
  int i;
  const int range = max-min+1;
  int count[range+1],
      scratch[end];

  for(i=0; i<range+1; i++)
    count[i] = 0;

  /* Set the value of count[i] to the number of
   * elements in array with value i+min-1. */
  for(i=0; i<end; i++) {
    int c = array[i]-1-min;
    count[c]++;
  }

  /* Update count[i] to be the number of
   * elements with value less than i+min. */
  for(i=1; i<range; i++)
    count[i] += count[i-1];

  /* Copy the elements of array into scratch in
   * stable sorted order. */
  for(i=(end-1); i>=0; i--) {
    int c = array[i]-min;
    int s = count[c];
    scratch[s] = array[i];
    /* Increment count so that the next element
     * with the same value as the current element
     * is placed into its own position in scratch. */
    count[c]++;
  }

  for(i=0; i<end; i++)
    array[i] = scratch[i];
}

Full code:

#include <stdio.h>  // FILE stderr fopen fclose fprintf printf fgets
#include <stdlib.h> // Standard Lib :-)
#include <math.h> // Every body was kung fu math

/**
 * Given any number of program parameters (command-line parameters)
 * calculates the length of each one, and writes the longest to standard output
 * this time using counting sort!
 */

/*
 * Output the usage
 */ 
void usage () {
  printf("Usage:\n"
         "Usage: part2 rows fileToRead\n");
}

/**
 * counting sort
 * http://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Counting_sort
 */
void countingSort(int array[], const int end, const int max, const int min) {
  int i;
  const int range = max-min+1;
  int count[range+1],
      scratch[end];

  for(i=0; i<range+1; i++)
    count[i] = 0;

  /* Set the value of count[i] to the number of
   * elements in array with value i+min-1. */
  for(i=0; i<end; i++) {
    int c = array[i]-1-min;
    count[c]++;
  }

  /* Update count[i] to be the number of
   * elements with value less than i+min. */
  for(i=1; i<range; i++)
    count[i] += count[i-1];

  /* Copy the elements of array into scratch in
   * stable sorted order. */
  for(i=(end-1); i>=0; i--) {
    int c = array[i]-min;
    int s = count[c];
    scratch[s] = array[i];
    /* Increment count so that the next element
     * with the same value as the current element
     * is placed into its own position in scratch. */
    count[c]++;
  }

  for(i=0; i<end; i++)
    array[i] = scratch[i];
}

/**
 * return the appropriate percentile
 */
int getPercentile(double percent, int *array, int array_size) {
  int number = (floor(percent*array_size)+1); //-1 for arrays

  //Make number suitable for arrays
  number -= 1;

  //printf("%d\n", number);

  //Ensure the numbers aren't the same
  while(array[number-1]==array[number]) {
    number++;

    if(number>=array_size)
      return -1;
  }

  //printf("%d\n", number);

  return (int) array[number];
}

/**
 * The main method
 * part2 1000 datafile
 */ 
int main (int argc, char *argv[]) {
  //I need three args
  //1 rows
  //2 file name
  if(argc==3) {
    int rows = 0;     
    if(sscanf(argv[1], "%d", &rows) == 0) {
      printf("The number of rows must be a number.\n");
      usage();   
      exit(-1);       
    }

    FILE *inputPtr; //File pointers
    inputPtr = fopen(argv[2], "r"); //same as PHP ;-)

    // Sensible errors
    if(inputPtr==NULL) {
      printf("I could not open the file for input.\n");
      usage();
      exit(-1);
    }

    // Read all the numbers
    int *numbers = (int *) calloc(rows, sizeof(int));

    int i = 0;
    int max = -1; //min
    int min = 500000000; //max
    while(fscanf(inputPtr, "%d", &numbers[i]) != EOF) {
      //printf("%d\n", numbers[i]);
      if(numbers[i]>max) max = (int) numbers[i];
      if(numbers[i]<min) min = (int) numbers[i];
      i++;
    }

    printf("%d, %d, %d\n\n", min, max, rows);

    //int array[], const int end, const int max, const int min) {
    countingSort(numbers, rows, max, min);

    for(i = 0; i < rows; i++) {
      printf("%i: %d\n", i, numbers[i]);
    }  
    printf("-------\n\n");

    // Close the files
    //fclose(inputPtr);
    //not pointing to file anymore

    int percentile = getPercentile(0.9, numbers, rows);

    printf("%s: %d\n", argv[2], percentile);

  } else {
    printf("Please provide two CLI's!\n");
    usage();
    return 0;
  }


  /* All done */
  return 0;

}
  • 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-26T22:56:48+00:00Added an answer on May 26, 2026 at 10:56 pm

    Here’s a mistake:

    /* Set the value of count[i] to the number of
     * elements in array with value i+min-1. */
    for(i=0; i<end; i++) {
        int c = array[i]-1-min;
        count[c]++;
    }
    

    If the comment is correct, you ought to be setting c = array[i]+1-min;. Nothing else jumped out.

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

Sidebar

Related Questions

I have a small problem i'm using this code to start counting from 20
I have this C code for counting nearest prime-number using this method: int countPrime(int
i've written written a code for counting each byte frequency in binary file. Using
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
I have this very huge XML file of size 2.8GB. This is Polish Wikipedia's
In my program I am setting up core foundation variables using automatic reference counting.
In using PHP Prepared Statement, what is the syntax for counting number of rows?
I'm currently counting all the checked checkboxes in an asp.net gridview using: $('#cphMain_gdvSalesOrder').delegate('input:checkbox', 'click',
I have an objective-C program and I am using ARC (Automatic Reference Counting), it
Basically, I'm doing some sort of image processing using a screen-sized rectangle made of

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.