473,503 Members | 3,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9137
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
news:ge****************@newsread4.news.pas.earthli nk.net...
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2255
by: JimmyT | last post by:
I just configured and installed 2.3.4 and noticed there is no datetime module. I noticed there is a datetimemodule.c file that did not get built (ie no object file). Is there something I need to...
1
3092
by: Alex Elbert | last post by:
Hi I have built dynamic HTMLTable. Now I want to attach it directly to the Email Body - it is already built, so why not to use a ready table. However, I cannot find the way of getting plain HTML...
0
2166
by: Andrew Crook | last post by:
does MYSQL have a quota built into it! I need it limit the size of each database AndiC
1
1869
by: Mark | last post by:
Is there a way to execute a statement that is built dynamically by a .NET application. For example I have a loop that is reading values from a database and I want to do something like the...
4
6546
by: Yasutaka Ito | last post by:
Hi, Is there a way to determine which version of .NET Framework any given assembly is built with? thanks! -Yasutaka
1
2081
by: William | last post by:
Looking for a pre built dotnet corporate or small business website template.
1
2159
by: William | last post by:
Looking for a pre built dot net website for consulting business. I am trying to put up a quick business web for a dot net frame work. I have a provider already. I am trying to save time. Any...
1
1527
by: Daniel | last post by:
is there any way to get to a unique build verion of an assembly at runtime? e.g. a version that is unique to the time that the assembly was built?
48
4881
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott...
3
1768
by: drewj840 | last post by:
I built a Windows service that sweeps a set of folders every 60 seconds and puts the files into a SQL Server database. I am creating a second service that will delete this set of folders and recreate...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.