• 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
thegilbAsked:
Who is Participating?
 
fridomCEO/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
0
 
Infinity08Commented:
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
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Hi fridom,

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

  :~}

0
KuppingerCole Reviews AlgoSec in Executive Report

Leading analyst firm, KuppingerCole reviews AlgoSec's Security Policy Management Solution, and the security challenges faced by companies today in their Executive View report.

 
fridomCEO/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
0
 
PaulCaswellCommented:
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!

btw, about this code:

  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
0
 
fridomCEO/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
0
 
fridomCEO/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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.