You are on page 1of 2

Name:

________________________________

PA8 Advanced Option – Sort Speed


Goals
• Practice with creating and manipulating arrays
• Use symbolic constants and global variables
• Repurpose code from previous assignments and examples
• Create modular programs using functions

Tasks
1. Write a program that does the following:
a. Create a global array to hold 10,000 random numbers between 1 and 1000
b. The array size MUST be represented in all places by a symbolic constant
c. Create functions to sort the array using different algorithms. Pick at least 2:
o Bubble sort
o Insertion sort
o Selection sort
d. Using the clock() function provided in time.h, calculate how many milliseconds it takes to
perform each sort on average.
o The clock() function returns how many clock cycles have executed at a given point
o You’ll need to use the constant CLOCKS_PER_SEC to convert elapsed clock cycles to
milliseconds
e. Call each different sort at least 10 times with different sets of random data to get the
average time for that method
f. The average time should be a floating point number showing 1 decimal place
2. Use the references listed on the next page for help on the clock() function and the different sorting
algorithms
3. You should also include a printArray() function so that you can test your sorting algorithms to
make sure they are working (I suggest using a smaller array size while testing the algorithms)
4. The final screen output should look similar to the examples on the next page.
5. When you are satisfied that the program builds and runs successfully, upload your C file via the
PA9 assignment on Canvas.

EG1302 - Programming for Engineers Page 1


Name: ________________________________
References

o https://www.tutorialspoint.com/c_standard_library/time_h.htm
o https://www.tutorialspoint.com/data_structures_algorithms/bubble_sort_algorithm.htm
o https://www.tutorialspoint.com/data_structures_algorithms/insertion_sort_algorithm.htm
o https://www.tutorialspoint.com/data_structures_algorithms/selection_sort_algorithm.htm
o https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html

Example Output

Let’s see how fast these algorithms can sort 10,000 integers:

Bubble sort… 1804.2 ms


Insertion sort… 1750.9 ms
Selection sort… 1800.4 ms

EG1302 - Programming for Engineers Page 2

You might also like