Previous: 12.2 Arrays of Structures
Up: 12 Structures and Unions
Next: 12.4 Unions
Previous Page: 12.2 Arrays of Structures
Next Page: 12.4 Unions

12.3 Sorting Arrays of Structures

We can make one more small improvement to our address label program. Often when we want to print labels, we would like to print them in some sorted order. In this section we will write a function to sort the array of label structures. As we saw in Chapter , an array is sorted by some key, that is, for an array of structures, by a specific member of the structure. A list of labels may be sorted either by last name, or by zip code, or by street address, and so forth. Again, considering the sorting algorithms in Chapter , we saw that sorting involves swapping data items to place them in the correct order. However, like passing structures to functions, swapping entire structures can be inefficient if the structures are large. In addition, it is common that an array of structures needs to be sorted by different keys for different purposes. To solve these problems, we can use a technique we used in Chapter for sorting two dimensional arrays - sorting the data using an array of pointers. In this way, the swapping operations while sorting involve only pointers, not entire records, and we can maintain several such pointer arrays, each sorted by a different key.

Figure 12.14 shows the code for the function sortlabels() added to the file lblutil.c which sorts labels by last name using pointers. (The function assumes the label structure defined in lbl.h --- Section 12.1.4). The function sortlabels() is passed the array of labels, person[] and an array of pointers to label structures, plabel[]. This array should be declared in main() as:

struct label *plabel[MAX];
and passed to sortlabels() in the call:
sortlabels(person,plabel,n);
after the person[] array is read. The function begins by initializing the elements of plabel[] to point to successive elements of the array of structures, person[]. It then calls sortptrs() to sort the array by last name using these pointers using a selection sort algorithm. The only thing to note is that for the comparison step of the sort, a structure element is accessed by:
plabel[j]->name.last
which accesses the last field of the name field of the object pointed to by plabel[j]. In the swap step of the sort algorithm, only the pointers are swapped.

We can now write a function, printsortedlabels(), to print the labels in sorted order using the plabel[] array, modifying main() appropriately. We leave this as an exercise.

The utility functions in the file lblutil.c provide most of the tools needed to write a useful, interactive address label data base program. In the next chapter, we discuss the remaining piece - file storage for the data base, and write the entire application.



Previous: 12.2 Arrays of Structures
Up: 12 Structures and Unions
Next: 12.4 Unions
Previous Page: 12.2 Arrays of Structures
Next Page: 12.4 Unions

tep@wiliki.eng.hawaii.edu
Sat Sep 3 07:12:43 HST 1994