Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Recursive Sort Example
// Example from Lecture 6 in class on recursive functions #include <iostream> #include <stdlib.h> // rand srand using namespace std; void recursiveSort(int *array, int n) { for (int i=0; i<10-n; i++) cout << " "; cout << "Entering into recursiveSort: n = " << n << ", Address of array = " << array << endl; if (n==1) { return; } // find smallest thing in the array, and put it first int smallIndex = 0; int smallValue = array[0]; for (int i=1; i<n; i++) { if (array[i] < smallValue) { smallValue = array[i]; smallIndex = i; } } // swap smallest value with first value array[smallIndex] = array[0]; array[0] = smallValue; // make recursive call, note the change of the arguement values array++; n -= 1; recursiveSort(array,n); for (int i=0; i<10-n; i++) cout << " "; cout << "Returning from recursiveSort: n = " << n << ", Address of array = " << array << endl; return; } int main() { int n, *array; cout << "Enter n: "; cin >> n; // create array of that size array = new int[n]; cout << endl << "n = " << n << endl << endl; srand(1111); cout << "Initial array = "; for (int i=0; i<n; i++) { array[i] = rand() % 100; cout << array[i] << " "; } cout << endl; recursiveSort(array, n); cout << "Sorted array = "; for (int i=0; i<n; i++) { cout << array[i] << " "; } cout << endl; return 0; }
run
|
edit
|
history
|
help
0
for_each_argument
test
Optional conversions
std::99 bottles of beer!
Argument passing by using reference and value
back_inserter example
simple in-memory b-tree
hello world
Recursive Function Calling Example with Stack Addresses
FUCK