• C

# Sort an array of characters. NEWBIE QUESTION, PRO POINTS (500) AVAILABLE

I need a program that sorts an array of 8-bit signed integers (characters).  The values should go from smallest to greatest.  The array can be of arbitrary size.  The program will take as input a pointer to the array and the size of the array.  The sorted values need to be stored in the original array.

Example:

void      Sort(char *pArray, int nSize)
{
//do sorting
}

Please note that I will award 500 points to the _first_ person to answer my question to the spec. The solution must be in ANSI C and it must be clean and efficient.
LVL 5
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEO/ProgrammerCommented:
No problem use qsort:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int char_compare_function (const void *a, const void *b){
const char *p1 =  a;
const char *p2 = b;

if (*p1 < *p2){
return -1;
}else if (*p1 == *p2){
return 0;
}else{
return 1;
}
}

static void Sort (char * buf, size_t buf_len) {
qsort(buf, buf_len, 1, char_compare_function);
}

int main(void){

char some_string[] = "one two three the you will get catched";
printf("%s sorted = '", some_string);
Sort(some_string, strlen(some_string));
printf("%s' \n", some_string);

return 0;
}

Friedrich

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
And if you want/need to implement this yourself (in case this is homework), then check this page :

http://en.wikipedia.org/wiki/Quicksort

especially the in-place algorithm
DBACommented:

Hi fridom,

This looks suspiciously like homework to me......

:~}

CEO/ProgrammerCommented:
Yeah, seems to be the case. But I can not revert it I just figured out after posting. I had this code more or less lying around. And I have neither seen the NEWBIE first nor the second post hex to decimal. But the code still is ok, I assume.

Friedrich
Commented:
Hi fridom,

>>This looks suspiciously like homework to me......

I was a little concerned when I started reading your post but by the time I got to the end I felt it was ok! :-) Asker will need to do the sorting in code, qsort will probably not be an acceptable solution but it serves as a good example of a simple framework for asker to start with. Both guiding and helpful. Good post!

const char *p1 =  a;
const char *p2 = b;

if (*p1 < *p2){
return -1;
}else if (*p1 == *p2){
return 0;
}else{
return 1;
}

I'd do it like this:

const char c1 =  *a;
const char c2 = *b;

if (c1 < c2){
return -1;
}else if (c1 == c2){
return 0;
}else{
return 1;
}

Is there some kind of extra optimisation available using const *?

Incidentally, I dont think 'const' is available in many raw C compilers. Many of the C++/C hybrids can handle it though.

Paul
CEO/ProgrammerCommented:
const is Standard C every C compiler supports it.
see e.g the Standard 6.4.1 (keywords)

keyword: one of auto break case char

const

continue default do double else enum extern float for goto if inline int long register restrict return short signed sizeof static struct switch typedef union unsigned void volatile while _Bool _Complex _Imaginary

This code however won't work

const char c1 = * a;

Because you can not access a void Pointer that way you would have to write somethign like

int char_cmp_fun (const char *a, const char* b)

or
const char c1 = (char*) a;

but casting is nearly always a bad idea, (although you can not get away with it in Windows programming e.g.)

Then this could work but not with the original void Pointer.

Regards
Friedrich
CEO/ProgrammerCommented:
Well it's advantage is that it fullfills the requirements up to the point ;-)

Now we'll see how much worth his writings are ;-)

Have a nice day hacking
Friedrich
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.