468,738 Members | 2,531 Online

# Built-in sorting algorithms?

Lets say I have the following structure

struct test_struct
{
int some_number;
char first_name[10]
}

test_struct s_table[100]

/*
In short a table within a table with a number, I would like to know if there
are any standard sorting algorithms implemented in c/c++ to sort s_table in
an alphabetical order based on first_name

I could code myself a small function using stricmp or strcasecmp but I
suspect a built-in algo would be
a lot more efficient and a few lines of code would be saved.

I do know about sort() function but since the structure contains more then
one element I somehow doubt it could be implemented?
Any tips would be appreciated

*/
Jul 19 '05 #1
5 8937
York wrote:
Lets say I have the following structure

struct test_struct
{
int some_number;
char first_name[10]
}

test_struct s_table[100]

/*
In short a table within a table with a number, I would like to know if there
are any standard sorting algorithms implemented in c/c++ to sort s_table in
an alphabetical order based on first_name

I could code myself a small function using stricmp or strcasecmp but I
suspect a built-in algo would be
a lot more efficient and a few lines of code would be saved.

I do know about sort() function but since the structure contains more then
one element I somehow doubt it could be implemented?
Any tips would be appreciated

*/

all you need to provide is a comparison function and you can use std::sort.

struct test_comparator
{
bool operator()( const test_struct & a, const test_struct & b)
{
return std::strcoll( a.first_name, b.first_name ) < 0;
}
};

then sorting is like

std::sort( s_table, s_table+100, test_comparator() )

I didn't try this code so you'll need to read between the typos.

Jul 19 '05 #2
In article <3f9a0b9c\$1_2@aeinews.>, sd**@dfssdfs.net says...
Lets say I have the following structure

struct test_struct
{
int some_number;
char first_name[10]
}

test_struct s_table[100]

To sort this, you need to supply a comparison function. In C++, you
could use code like this:

struct test_struct {
int number;
std::string first_name;

bool operator<(test_struct const &other) const {
return first_name < other.first_name;
}
};

For the moment, I've changed first_name to std::string instead of an
array of char -- if you really need to use the latter, your comparison
routine can use strcmp.

The basic idea is pretty similar in C, but it's generally more work, and
when you're done it'll normally run quite a bit slower, so IMO, it's not
worth a lot more discussion unless you have no choice in the matter.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #3
York wrote:
Lets say I have the following structure

struct test_struct
{
int some_number;
char first_name[10]
}

test_struct s_table[100]

In short a table within a table with a number, I would like to know if
there are any standard sorting algorithms implemented in c/c++ to sort
s_table in an alphabetical order based on first_name

In C, you would probably want to use the qsort function. C is off-topic
here, so I'll just tell you to look up "qsort" in your C textbook or on the
web.

In C++, you'd do something like this:

#include <algorithm>
#include <cstddef>
#include <cstring>
#include <iostream>
#include <iterator>
#include <ostream>

namespace
{
std::size_t const NAME_LENGTH(9);

struct test_struct
{
int some_number;
char first_name[NAME_LENGTH + 1];
};

bool order_by_first_name (test_struct const &ts1,
test_struct const &ts2)
{
return std::strcmp(ts1.first_name, ts2.first_name) < 0;
}

std::ostream &operator<< (std::ostream &os, test_struct const &ts)
{
return os << ts.some_number << ": " << ts.first_name;
}
}

int main()
{
std::size_t const ARRAY_SIZE(3);
test_struct s_table[ARRAY_SIZE] = { { 1, "Tom" },
{ 2, "Dick" },
{ 3, "Harry" } };

typedef std::ostream_iterator<test_struct> test_struct_writer;
test_struct_writer ts_writer(std::cout, "\n");

std::cout << "Before sorting:\n";
std::copy(s_table, s_table + ARRAY_SIZE, ts_writer);

std::sort(s_table, s_table + ARRAY_SIZE, order_by_first_name);

std::cout << "\nAfter sorting:\n";
std::copy(s_table, s_table + ARRAY_SIZE, ts_writer);
}

--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Jul 19 '05 #4

"Russell Hanneken" <rg********@pobox.com> wrote in message
York wrote:

In C, you would probably want to use the qsort function. C is off-topic
here, so I'll just tell you to look up "qsort" in your C textbook or on the web.

In C++, you'd do something like this:

Thanks, that is pretty much what I've been looking for.
Jul 19 '05 #5
"Russell Hanneken" <rg********@pobox.com> writes:
York wrote:
Lets say I have the following structure

struct test_struct
{
int some_number;
char first_name[10]
}

test_struct s_table[100]

In short a table within a table with a number, I would like to know if
there are any standard sorting algorithms implemented in c/c++ to sort
s_table in an alphabetical order based on first_name

In C, you would probably want to use the qsort function. C is off-topic
here, so I'll just tell you to look up "qsort" in your C textbook or on the
web.

However, since std::qsort() is also in C++, it would still be
topical to discuss it, though I'd agree that C++ provides better
candidates.

--
Micah J. Cowan
mi***@cowan.name
Jul 19 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.