What sort algorithm to choose

I want to sort 12 Full Names alphabetically. What algorithm to choose?

I use char array to store the names. I also use functions and array of struct.

It will be helpfull if you provide me with any link you know with simple sourcecode example. Please consider the spaces in the char

Example:

Jennifer Moss
Anthony Calvin
James John
Thomas Christopher
Steven Donald
Anthony Kevin

to

Anthony Calvin
Anthony Kevin
James John
Jennifer Moss
Steven Donald
Thomas Christopher
std::sort http://www.cplusplus.com/reference/algorithm/sort/

And notice that for 12 items doesn't matter which algorithm you use - unless you chose bogosort ;^)
The example in the link is with integer. It will work with char or string ?
There is simpler way to do it ?

Do i have to use vectors ?
Last edited on
There is nothing complicated about this
you don't need vectors, pointers will do:
example:
1
2
int my_array[] = {2, 5, 7, 9, 0, 1};
std::sort(my_array, my_array + 6);

If you're sorting std::strings, that will do. If you want to sort some object or char* you will have to either write operator < or a comparison function.
Yes it will work with strings.
Sort algorithm takes only 2 mandatory parameters, the beginning and the ending of the range to be sorted. How much more simplified can it get?
You do not have to use vectors. You may just use arrays as well.

See example code below for using sort with arrays (code not compiled and tested)

1
2
3
4
const int size = 10;
string names[size];
//Add strings to array
sort(names, names+size);
Last edited on
Ok, thanks all very much for the info!
If you're sorting std::strings, that will do.


Actually I also used to think for built-in datatype the sort will do but if I have a business user defined sorting order for them, I also need to either write operator < or a comparison function.

E.g
User want int 1 , 5 to be in-front and all other int values to sort behind 1 and 5. This is what I mean by customized sorting order.
I got the easy way..I tried with the sort function but have some problems...
Since you are using char* instead of std::string, you must supply the third argument to the sort algorithm to properly sort the names. Your compare function should look like this:

1
2
3
4
5
6
bool const_char_less( const char* lhs, const char* rhs )
  {
  // Return true if and only if the LHS is strictly alphabetically less than the RHS.
  // There are functions (like strcmp() in <cstring>) to do this, or you 
  // can write your own...
  }

Then you can sort your array of char*

1
2
3
4
5
std::sort(
  my_array_of_names,      // The beginning of the array
  my_array_of_names + N,  // The end, where N is the number of names in the array
  const_char_less         // The function to use to compare two elements
  );

All said, however, you will have a much easier time if you do as suggested above to use std::string instead of char arrays.

Hope this helps.
Ok, i understand. Thanks a lot Duoas!
Topic archived. No new replies allowed.