In this lab you will work with one partner, and implement Quicksort and Heapsort. For testing, generate random floating point or integer numbers, and test your code on inputs of size n=1000, 1M, 10M, 100M, 1B. Record your findings in a brief report.
Your team should have two files: heapsort.c and quicksort.c. To make grading easier, please prefix the files with the last names in your team.
For example, if James Little and Jack Ward were working together, they would submit two files: LittleWard_quicksort.c and LittleWard_heapsort.c
Note: Please use last names, since we got three James, two Andrews, and two Grace's !
Each file should contain the sort function, a main function that calls the sort function in the appropriate way, and a function that tests that the sorting was correct, like so:
/* file: LittleWard_quicksort.c Authors: James Little and Jack Ward Description:insert high level description here */ /* this is the size of the array; if the user wants a different size, he or she needs to change this constant and recompile note: ideally the size of the array should be passed on the command line, to avoid recompilation every time */ const int N = 100; /* quicksort in place array a of length n; assume array is allocated and populated with data prior to this call */ void quicksort(int* a, int n) { ... } /* return 1 if a is sorted (in non-decreasing order); and 0 otherwise assume array is allocated and populated with data prior to this call */ int issorted(int* a, int n) { ... } int main() { //declare and allocate the array of size N, where N is a global //populate the array with random data //call quicksort //test that array is sorted #IFNDEF NDEBUG if (issorted(a,n)) { printf("Array is sorted!\n"); } else { printf("OOPS! Array is NOT sorted!\n"); } #endif
[ltoma@lobster ~]$ time LittleWard_quickortThis will return the total time for the command (real time), and CPU time (user + system), something like this:
real 0m0.003s user 0m0.001s sys 0m0.002sYou can try man time for more details.
First we will edit the file and set the constant Then we'll compile with -DNDEBUG to turn off the call to issorted().
gcc LittleWard_quicksort.c -o LW_quicksort
Then we'll run it:
./LW_quicksort
This will sort and call the issorted function to verify that
the array is sorted.
gcc -DNDEBUG LittleWard_quicksort.c -o LW_quicksort
Then we'll run again.
time LW_quicksort
This time, because -DNDEBUG is on, the
issorted function is not called, so the timing reported at
the end represents the time toinitialize the array, and the time to
sort it.
Segfaults are usually caused by accessing a bad address. Look for out-of-bounds array indices.
Something to look into is static versus dynamic allocation in C. It may be that you won't be able to statically allocate 1 billion floats. Perhaps. Something to look into and be aware.
As you work with big data, don't take for granted that your requests for memory are succesfull.
At some point the code will run into issues because it won't be able (maybe) to allocate as much memory as it wants. You want to handle this issue gracefully in your code (that is, check that when you request memory to be allocated, check that the request was actually succesfull).
There is lots of information on the web, use it!
Enjoy!