luishenriique

Atividade 01 - TAP II

Aug 1st, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.00 KB | None | 0 0
  1. /*
  2.  * @Ano: 2013/2
  3.  * @Escola: Pontifícia Universidade Católica do Paraná
  4.  * @Curso: Engenharia de Computação
  5.  * @Autor: Luis Henrique de Souza, Maicon Augusto Tibola
  6.  */
  7.  
  8. package br.pucpr;
  9.  
  10. import java.io.*;
  11. import java.util.Scanner;
  12.  
  13.     public class MainCls {
  14.         static char vet[];
  15.         static long timeInicial;
  16.         static String dir = "";
  17.         static int tam[] = { 1024, 10240, 102400, 512000, 1048576, 2097152 };
  18.         static long comparacoes = 0, trocas = 0;
  19.         static Scanner scanner = new Scanner(System.in);
  20.  
  21.         public static void selectMetodo() throws IOException {
  22.             int opcMet = 0, opcTam = 0;
  23.  
  24.             System.out.println("Criar arquivo de:");
  25.             System.out.println("<1> 1K | <2> 10K | <3> 100K | <4> 500K | <5> 1M | <6> 2M | <0> Arquivo existente");
  26.             System.out.print("Arquivo: ");
  27.             opcTam = scanner.nextInt();
  28.  
  29.             if (opcTam == 0) {
  30.                 System.out.print("Arquivo: ");
  31.                 opcTam = scanner.nextInt();
  32.  
  33.                 lerArquivo(opcTam);
  34.             } else {
  35.                 criarArquivo(opcTam);
  36.                 lerArquivo(opcTam);
  37.             }
  38.  
  39.             System.out.println("Informe o método desejado:");
  40.             System.out.println("(1) Buble Sort, (2) QuickSort, (3) HeapSort, (4) MergeSort");
  41.             System.out.print("Método: ");
  42.             opcMet = scanner.nextInt();
  43.  
  44.             timeInicial = System.currentTimeMillis();
  45.             switch (opcMet) {
  46.             case 1:
  47.                 bubbleSort(vet);
  48.                 System.out.println("\nBubble Sort:");
  49.                 imprimirDados();
  50.                 break;
  51.             case 2:
  52.                 quickSort(vet, 0, vet.length - 1);
  53.                 System.out.println("\nQuick Sort:");
  54.                 imprimirDados();
  55.                 break;
  56.             case 3:
  57.                 heapSort(vet);
  58.                 System.out.println("\nHeap Sort:");
  59.                 imprimirDados();
  60.                 break;
  61.             case 4:
  62.                 mergeSort(vet, 0, vet.length - 1);
  63.                 System.out.println("\nMerge Sort:");
  64.                 imprimirDados();
  65.                 break;
  66.             default:
  67.                 System.out.println("Opção inválida.");
  68.             }
  69.  
  70.             gravarArquivo(opcTam);
  71.             System.out.println("Arquivo salvo: " + dir);
  72.         }
  73.  
  74.     public static void criarArquivo(int n) throws IOException {
  75.         int imp = 0, i;
  76.  
  77.         dir = "C:/temp/" + tam[n - 1] + ".txt";
  78.         FileWriter arq = new FileWriter(dir);
  79.         PrintWriter gravarArq = new PrintWriter(arq);
  80.  
  81.         for (i = 0; i < tam[n - 1]; i++) {
  82.             imp = (int) (Math.random() * 10);
  83.             gravarArq.print(imp);
  84.         }
  85.         arq.close();
  86.     }
  87.  
  88.    
  89.     public static void lerArquivo(int n) throws IOException {
  90.         try{
  91.         dir = "C:/temp/" + tam[n - 1] + ".txt";
  92.         BufferedReader buf = new BufferedReader(new FileReader(dir));
  93.         String line = "", l;
  94.         while ((l = buf.readLine()) != null) {
  95.             line += l;
  96.         }
  97.         vet = line.toCharArray();
  98.         }catch(Exception erro){
  99.             System.out.println(erro.getMessage() + "\n");
  100.             System.exit(0);
  101.         }
  102.     }
  103.  
  104.     public static void gravarArquivo(int n) throws IOException {
  105.         dir = "C:/temp/" + tam[n - 1] + "_organizado.txt";
  106.         FileWriter arq = new FileWriter(dir);
  107.         PrintWriter gravarArq = new PrintWriter(arq);
  108.  
  109.         gravarArq.print(vet);
  110.         arq.close();
  111.     }
  112.  
  113.     public static void imprimirDados() {
  114.         System.out.println("Comparações: " + comparacoes + "\nTrocas: " + trocas);
  115.         System.out.println("Tempo: " + (System.currentTimeMillis() - timeInicial) + "ms");
  116.     }
  117.  
  118.     // BUBBLE SORT \\
  119.     public static void bubbleSort(char v[]) {
  120.         char aux = 0;
  121.         int i, j;
  122.  
  123.         for (i = 0; i < v.length; i++) {
  124.             for (j = 0; j < (v.length - 1); j++) {
  125.                 comparacoes++;
  126.                 if (v[j] > v[j + 1]) {
  127.                     trocas++;
  128.                     aux = v[j];
  129.                     v[j] = v[j + 1];
  130.                     v[j + 1] = aux;
  131.                 }
  132.             }
  133.         }
  134.     }
  135.  
  136.     // QUICK SORT \\
  137.     private static void quickSort(char[] input, int start, int end) {
  138.         int currStart = start, currEnd = end;
  139.         int pivot = input[start + (end - start) / 2];
  140.  
  141.         while (currStart <= currEnd) {
  142.             comparacoes++;
  143.             while (input[currStart] < pivot) {
  144.                 comparacoes++;
  145.                 currStart++;
  146.             }
  147.  
  148.             while (input[currEnd] > pivot) {
  149.                 comparacoes++;
  150.                 currEnd--;
  151.             }
  152.  
  153.             if (currStart <= currEnd) {
  154.                 trocas++;
  155.                 char temp = input[currStart];
  156.                 input[currStart] = input[currEnd];
  157.                 input[currEnd] = temp;
  158.                 currStart++;
  159.                 currEnd--;
  160.             }
  161.         }
  162.  
  163.         if (start < currEnd)
  164.             quickSort(input, start, currEnd);
  165.  
  166.         if (currStart < end)
  167.             quickSort(input, currStart, end);
  168.     }
  169.  
  170.     // HEAP SORT \\
  171.     public static void heapSort(char v[]) {
  172.         buildMaxHeap(v);
  173.         int n = v.length;
  174.  
  175.         for (int i = v.length - 1; i > 0; i--) {
  176.             comparacoes++;
  177.             swap(v, i, 0);
  178.             maxHeapify(v, 0, --n);
  179.         }
  180.     }
  181.  
  182.     private static void buildMaxHeap(char v[]) {
  183.         for (int i = v.length / 2 - 1; i >= 0; i--) {
  184.             comparacoes++;
  185.             maxHeapify(v, i, v.length);
  186.         }
  187.     }
  188.  
  189.     private static void maxHeapify(char v[], int pos, int n) {
  190.         int maxi;
  191.         int l = 2 * pos + 1;
  192.         int right = 2 * pos + 2;
  193.         if ((l < n) && (v[l] > v[pos])) {
  194.             maxi = l;
  195.         } else {
  196.             maxi = pos;
  197.         }
  198.         if (right < n && v[right] > v[maxi]) {
  199.             maxi = right;
  200.         }
  201.         if (maxi != pos) {
  202.             swap(v, pos, maxi);
  203.             maxHeapify(v, maxi, n);
  204.         }
  205.     }
  206.  
  207.     public static void swap(char v[], int j, int aposJ) {
  208.         trocas++;
  209.         char aux = v[j];
  210.         v[j] = v[aposJ];
  211.         v[aposJ] = aux;
  212.     }
  213.  
  214.     // MERGE SORT \\
  215.     private static void mergeSort(char v[], int start, int end) {
  216.         int mid = (start + end) / 2;
  217.         if (start < end) {
  218.             mergeSort(v, start, mid);
  219.             mergeSort(v, mid + 1, end);
  220.             merge(v, start, mid, end);
  221.         }
  222.     }
  223.  
  224.     private static void merge(char v[], int start, int mid, int end) {
  225.         char inputCopy[] = new char[v.length];
  226.         int firstArrStart = start, secondArrStart = mid + 1;
  227.         for (int i = start; i <= end; i++) {
  228.             inputCopy[i] = v[i];
  229.         }
  230.         while (secondArrStart <= end && firstArrStart <= mid) {
  231.             comparacoes++;
  232.             if (inputCopy[firstArrStart] >= inputCopy[secondArrStart]) {
  233.                 trocas++;
  234.                 v[start++] = inputCopy[secondArrStart++];
  235.             } else {
  236.                 trocas++;
  237.                 v[start++] = inputCopy[firstArrStart++];
  238.             }
  239.         }
  240.         while (firstArrStart <= mid) {
  241.             comparacoes++;
  242.             v[start++] = inputCopy[firstArrStart++];
  243.         }
  244.         while (secondArrStart <= end) {
  245.             comparacoes++;
  246.             v[start++] = inputCopy[secondArrStart++];
  247.         }
  248.     }
  249.  
  250.     public static void main(String[] args) throws IOException {
  251.         selectMetodo();
  252.     }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment