gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
dodati MaxNiza i Pitagorine trojke - primeri za efikasnosta algoritama
authorDoni Pracner <quinnuendo@gmail.com>
Sun, 15 Mar 2015 10:52:50 +0000 (11:52 +0100)
committerDoni Pracner <quinnuendo@gmail.com>
Sun, 15 Mar 2015 10:52:50 +0000 (11:52 +0100)
kodovi/efikasnost-algoritama/MaxNizaPoredjenje.java [new file with mode: 0644]
kodovi/efikasnost-algoritama/Pitagora.java [new file with mode: 0644]

diff --git a/kodovi/efikasnost-algoritama/MaxNizaPoredjenje.java b/kodovi/efikasnost-algoritama/MaxNizaPoredjenje.java
new file mode 100644 (file)
index 0000000..c965c2d
--- /dev/null
@@ -0,0 +1,170 @@
+import java.util.Scanner;
+
+/**
+ * Program rešava problem maksimalnog podniza celih brojeva. Tačnije nalazi
+ * podniz susednih elemenata u nizu koji imaju maksimalnu sumu. Problem je rešen
+ * na 4 načina različite efikasnosti i dodati su brojači operacija sabiranja da
+ * bi se dodatno ilustrovale razlike.
+ */
+public class MaxNizaPoredjenje {
+
+       static void maxNiza1(int[] niz) {
+               int brojOp = 0;
+               int max = 0;
+               int pocetak = 1, kraj = 1;
+               
+               int n = niz.length;
+
+               for (int d = 0; d < n; d++) {
+                       for (int g = d; g < n; g++) {
+                               int suma = 0;
+                               for (int i = d; i <= g; i++) {
+                                       suma = suma + niz[i];
+                                       brojOp++;
+                               }
+                               if (suma > max) {
+                                       max = suma;
+                                       pocetak = d;
+                                       kraj = g;
+                               }
+                       }
+               }
+
+               System.out.println("Maksimum je " + max + " u intervalu od " + pocetak
+                               + " do " + kraj);
+               System.out.println("Broj racunskih operacija: " + brojOp);
+       }
+
+       static void maxNiza2(int[] niz) {
+               int brojOp = 0;
+               int pocetak = 1, kraj = 1;
+               int max = 0;
+
+               int n = niz.length;
+
+               int[] pom = new int[n + 1];
+
+               pom[0] = 0;
+               for (int i = 1; i < n + 1; i++) {
+                       pom[i] = pom[i - 1] + niz[i - 1];
+                       brojOp++;
+               }
+
+               for (int d = 0; d < n; d++) {
+                       for (int g = d; g < n; g++) {
+                               int suma = pom[g + 1] - pom[d];
+                               brojOp++;
+                               if (suma > max) {
+                                       max = suma;
+                                       pocetak = d;
+                                       kraj = g;
+                               }
+                       }
+               }
+
+               System.out.println("Maksimum je " + max + " u intervalu od " + pocetak
+                               + " do " + kraj);
+               System.out.println("Broj racunskih operacija: " + brojOp);
+       }
+
+       static void maxNiza3(int[] niz) {
+               int brojOp = 0;
+               int max = 0;
+               int pocetak = 1, kraj = 1;
+
+               int n = niz.length;
+
+               for (int d = 0; d < n; d++) {
+                       int suma = 0;
+                       for (int g = d; g < n; g++) {
+                               suma = suma + niz[g];
+                               brojOp++;
+                               if (suma > max) {
+                                       max = suma;
+                                       pocetak = d;
+                                       kraj = g;
+                               }
+                       }
+               }
+
+               System.out.println("Maksimum je " + max + " u intervalu od " + pocetak
+                               + " do " + kraj);
+               System.out.println("Broj racunskih operacija: " + brojOp);
+       }
+
+       static void maxNiza4(int[] niz) {
+               int brojOp = 0;
+               int max = 0;
+               int pocetak = 1, kraj = 1;
+
+               int n = niz.length;
+
+               int maxDovde = 0;
+
+               int d = 0;
+               for (int i = 0; i < n; i++) {
+                       if (maxDovde == 0) {
+                               d = i;
+                       }
+                       maxDovde = maxDovde + niz[i];
+                       brojOp++;
+                       if (maxDovde < 0) {
+                               maxDovde = 0;
+                       }
+
+                       if (maxDovde > max) {
+                               max = maxDovde;
+                               pocetak = d;
+                               kraj = i;
+                       }
+               }
+
+               System.out.println("Maksimum je " + max + " u intervalu od " + pocetak
+                               + " do " + kraj);
+               System.out.println("Broj racunskih operacija: " + brojOp);
+       }
+
+       static void stampajNiz(int[] niz) {
+               System.out.println("Duzina niza: " + niz.length);
+               for (int i = 0; i < niz.length; i++) {
+                       System.out.println(i + " " + niz[i]);
+               }
+       }
+       
+       static int[] ucitajNiz() {
+               Scanner in = new Scanner(System.in);
+               System.out.print("Unesite broj brojeva u nizu X:");
+               int n = in.nextInt();
+               int[] niz = new int[n];
+
+               System.out.print("Unesite niz x:");
+               for (int i = 0; i < n; i++) {
+                       niz[i] = in.nextInt();
+               }
+               in.close();
+               
+               return niz;
+       }
+
+       public static void main(String[] args) {
+               int[] niz = ucitajNiz();
+                               
+               stampajNiz(niz);
+               System.out.println();
+
+               System.out.println("Metod 1: ");
+               maxNiza1(niz);
+               System.out.println();
+
+               System.out.println("Metod 2: ");
+               maxNiza2(niz);
+               System.out.println();
+
+               System.out.println("Metod 3: ");
+               maxNiza3(niz);
+               System.out.println();
+
+               System.out.println("Metod 4: ");
+               maxNiza4(niz);
+       }
+}
\ No newline at end of file
diff --git a/kodovi/efikasnost-algoritama/Pitagora.java b/kodovi/efikasnost-algoritama/Pitagora.java
new file mode 100644 (file)
index 0000000..f4e890e
--- /dev/null
@@ -0,0 +1,131 @@
+public class Pitagora {
+
+       /**
+        * Generisanje svih trojki brojeva do neke granice uz proveru da li je to
+        * upravo Pitagorina trojka.
+        */
+       static void trojke1() {
+               int br = 0;
+               int gr = 100;
+
+               System.out.println("\nPitagorine trojke\n");
+
+               for (int x = 1; x <= gr; x++) {
+                       for (int y = 1; y <= gr; y++) {
+                               for (int z = 1; z <= gr; z++) {
+                                       if (x * x + y * y == z * z) {
+                                               br++;
+                                               stampajTrojku(br, x, y, z);
+                                       }
+                               }
+                       }
+               }
+       }
+
+       /**
+        * Generisanje parova brojeva a,b i provera da li postoji celo c koje bi
+        * formiralo Pitagorinu trojku.
+        */
+       static void trojke2() {
+               int br = 0;
+               int gr = 100;
+
+               System.out.println("\nPitagorine trojke 2\n");
+
+               for (int x = 1; x <= gr; x++) {
+                       for (int y = 1; y <= gr; y++) {
+                               double zreal = x * x + y * y;
+                               zreal = Math.sqrt(zreal);
+                               // Provera da li je ceo broj
+                               int z = (int) zreal;
+                               if (zreal == z) {
+                                       br++;
+                                       stampajTrojku(br, x, y, z);
+                               }
+                       }
+               }
+       }
+
+       /**
+        * Euklidova teorema daje Pitagorinu trojku za bilo koja dva prirodna broja
+        * n i m.
+        */
+       static void trojke3() {
+               int br = 0;
+               int gr3 = 10;
+
+               System.out.println("\nPitagorine trojke 3\n");
+
+               for (int m = 1; m <= gr3; m++) {
+                       for (int n = 1; n <= m - 1; n++) {
+                               int x = m * m - n * n;
+                               int y = 2 * m * n;
+                               int z = m * m + n * n;
+                               br++;
+                               stampajTrojku(br, x, y, z);
+                       }
+               }
+       }
+
+       /**
+        * Generisanje trojki kod kojih je razlika izmedju a i c 1. Postupak je
+        * izveden preko Euklidove teoreme.
+        */
+       static void trojke4() {
+               int br = 0;
+               int gr4 = 10;
+
+               System.out.println("\nPitagorine trojke 4\n");
+
+               for (int m = 2; m <= gr4; m++) {
+                       int n = m - 1;
+                       int a = 2 * m * n;
+                       int b = m * m - n * n;
+                       int c = m * m + n * n;
+                       br++;
+                       stampajTrojku(br, a, b, c);
+               }
+       }
+
+       /**
+        * Generisanje trojki kod kojih je razlika izmedju a i b 1. Postupak je
+        * izveden preko Euklidove teoreme i koriste se inkrementalna resenja
+        * dobijene Pell-ove jednacine.
+        */
+       static void trojke5() {
+               int gr5 = 6;
+               int br = 0;
+
+               System.out.println("\nPitagorine trojke 5\n");
+
+               int w = 3;
+               int n = 2;
+               for (int i = 1; i <= gr5; i++) {
+                       int m = n + w;
+                       int a = 2 * m * n;
+                       int b = m * m - n * n;
+                       int c = m * m + n * n;
+                       br++;
+                       stampajTrojku(br, a, b, c);
+                       int temp = w;
+                       w = 3 * w + 4 * n;
+                       n = 2 * temp + 3 * n;
+               }
+       }
+
+       public static void stampajTrojku(int br, int x, int y, int z) {
+               System.out.print(br + " ");
+               System.out.print("a = " + x);
+               System.out.print(", b = " + y);
+               System.out.print(", c = " + z);
+               System.out.println();
+       }
+
+       public static void main(String[] args) {
+               trojke1();
+               trojke2();
+               trojke3();
+               trojke4();
+               trojke5();
+       }
+}
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner