Find the minimum number in an array with recursion?

匿名 (未验证) 提交于 2019-12-03 01:54:01

问题:

int i = 0; int min = x[i]; while ( i 

I've written the iterative form to find the min number of an array. But I'd like to write a function that with recursion. Please help!

回答1:

Because this sounds like homework, here's a hint: The minimum value in an array is either the first element, or the minimum number in the rest of the array, whichever is smaller.



回答2:

The minimum number of a single-element array is the one element in the array.

The minimum number of an array with size > 1 is the minimum of the first element and the minimum of the rest of the array.

(The minimum number of an empty array is not defined.)



回答3:

This is not an optimal solution, because you may save the result of the recursive call in an int variable, but I wasn't allowed to in my exercise.

Anyway, this function searches the lowest value in an int array by using recursion. As first it checks how many elements are in the array by checking if the size equals to 1 and then returns the first value as result. If the table is bigger than 1 (and technically also when lower) it compares the last value in the array with the recursive call with the rest of the array.

The recursion stops at the 0-index and then compares that value with the index 1. Then it compares the lowest value of index 0 and 1 with the value from index 3 and so on until it reaches last value.

int minimum(int array[], int size) {     if (size == 1) {         return array[0];     }     else {         return (array[size] 

Rules in my exercise:

  • Do not change the array
  • Do not use any variable
  • Use recursion


回答4:

Sounds like homework, but your best bet is something like this:

int main(void) {     int size = 2;     int test[] = {0,1};     int min = test[0];     findMin(&min, test, size);     printf("%d", min); }  void findMin(int* min, int* test, int* length);  void findMin(int* min, int* test, int* length) {     if (length >= 0) {          if (*test 


回答5:

Here is a function that will return a pointer to the minimum value:

#include   int *recursiveMin(int *x, int n) {   if (x == NULL || n == 0)       return NULL;   if (n == 1)       return x;   int *t = recursiveMin(x + 1, n - 1);   return *x 


回答6:

general rule of recursion is to avoid "small steps" - so "first element compared to rest of the array" is very bad idea. try to process the array in halves, like this:

min( array ) {    if size of array == 1 return array[0]    else if size of array == 2 return array[0]   

for further optimization:
- avoid array copies - consider using quicksort-like signature (array, from, to)
- avoid recursive calls for sizes



回答7:

Why do you want to do this with recursion? A general rule with recursion is don't use recursion if you can do it inside a simple linear loop.



回答8:

Try:

  int recursive_min(list arr) {     return recursive_min(arr);   }

Although this doesn't work in imperative languages.

A more serious answer would be, in pseudocode:

func recursive_min( list l ) {  head = l[1]  tail = l[2end]  if l.length==1 return head else return min(head,recursive_min(tail)) }

Although that doesn't work if l is empty.



回答9:

int findMin(int a[], int len) { //  normal version //  if (len==1) return a[0]; //  return (a[len-1] 


回答10:

#include  #include  using namespace std;  int min(int [], int); void mostrar(int [], int);  int main() {     int f[] = {4, 9, 0, -1, 5, 8, -3, 6, -4, 0};     int t = sizeof(f)/sizeof(int);     cout nul"); }  int min(int x[], int p) {     if (p == 1)         return x[0];     int aux = min(x+1, p-1);     return x[0]  1)         mostrar(v, k-1);     cout 


回答11:

const int = 20;  int getmin (int v[], int n);  main () {     int v[N];     int i,n,min;      printf("\n\tType number of elements:\t");     scanf("%d",&n);      for (i=0;i0)     {         if ( v[n-2] > v[n-1] )         {             v[n-2]=v[n-1];                        }         getmin (v,n-1);     }      return v[n-2];        }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!