For Programmers: Free Programming Magazines  


Home > Archive > C > February 2006 > Sorting Array of Structures









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Sorting Array of Structures
Allie

2006-02-26, 9:55 pm

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
*/
LIBRARY temp[N];
int i;

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

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 #AVAIL

Golumbic Graph Theory G01 5 2 3
Jacobs Database Logic J01 3 1 2
Kanter Management Information K01 8 1 7
Kuo Numerical Methods K02 2 0 2
Habermann Operating Systems H01 10 5 5
Schroder C S01 7 2 5
Herzog Computing Structures H03 10 7 3
Holub Compiler Design H05 11 8 3
Galvin Operating Systems G02 15 2 13
Lane Data Communications L01 20 3 17
Kleinrock Queueing Systems K03 14 0 14
Kindred Data Systems K04 2 0 2
Horowitz Programming Languages H06 16 10 6

(Original output, before sort is called:
AUTHOR TITLE CODE #COPY #BORR #AVAIL

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

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

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

Thank you muchly :)

--Allie

Chris Smith

2006-02-27, 3:56 am

Allie <fakeprogress@gmail-dot-com.no-spam.invalid> wrote:
> 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.

--
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
A. Sinan Unur

2006-02-27, 3:56 am

fakeprogress@gmail-dot-com.no-spam.invalid (Allie) wrote in
news:440276c4$0$49035$892e7fe2@authen.yellow.readfreenews.net:

> 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
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com