I have checked the code you shared regarding this problem.
I spent quite a long time to make this easy for you. I came up with a project where everything is ready for you all you need to do is implement the sorting functions in srt.c file:
- bubble sort
- heap sort
- insertion sort
- merge sort
Actually, I've already implemented the bubble sort, just to demonstrate the project works.
The project you shared was a complete mess (variable names missing, mismatched opening/closing braces, etc). I needed to understand (and guess) what needs to be done here, what is given and what you tried to do.
I deduced that srt.h was given by your teacher. So I built upon this and removed all the confusing part.
I figured this is the file that was given to you by your teacher, so I left this file unchanged.
Please note, in C you can crate a function prototype (declare a function) without specifying the name of your arguments. However, when you implement your functions (define the functions) you cannot omit the name, otherwise you won't be able to reference them. (Actually you can omit the name if you never want to use those parameters, but why do you have them in the first place them?)
So this is the file where you need to implement the various sorting algorithms.
First of all, this file needs to include srt.h.
I've done the implementation for the bubble sort (that's the easiest one XD) just to demonstrate how everything would work. Note: I did just the naive implementation and not the most efficient algorithm. Feel free to make it better. ;)
All these functions modify the input buffer and reorder the elements there.
All the functions have the following arguments:
|void * buffer||the buffer where the array to be sorted is. Void type, so any sorting would work with any arbitrary type.|
|size_t elementSize||The size of a single element of the array to be sorted|
|size_t elementCount||The number of elements in the array to be sorted|
|int (*compare)(const void *, const void *)||The function (pointer) that compares two elements in the array. If the first element is greater, it returns >0; if the first element is smaller, it returns <0; if they are equal 0 is returned.|
You can see how to access (the address of) an arbitrary member of the array: buffer + elementSize * j.
I created a main.c where there are 4 main functions:
These will test all the sorting implementations you need to do. Of course this is not an exhaustive test, feel free to apply more testing on your own.
However, I'd advise first to implement the sorting functions then do more testing.
Also, the main.c has some helper functions. You are free to explore them and see what they do, but you should first really focus on srt.c and implement the various sorting mechanisms.
The Test Data
I chose to test with float (4 bytes) and byte (1 byte) data types just to demonstrate that the generic sorting functions support any arbitrary element sorting, it is not tied to the size of the elements in the array.
Also, all the four test functions above start by duplicating the test data. This is for two reasons:
- this allows the same test data to be passed to all the testing functions
- duplication makes sure the original data would not change
I didn't go all the way to allow automatic test result validation i.e. notify the user if the sorting algorithms provided the wrong order.
For this after every test, you have to do your validation on your own. Look at the output, and check if the sorting algorithms indeed provided the right order.
I think this project is very clear now. Testing is ready for you. Sort function stubs are ready, all you need to do is implement them.
So, here's the project, fork from it and do your own implementation. You can share the link to your changes (as you did before) when done.