class Kjoretid { static int work; static void theta_1(int n){ work++; } static void theta_log_n(int n){ work++; if(n > 0) theta_log_n( n / 2 ); } static void theta_n(int n) { for( int i = 0; i < n; i++) work++; } static void theta_n_log_n(int n){ if( n > 0 ){ theta_n_log_n( n / 2); theta_n_log_n( n / 2); for( int i = 0; i < n; i++) work++; } } static void theta_n_i_andre(int n){ for( int i = 0; i < n; i++) for( int j = 0; j < n; j++) work++; } static void theta_2_i_nte(int n){ work++; if(n > 0){ theta_2_i_nte( n - 1 ); theta_2_i_nte( n - 1 ); } } static void theta_n_fakultet( int n ){ work++; if( n > 0 ) for( int i = 0; i < n; i++) theta_n_fakultet( n - 1 ); } /*hva gjør denne metoden? Hva er tidskompleksiteten?*/ static String metodeA( int n, int a){ StringBuffer sb = new StringBuffer(); do{ sb.insert(0, n % a ); n /= a; work++; }while( n > 0 ); return sb.toString(); } /*Hva er tidskompleksiteten til denne metoden?*/ static void metodeB( int n ){ for( int i = 0; i < n; i++) work++; if(n > 0) metodeB( n / 2); } public static void main(String[] args) throws Exception{ int num = args.length > 0 ? Integer.parseInt( args[0] ) : 10; work = 0; theta_1(num); System.out.println("theta(1): " + work ); work = 0; theta_log_n(num); System.out.println("theta(log n): " + work ); work = 0; theta_n(num); System.out.println("theta(n): " + work ); work = 0; theta_n_log_n(num); System.out.println("theta(n log n): " + work ); work = 0; theta_n_i_andre(num); System.out.println("theta(n^2): " + work ); work = 0; theta_2_i_nte(num); System.out.println("theta(2^n): " + work ); work = 0; theta_n_fakultet(num); System.out.println("theta(n!): " + work ); } }