Sorting Array of Structures

A

Allie

How would I go about sorting this structure by title

typedef struct
char author[40]
char title[40]
char code[4]
int hold
int loan
} LIBRARY

LIBRARY book[N]

This is what I've written

void sortbytitle( LIBRARY *b, int n ) { /* n = number of books i
stoc
*
LIBRARY temp[N]
int i

for( i = 0; i < n; i++ )
if( strcmp( b.title, b[i + 1].title ) > 0 )
temp = b
b = b[i + 1]
b[i + 1] = temp



return


I get no errors or warnings when I compile using Bloodshed Dev-C++
However, my output then looks like this
AUTHOR TITLE CODE #COPY #BORR #AVAI
Golumbic Graph Theory G01 5 2
Jacobs Database Logic J01 3 1
Kanter Management Information K01 8 1
Kuo Numerical Methods K02 2 0
Habermann Operating Systems H01 10 5
Schroder C S01 7 2
Herzog Computing Structures H03 10 7
Holub Compiler Design H05 11 8
Galvin Operating Systems G02 15 2 1
Lane Data Communications L01 20 3 1
Kleinrock Queueing Systems K03 14 0 1
Kindred Data Systems K04 2 0
Horowitz Programming Languages H06 16 10

(Original output, before sort is called
AUTHOR TITLE CODE #COPY #BORR #AVAI
Habermann Operating Systems H01 10 5
Golumbic Graph Theory G01 5 2
Jacobs Database Logic J01 3 1
Kanter Management Information K01 8 1
Kuo Numerical Methods K02 2 0
Hughs Structured Programming H02 4 4
Schroder C S01 7 2
Herzog Computing Structures H03 10 7
Hunter Understanding C H04 6 6
Holub Compiler Design H05 11 8
Galvin Operating Systems G02 15 2 1
Lane Data Communications L01 20 3 1
Kleinrock Queueing Systems K03 14 0 1
Kindred Data Systems K04 2 0
Mano Computer Architecture M01 2 2
Horowitz Programming Languages H06 16 10

The sortbytitle() function is used in conjunction wit
printbyavail()... which explains the three missing books in the outpu
produced by sortybytitle().

The titles aren't sorted properly! What am I doing wrong

Thank you muchly :

--Alli
 
C

Chris Smith

Allie said:
How would I go about sorting this structure by title?

You need to look up sort algorithms. You can't just write something
that sorta looks like it may work, and expect that it will. There is a
fair amount of intelligent research that's gone into good ways to sort.

Sort algorithms are not specific to the C programming language, and
therefore would be considered off-topic here by general agreement as I
understand it. Another good newsgroup might be comp.programming. You
could also obtain any of the fundamental algs books by Robert
Sedgewick... including one with the example code in C, if that's your
desire.
 
A

A. Sinan Unur

(e-mail address removed)-spam.invalid (Allie) wrote in

How would I go about sorting this structure by title?

typedef struct {
char author[40];
char title[40];
char code[4];
int hold;
int loan;
} LIBRARY;

LIBRARY book[N];


This is what I've written:

void sortbytitle( LIBRARY *b, int n ) { /* n = number of books in
stock
*/

The standard library includes a general purpose sort function called
qsort. Look it up in your favorite C book.

You are unlikely to improve on that routine at this point.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top