From the course: Advanced C Programming: Optimize Performance and Efficiency
Unlock the full course today
Join today to access over 24,500 courses taught by industry experts.
Solution: Sort an array of structures - C Tutorial
From the course: Advanced C Programming: Optimize Performance and Efficiency
Solution: Sort an array of structures
(upbeat music) - [Instructor] For my solution, I combined exercises from two earlier movies. One was on presetting structure array elements. The other was on sorting an array. So the sort routine goes in this code from lines 29 through 40. It contain two nested loops. One using the variable outer, the other using variable inner. Line 33 compares the temp member of two structure array elements. If the temperature is greater, it needs to be swapped to a later position in the array. That swap takes place at lines 35, 36, and 37. The t variable temporarily holds structures. It's declared at line 18. You see that it's the same structure type as the array. The key to remember when sorting an array of structures is that you don't need to swap each structure members. Simply swap the structure array elements, which is shown here at lines 35, 36, and 37. Build and run. And it works.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Sort an array6m 39s
-
(Locked)
Challenge: Sort a string24s
-
(Locked)
Solution: Sort a string1m 28s
-
(Locked)
Work with arrays and functions3m 21s
-
(Locked)
Challenge: An array-modification function24s
-
(Locked)
Solution: An array-modification function1m 16s
-
(Locked)
Send a structure to a function4m
-
(Locked)
Challenge: Create a structure function27s
-
(Locked)
Solution: Create a structure function1m 20s
-
(Locked)
Build an array of structures3m 36s
-
(Locked)
Challenge: Sort an array of structures32s
-
(Locked)
Solution: Sort an array of structures1m 7s
-
(Locked)
-
-