How to implement Counting Sort to sort an array of char in C?

So, I’m trying to implement the Counting Sort algorithm to sort an array of digits given by the user, here’s my code:

#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
int i,n=15;
char arr[]={"ABEFGEAGCEDBFAC"}; //I'm using this string to test it out
/*for(int i=0; i<n; i++){
printf("Insert a letter %d: ",i+1);
scanf("%c",&arr[i]);
fflush(stdin);
}
*/
printf("Array: n[ ");
for(i=0; i<n; i++){
if(i<n-1)
printf("%c | ",arr[i]);
if(i==n-1)
printf("%c ]n",arr[i]);
}
printf("n");
int range;
for(i=0;i<7;i++){
range[i]=0;
}
for(i=0; i<n; i++){ //I've seen other ways to do this, but I just need it to go from A-G
//not sure if this is the problem.
switch(arr[i]){
case 'A':
range++;
break;
case 'B':
range++;
break;
case 'C':
range++;
break;
case 'D':
range++;
break;
case 'E':
range++;
break;
case 'F':
range++;
break;
case 'G':
range++;
break;
}
}

printf("Counting array (number of times it appear in the array): n[ ");
for(i=0; i<7; i++){
if(i<6)
printf("%d | ",range[i]);
if(i==6)
printf("%d ]n",range[i]);
}
printf("n");

for(i=1;i<7;i++){ //Here I do the sum of all elements in the array
range[i] = range[i] + range[i-1];
}

printf("Sum of the array: n[ ");
for(int i=0; i<7; i++){
if(i<6)
printf("%d | ",range[i]);
if(i==6)
printf("%d ]n",range[i]);
}
printf("n");
char ord;

for(i=n-1;i>=0;i--)
ord[--range[arr[i] - 'A']] = arr[i];
for (i=0;i<n;i++){
printf("%c ",ord[i]);
}
/*
printf("Ord: n[ ");
for(int i=0; i<15; i++){
if(i<14)
printf("%c | ",ord[i]);
if(i==14)
printf("%c ]n",ord[i]);
}
*/
}

So, as you can see, if I’m not wrong, the error is when I try to put each letter in its correct position using the range array in the second to last for sentence. I’ve seen other ways to implement it but I just can’t get it done, it’s crashing as soon as I try to print ord[i].
I’m having trouble trying to fully understand what’s going on in that for.

Update:
Tried to implement fish-404’s corrections on the code as shown above. Can’t get it properly implemented.

Last Update:
Code above is now finished and fully functional. Thanks to @fish-404 .

Stack Overflow Asked by Izanami on December 30, 2020

In this line, arr[i] is a char, you try to use char as the array range index.

ord[range[arr[i]]] = arr[i];

Update

If I get you correctly, I think you want use the arr[i] char as a index to search in range array.

As you can see, the range array you use count the letter, to use the letter to index, you just use arr[i]-'A' to count the letter order in the range array.

for (i = n; i >= i; i--)
ord[--range[arr[i] - 'A']] = arr[i];

// use this to see result
for (i = n;i >= 1; i--)
printf("i: %d, ord[i]: %cn", i, ord[i-1]);

Correct answer by fish-404 on December 30, 2020

Related Questions

How Can I Not Double Count Rows And Objects In My Query?

3  Asked on November 22, 2021 by ziegler199

Unable to integrate Cypress with testmanagement tool

2  Asked on November 22, 2021 by muller

0  Asked on November 20, 2021 by artisan

External CSS not linking for all elements

1  Asked on November 20, 2021 by stratos-la

Why is ‘u{1D11E}’.charAt(0) not equal to ‘u{1D11E}’?

2  Asked on November 20, 2021 by json-prime

E: Unable to locate package python3-boto3

2  Asked on November 20, 2021 by rosi-darmawati

How to handle unhandled exceptions in windows service?

0  Asked on November 20, 2021 by suman_dh

How do I go about adding a javascript animation to a .svg image?

2  Asked on November 20, 2021 by varun-jain

Better way to wrap specific Word of div?

2  Asked on November 20, 2021

Why are my separate random objects being assigned the same values?

1  Asked on November 20, 2021

x and o in tic tac toe keeps alternating

0  Asked on November 20, 2021 by channee-mathmath

Remove using a button

2  Asked on November 20, 2021 by gastn

Typescript: What is the difference between if(variable), if(!variable) and if(!!variable)?

1  Asked on November 20, 2021 by syroman

remove 0 and add ‘ ‘ in every number in pandas

1  Asked on November 20, 2021 by spt-hsb

nav-tabs doesn’t change on click

0  Asked on November 20, 2021 by beureu

Stick element on bottom on page scroll

3  Asked on November 20, 2021

Exchange each 100 in a dataframe for another value

1  Asked on November 20, 2021 by tarik-benrabah

Proper way of getting boolean from option value for Javascript

2  Asked on November 20, 2021 by avow-studio

Can I see the SQL in progress when an EntitySqlException is thrown, or can I override an EntityCommandCompilationException?

1  Asked on November 20, 2021 by peter-bailey

GraphQL & Using a nested subscribe function

1  Asked on November 20, 2021 by u-rogel