1

I have to sort a set of strings alphabetically without any built-in functions and then print them on the screen. I have tried converting first char of the string to a number value and then compare this value with other first char's of a string, but I don't think that's a good option.

(something like that)

for (int i = 0; i < numberOfStrings; i++)
{
  for (int j = i + 1; j < numberOfStrings; j++)
  {
    if ((int)string1[i] > (int)string2[i]         
    {
      biggestFirstChar = (int)string1[i];
    }
  }
}

So I'd like to know what's an easy way to sort multiple strings alphabetically without any functions. This is a school assignment.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Fihii
  • 75
  • 2
  • 9
  • https://stackoverflow.com/questions/30010927/sort-list-of-strings-alphabetically-in-net – vadzim dvorak Jan 19 '18 at 14:10
  • 3
    Don't cast `char` values to `int`. You can just compare them as `char`s. Also, this method will compare their Unicode values, but may not give you "natural" sort order – JamesFaix Jan 19 '18 at 14:10
  • 1
    Read about bubble sort or quicksort, there are others too – The One Jan 19 '18 at 14:10
  • 1
    Try looking at different sorting algorithms. Perhaps look at bubble sort, it is quite easy to understand. – Malte R Jan 19 '18 at 14:10
  • Search for [Bubble Sort](https://www.google.com/search?q=bubble+sort). Very easy to implement. – 001 Jan 19 '18 at 14:11
  • 1
    Thank you for your answers. I will look up bubble sort. – Fihii Jan 19 '18 at 14:15
  • ps. A question for your tutor... When they say "without using built in functions", do they mean "without using built in sort functions"; i.e. I'd interpret this question as the latter, so would still feel comfortable using the string comparison functionality... or are they literally restricting you to C#'s keywords list (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/)? FYI: You can do string comparison just using operators too... but if I were the tutor I'd prefer to see students making use of the comparison functionality since that teaches better OO practice. – JohnLBevan Jan 19 '18 at 14:50
  • We aren't allowed to use the string comparison functionality either. I don't understand why, though. – Fihii Jan 19 '18 at 14:57

2 Answers2

7

Since this is a homework question, I'll given an enabling answer rather than the actual code version...

Approach to working out the algorithm

  1. Get a sheet of paper, rip it into several pieces, then write one word on each piece.
  2. Put the bits of paper one after the other (e.g. so they represent an unsorted array)
  3. Work your way through the list putting items in order. Try to take a procedural approach; i.e. so you're obeying a rule each time you make a decision, rather than "magically"/intelligently jumping to the next step.
  4. Try to write down the rules you're following.
  5. Jumble up the list again, and repeat, exclusively following the rules you've written; does it work, or are you missing any rules / are there rules which didn't make sense.
  6. At this point your list of rules is essentially pseudo-code; i.e. you have a set of instructions which can be followed to get your result; but it's not yet written in any recognised programming language.
  7. Translate those rules / the pseudo-code to actual code.
  8. Try it out. If it doesn't work, try to work out where it's going wrong. If you can't see what's wrong, come on here, post that code, and state your issue; we can then help get you back on the right path.

NB: You can of course look up existing sorting algorithms and implement those; but personally I prefer the above since it teaches you to think for yourself, rather than translating an existing algorithm into your particular coding language.

Other Hints

When making comparisons, consider what you need to do. i.e. You've asked above about converting the characters in the strings to numbers before comparing them. Is that step required; could you not simply compare the strings alphabetically? https://www.google.co.uk/search?q=c%23+compare+strings+alphabetically

In the code sample you've provided you have values string1 and string2, but you never show how their values' are assigned... Do you need to assign a value to a variable before you can compare it / when and where should those assignments take place?

You also have a loop with integer j being assigned... Where are you making use of that value / what's the point of assigning it a value unless you're going to use it?

Likewise you have a variable biggestFirstChar which you assign a value, but don't do anything with. Is that needed / where would you use it?

The result you're after is a sorted list of values, which you then want to output. Where are you holding that result / where's the code to output the results in order?

How do you want to build up that result list; i.e. are you able to populate it all at once, or do you have to modify it bit by bit until all items are in order?

Additionally, break the issue down into chunks and test each chunk and/or think about the implications. i.e.

  • How do you produce output?
  • How do you compare 2 strings to determine which should be first in a list?
  • How do you output an array (list)?
  • How do you add items to your result list (or how do you swap values in an existing list if you're working on the original)?
  • Given an array (list) with 2 elements (items), write code to put them in order. Ensure this works regardless of the initial order (Aardvark, Balloon and Balloon, Aardvark).
  • Now extend this to work for a list of 3 items; but ensure your solution will still work if given a list of 2 items.
  • Now try to solve the original issue
  • Finally (optional; for bonus points) look at your code and see if you can make it better (i.e. refactor it). This means looking at simple presentation (have you indented your code / are you using meaningful variable and function names, did you comment anything which isn't self explanatory, etc), and also looking for ways to improve it (can you see something inefficient where a different approach may work better / is any code repeated which could be moved out into a different function and called from all the places it's required?
Community
  • 1
  • 1
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
1

What you need is two methods, a bubble sort (or any sort algorithm) and algorithm for string comparison.

you already know bubble sort, so I go for string comparison.

int Compare(string x, string y)
{
    if(x == y) return 0;
    if(x == null) return -1;
    if(y == null) return 1;

    int i = 0;
    int length = x.Length <= y.Length ? x.Length : y.Length;

    while(i < length)
    {
        if(x[i] > y[i]) return 1;
        if(x[i] < y[i]) return -1;
        i++;
    }

    if(x.Length == y.Length) return 0;
    if(x.Length > y.Length) return 1;
    else return -1;
}

Simply use this comparison algorithm in your bubble sort algorithm.

To compare two strings

if(Compare(str1, str2) >= 0)
{
    // str1 is bigger or equal
}
else 
{
    // str2 is bigger.
}
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118