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.

Sort an array

Sort an array

- [Instructor] The computer can plow through a tediously long array of numbers and place them in order. It does so without complaint and it creates the sorted list as fast as your program allows. To meet your sorting needs in the C language, you have several options. The most basic sort is the bubble sort. It's not very efficient, but it's easy to understand. Open exercise file 04-01_arraysort1. This is a rather long piece of source code but it's divided up into chunks. Starting at line 12, the array is initialized. The number of elements is set by the size constant which is defined at line 5. At line 15, the array is filled with random values. The expression at line 15 ensures that the random values lie in the range between 1 and 100. Lines 18 through 21 display each number in the array. You see the %3d placeholder used in the printf function at line 20. That allows a character width of three digits for each number,…

Contents