As usual, create a directory to hold today's activities:
$ mkdir ~/cs170/labs/lab19 $ cd ~/cs170/labs/lab19
Similar to pointers, many people find the concept of recursion a little bit intimidating. However,
isn't really different from what we've seen so far in C++. It's just function calls. The difference, however, is that the function is going to call itself. We just need to make sure that the function knows when to stop.ANYTHING written using a loop, can be written using recursion! So today we will practice writing some of the loops we've seen before using recursion instead of using loops. One common loop we have seen (specifically in selection sort, for example) is a loop to find the smallest element in an array.
In a file called recursion.cc, create a function
called int findSmallest(int aList[], int length)
. This
function should find and return the smallest element from the
array. You should find the smallest element using recursion. You
may need to define a helper function to help with this.
Make sure you test your function on several lists of various sizes, just to make sure that it works correctly.
int * my_list = new int[5]; for(int i = 0; i < 5; i++) { my_list[i] = 5 - i; } cout << findSmallest(my_list, 5) << endl; //Prints 1 delete[] my_sorted_list;
Define a function int findSmallestHelper(int aList[],
int length, int currentIndex, int currentSmallest)
This function will perform the actual recursion. Your other
function just allows for calling this function a little bit easier.
Your base case is when currentIndex == length
.
Note! Every iteration of the recursive function MUST return something! It should return the smallest element from the array no matter if it was in the base case or recursive case!
Bubble sort required 2 loops. One loop iterated through each index, comparing with neighbors and swapping until it reached the end of the array. The second loop made sure that the first loop executed N times. So, for bubble sort to be written recursively, you need two recursive functions.
In your recursion.cc file, create a function
called void bubble_sort(int * a_list, int size)
. This
function takes an array of integers,
and returns a sorted copy of a_list
using the bubble sort algorithm. Instead of using loops this time,
you should use recursion instead.
int * my_list = new int[5]; for(int i = 0; i < 5; i++) { my_list[i] = 5 - i; } int * my_sorted_list = bubble_sort(my_list, 5); printList(my_list); #Prints [5, 4, 3, 2, 1] printList(my_sorted_list); #Prints [1, 2, 3, 4, 5] delete[] my_sorted_list; int my_list2[5] = {4, 2, 1, 3, 5}; my_sorted_list = bubble_sort(my_list2, 5); printList(my_list2); #Prints [4, 2, 1, 3, 5] printList(my_sorted_list); #Prints [1, 2, 3, 4, 5]
The "easiest" way to tackle this problem is to work from the
inside loop out. First, write a function void
bubbleAcross(int aList[], int length, int
currentIndex)
. This functions sole purpose is to
iterate from currentIndex to the end of the list, comparing
neighbors and swapping them. What will your base case be here?
Test this function first! If you call this function once on an unsorted array, the largest element should end up at the end!
Once the previous function works correctly, then start
writing your second recursive function:
bubbleWholeArray(int aList[], int length)
.
This function should make sure that it calls the
"bubbleAcross" function a total of length number of times.
Remember, once you finish one call to bubbleAcross,
the largest element in the array is located in its correct
spot. You don't need to compare to that element any more.
So you can make sure that bubbleWholeArray calls
bubbleAcross the correct number of times by subtracting one
from length for every recursive call to bubbleWholeArray.
What does that mean your base case should be for this function?
You've now written one sorting algorithm, and a function to find
the smallest element in an array. Let's finish a 2nd sorting
algorithm recursively! Write a function
void selectionSort(int aList[], int length)
in the
same file as your other functions, which sorts the array in place
using recursion.