Run Code
|
API
|
Code Wall
|
Users
|
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
Please
log in
to post a comment.
cv5_class
First
CS1428 SI Tuesday
enum operator
Crow unordered_map Quiz
NaN inside set
6 14 15 17 21 33 29
Recursive Enumeration Example
marquee text in C++
insertion sort
Please log in to post a comment.