Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void bubble(int[] array) {
- for (int i = 1; i <= array.length - 1; i++) {
- for (int j=0; j < array.length-1; j++) {
- if (array[j] > array[j + 1]) {
- int aux = array[j];
- array[j] = array[j + 1];
- array[j + 1] = aux;
- }
- }
- }
- }
- public static void selection(int[] array) {
- for (int i=0; i < array.length-1; i++) {
- int lowestIndex = i;
- for(int j=i+1; j < array.length; j++) {
- if(array[j] < array[lowestIndex])
- lowestIndex = j;
- }
- int aux = array[i];
- array[i] = array[lowestIndex];
- array[lowestIndex] = aux;
- }
- }
- public static void insercao(int vet[]) {
- for(int i = 1; i < vet.length; i++){
- int aux = vet[i];
- int j = i - 1;
- while (j >= 0 && vet[j] < aux) {
- vet[j + 1] = vet[j];
- j--;
- }
- vet[j + 1] = aux;
- }
- }
- public static void main(String[] args) {
- String response1 = "\nMetodo da bolha\n";
- String response2 = "\nMetodo da Seleção\n";
- String response3 = "\nMetodo da Inserção\n";
- for (int tam = 5000; tam <= 40000; tam = tam + 5000) {
- int[] array1 = new int[tam];
- int[] array2 = new int[tam];
- int[] array3 = new int[tam];
- for (int i = 0; i < tam; i++)
- array1[i] = array2[i] = array3[i] = (int) (Math.random() * 1000);
- long initialTime = System.currentTimeMillis();
- bubble(array1);
- long finalTime = System.currentTimeMillis();
- response1 += "Tamanho = " + tam + " => Tempo = " + (finalTime - initialTime) + " ms\n";
- initialTime = System.currentTimeMillis();
- selection(array2);
- finalTime = System.currentTimeMillis();
- response2 += "Tamanho = " + tam + " => Tempo = " + (finalTime - initialTime) + " ms\n";
- initialTime = System.currentTimeMillis();
- insercao(array3);
- finalTime = System.currentTimeMillis();
- response3 += "Tamanho = " + tam + " => Tempo = " + (finalTime - initialTime) + " ms\n";
- }
- System.out.println(response1);
- System.out.println(response2);
- System.out.println(response3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement