From 89d316a703abf3b57056044e6242f3694ded2423 Mon Sep 17 00:00:00 2001 From: vigusmao Date: Fri, 16 Jul 2021 08:58:34 -0300 Subject: [PATCH] Comparando complexidades assintoticas --- Armarios/src/Principal.java | 105 +++++++++++++++++++++++++++++++++--- 1 file changed, 97 insertions(+), 8 deletions(-) diff --git a/Armarios/src/Principal.java b/Armarios/src/Principal.java index 7a95042..662268a 100644 --- a/Armarios/src/Principal.java +++ b/Armarios/src/Principal.java @@ -3,7 +3,8 @@ public class Principal { - public static ArrayList obterArmariosAbertosViaSimulacao(int n) { + // O(n^2) + public static int obterArmariosAbertosN2(int n) { /* false: armário fechado; true: armário aberto */ boolean[] armarios ; // é null neste momento @@ -27,28 +28,116 @@ public static ArrayList obterArmariosAbertosViaSimulacao(int n) { } } - return armariosAbertos; + return armariosAbertos.size(); } - public static int obterArmariosAbertos(int n) { + // O(n log n) + public static int obterArmariosAbertosNLogN(int n) { + /* false: armário fechado; + true: armário aberto */ + boolean[] armarios ; // é null neste momento + + armarios = new boolean[n + 1]; // descartaremos a posição 0; + // O array contém apenas false neste momento (armários todos fechados) + + for (int crianca = 1; crianca <= n; crianca++) { + for (int armario = crianca; armario <= n; armario += crianca) { + armarios[armario] = !armarios[armario]; + } + } + + ArrayList armariosAbertos = new ArrayList<>(); + + for (int armario = 1; armario <= n; armario++) { + if (armarios[armario]) { + armariosAbertos.add(armario); + } + } + + return armariosAbertos.size(); + } + + // O(n) + public static int obterArmariosAbertosN(int n) { + + ArrayList armariosAbertos = new ArrayList<>(); + + for (int armario = 1; armario <= n; armario++) { + int raiz = (int) Math.sqrt(armario); + if (raiz * raiz == armario) { + armariosAbertos.add(armario); + } + } + + return armariosAbertos.size(); + } + + // O(n^0.5) + public static int obterArmariosAbertosSqrtN(int n) { + + ArrayList armariosAbertos = new ArrayList<>(); + + int base = 1; + int armario = 1; + while (armario <= n) { + armariosAbertos.add(armario); // armario é quadrado perfeito! + base++; + armario = base * base; + } + + return armariosAbertos.size(); + } + + // O(1) -- tempo constante + public static int obterArmariosAbertosConstante(int n) { return (int) Math.sqrt(n); } public static void main(String[] args) { while (true) { - System.out.print("Quantos armários? "); + System.out.print("\n\nQuantos armários? "); Scanner scanner = new Scanner(System.in); int contArmarios = scanner.nextInt(); if (contArmarios < 1) { return; } - long inicio = System.currentTimeMillis(); - ArrayList armariosAbertos = obterArmariosAbertosViaSimulacao(contArmarios); + long inicio, duracao; + int abertos; + + inicio = System.currentTimeMillis(); + abertos = obterArmariosAbertosN2(contArmarios); + duracao = System.currentTimeMillis() - inicio; + System.out.println("\nO(n^2)"); + System.out.println("Número de armários abertos: " + abertos); + System.out.printf("Duração: %.3f segundos\n", duracao / 1000f); - long duracao = System.currentTimeMillis() - inicio; + inicio = System.currentTimeMillis(); + abertos = obterArmariosAbertosNLogN(contArmarios); + duracao = System.currentTimeMillis() - inicio; + System.out.println("\nO(n log n)"); + System.out.println("Número de armários abertos: " + abertos); + System.out.printf("Duração: %.3f segundos\n", duracao / 1000f); + + inicio = System.currentTimeMillis(); + abertos = obterArmariosAbertosN(contArmarios); + duracao = System.currentTimeMillis() - inicio; + System.out.println("\nO(n)"); + System.out.println("Número de armários abertos: " + abertos); + System.out.printf("Duração: %.3f segundos\n", duracao / 1000f); + + inicio = System.currentTimeMillis(); + abertos = obterArmariosAbertosSqrtN(contArmarios); + duracao = System.currentTimeMillis() - inicio; + System.out.println("\nO(n^0.5)"); + System.out.println("Número de armários abertos: " + abertos); + System.out.printf("Duração: %.3f segundos\n", duracao / 1000f); - System.out.println("Armários abertos: " + armariosAbertos); + inicio = System.currentTimeMillis(); + abertos = obterArmariosAbertosConstante(contArmarios); + duracao = System.currentTimeMillis() - inicio; + System.out.println("\nO(1)"); + System.out.println("Número de armários abertos: " + abertos); System.out.printf("Duração: %.3f segundos\n", duracao / 1000f); } }