Advertisement
Razali

Recursion : Minimum value index of Array

Nov 14th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. /*
  2.  * <summary> Determines the index of the smallet element </summary>
  3.  * <params>
  4.  *      "arr" = Array containing the integer elements
  5.  *      "size" = Size of "arr"
  6.  *      "currentIndex" = Index position of the current element
  7.  * </params>
  8.  * <return> Index of the first occuring minimum value </return>
  9.  * <precond>
  10.  *  "size" > 0
  11.  *  Function should have an intial call with "0" as the currentIndex
  12.  * </precond>
  13.  */
  14.  
  15.  int get_min_index(int arr[], int size, int currentIndex)
  16.  {
  17.     int minIndex;
  18.    
  19.     /* Base Case - Last Element */
  20.     if(currentIndex == size - 1)
  21.         return currentIndex;
  22.    
  23.     minIndex = get_min_index(arr, size, currentIndex + 1);
  24.    
  25.     return (arr[currentIndex] <= arr[minIndex]) ? currentIndex : minIndex;
  26.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement