-3

I was given this by my teacher:

int[] letters = { 'c', 's', 'a', 'k', 'x', 'l', 'j' };

and told that I should convert the chars to int and then bubble-sort them alphabetically and back to char and save and print the array alphabetically.

I've only every come across bubble sorting when it deals with numbers but not when using chars. Any assistance or enlightenment is greatly appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Carl
  • 11
  • 1
  • 1
  • 3
    Any effort to solve it? Homework questions are okey as far as you showed your effort. http://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions – Soner Gönül May 28 '15 at 11:27
  • possible duplicate of [bubble sort a character array in alphabetic order in c](http://stackoverflow.com/questions/9809839/bubble-sort-a-character-array-in-alphabetic-order-in-c) – Baximilian May 28 '15 at 11:29
  • Yes and no, I've been going through my course litterature but I can't make out the connection of the conversion as the only thing I've encountered so far when it comes to sorting is numbers. So I'm rather lost. Not asking for someone to write the whole code for me but rather point me in the right direction with some helpful tips – Carl May 28 '15 at 11:30
  • Converting is definitely unnecessary. Characters are ordered, just like numbers. – Dennis_E May 28 '15 at 11:33
  • Will all the characters be in the same case? Are you guaranteed to not have non-alpha characters? Also google ASCII table to find the numerical values of characters. – juharr May 28 '15 at 11:35

2 Answers2

3

and told that I should convert the chars to int

Probably you didn't comprehend what he told you. In a computer "characters" are already numbers. There aren't special memory cells that have the shape of the character A.

You can:

char ch = 'A';
int num = ch;

or you can:

char ch1 = 'A';
char ch2 = 'B';

if (ch1 < ch2)
{
}

and for example (but it isn't needed for what you want to do):

char ch1 = 'A';
char ch2 = (char)(ch1 + 1); // 'B'

(note the final casting: when you do math operations on a char, it is implicitly converted to an int)

and so on.

Note that the opposite isn't true:

char ch = 'A';
int num = ch;
char ch2 = num; // COMPILATION ERROR

you need a cast:

char ch2 = (char)num;

(technically the correct definition is that there is an implicit cast conversion from char to int (used in the first and third examples) and there is an explicit cast conversion from int to char (used in the last example).

(technically [2] chars should be sorted through the use of a collation (that is a special ordering for strings that knows that e < è < f) but this is something a little overboard for a school exercise about bubble sorting)

xanatos
  • 109,618
  • 12
  • 197
  • 280
1
// init array
char[] letters = { 'c', 's', 'a', 'k', 'x', 'l', 'j' };

// bubble sorting...
int count = letters.Length;
bool swapped;
do
{
    swapped = false;
    for (int i = 0; i < count - 1; i++)
    {
        if (letters[i] > letters[i + 1])
        {
            char c = letters[i];
            letters[i] = letters[i + 1];
            letters[i + 1] = c;
            swapped = true;
        }
    }
    count--;
} while (swapped);

string result = string.Join(", ", letters);

Result

a, c, j, k, l, s, x

General-Doomer
  • 2,681
  • 13
  • 13