Development tools
For this lab you will create a simple selection sort program in C. This assignment is practice of effective development practices:
- Develop in a branch
- Checkin often
- Keep commits as small as possible
- Merge back to main branch when done
You should checkin your progress after each step. You can use git commit -a
to commit all changes since your last commit. This can save time if the changes are small.
-
Pull your course repository.
-
Create a branch to contain the feature we will develop. Call the branch
simplesort
. The commandgit branch XYZ
will be helpful. Read the man page if you need. Checkout the branch after you create it. -
Open Visual Studio (or your IDE of choice). Create a project in the simplesort directory. Your source files must go in the root of the directory. If you use Visual Studio:
- Make the project an empty project
-
Create a file called
sort.c
. Go to Project > Properties > Linker > System and set SubSystem to Console. -
Add this to the top of your sort.c file:
int size = 10; int array[] = {-7, -32, 20, -2, 14, 5, -1, 13, -22, 23};
-
Create a
main
function in the standard C style. Remember that any functions thatmain
calls need to be defined abovemain
. -
Create an empty function called
void testMax()
. Call this function from main. This function should test a function you will create in the next step:getMaxId
.getMaxId
takes an array and a size and returns the index of the maximum element. -
Add an include for the
assert.h
header. Complete thetestMax()
function, using assert and a loop to verify you found the maximum element id. You may stub ingetMaxId()
if you wish. Assert tests look similar to this:assert(array[max] >= array[i]);
-
Add a function
getMaxId()
that takes an int array, its size, and returns the index of the maximum element. Verify your function is correct using your test. -
Add a function
testMaxAtEnd()
. This function will testmaxToEnd()
which will swap the maximum element to the last position in the array. You should again use asserts to verify correct operation. -
Complete the function
maxToEnd()
. This function takes an array, its size, and finds the maximum element. It then swaps this element to the last position in the array. Verify your function is correct using your test. -
Add a function
testSort()
. This function will testsort()
. Again, use loops and asserts to verify the array is sorted. -
Complete the function
sort()
, which takes an array and an upper bound. The function should swap the maximum element to the end of the array, then repeat on the same data but with bounds size smaller by one. This is basically selection sort. Verify your selection sort is correct using your test. -
After your final commit, merge your changes back to the main branch:
- Switch to the master branch
- Merge the simplesort branch to master
- Complete the assignment:
- Open repo and show all branches visually (use
gitk --all
) - Open your
sort.c
file. - Ask your instructor to verify the assignment.
- Open repo and show all branches visually (use