Sagging shelves are unattractive and can be dangerous, especially if they are weighed down with heavy items that can come crashing down if the shelf breaks. Providing simple reinforcement can often solve the problem, although if your shelf is made of cheap material such as medium-density fiberboard (MDF), a reinforcement may only help for a short period of time.
Empty the shelf of all contents, and use a tape measure to locate the centre of the sagging shelf. Mark that spot on the wall (or the back of the freestanding bookcase) underneath the shelf.
Place a metal shelving bracket at this centre point, and line it up with the other brackets on both ends of the shelf. Mark where the screws will need to go, and then remove the bracket and the shelf.
Screw drywall anchors into the spots you marked, making sure they are flush with the wall when you are done.
Put the bracket in place, and then put the screws into the drywall anchors. When they are completely screwed in, you are ready to replace the shelf.
For freestanding bookshelves, you can also make a vertical divider from 3/4-inch plywood, cut to fit between the shelves, propping up the one that is sagging.