a recursuve function demonstation Solution //The GCD and Extended GCD. //This pr
ID: 3557897 • Letter: A
Question
a recursuve function demonstation
Explanation / Answer
//The GCD and Extended GCD. //This program demonstrates inefficient recursive calls that can be corrected. //It also demonstrates the difference between a method whose running time is O(2^n) vs. //an implementation whose running time is O(n). //written by Moshe Rosenfeld, Jan. 2002 import java.io.*; public class Recursive { public static void main(String[] args) throws IOException { System.out.println("This program will demosntrate the execution time of two algorithms to find the largest entry among the first k entries of an array"); int [] a = new int[20000]; for (int k = 0; k < 20000; k++) a[k] =(int) (100000*Math.random()); int flag = 0; do { System.out.print(" Enter an inetegr -->"); int first = Tools.GetInt(); System.out.println(" "); System.out.print("The "+first+"-th largest entry is: "+k_th(a,first)); System.out.print(" Enter 0 to quit, 1 to continue -->"); flag = Tools.GetInt(); } while (flag > 0); do { System.out.print(" We shall now execute the alternative method on the asme array: Enter an inetegr -->"); int first = Tools.GetInt(); System.out.println(" "); System.out.print("The "+first+"-th largest entry is: "+better_k_th(a,first)); System.out.print(" Enter 0 to quit, 1 to continue -->"); flag = Tools.GetInt(); } while (flag > 0); }//End main method public static int k_th(int n[], int m) { if (m==1) return n[0]; else return (n[m-1] > k_th(n,m-1) ? n[m-1]:k_th(n,m-1)); }//k_th public static int better_k_th(int n[], int m) { if (m==1) return n[0]; else { int temp = better_k_th(n, m-1); return (n[m-1] > temp ? n[m-1] : temp); } }//better_k_th }//End classRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.