Generate random numbers in java, store in a vector and sort them

0

I need to generate 100000 numbers, store them in a vector and sort them using the BubbleSort algorithm, the error "Exception in thread" main "java.lang.Error: Unresolved compilation problem:

at testaSort.main(testaSort.java:8)"

Code below:

import java.util.*;
import java.util.Random;
import javax.swing.*;


    public class testaSort {

        public static void main(String[] args)
        {
            int i;
            long inicio;
            long fim;
            double tempo;
            int[] vetor = new int [100000];
            int intervaloInicial = 0;
            int intervaloFinal = 100000;
            for (i=0 ; i < vetor.length; i++)
            {
                vetor [i] = getRandomNumberRange(intervaloInicial , intervaloFinal);
                System.out.println(vetor[i]);}
            }
            private static int getRandomNumberRange(int min, int max)
            {
                Random r = new Random();
                return r.ints (min,(max+1)).limit(1).findFirst().getAsInt();
            }


            //BubbleSort
            System.out.println("--Bubblesort--");
            inicio = System.currentTimeMillis();
            Sort.bubbleSort(vetor);
            fim = System.currentTimeMillis();
            System.out.printf("%.3f ms%n", (fim - inicio) / 1000d);
     }  
}
    
asked by anonymous 14.05.2017 / 03:43

2 answers

1

This message indicates that there are syntax errors that prevent your code from being compiled.

For example, the code block below the comment //BubbleSort is outside of a method. In addition, you have not implemented the Bubble Sort algorithm in your code and there are keys without closing.

I gave a tidy in your code. The Bubble Sort that I implemented was removed from this site: BubbleSort in Java and C

import java.util.*;
import java.util.Random;
import javax.swing.*;

public class testaSort {
    public static void main(String[] args) {
        int i;
        long inicio;
        long fim;
        //double tempo;
        int[] vetor = new int[100000];
        int intervaloInicial = 0;
        int intervaloFinal = 100000;
        for (i = 0; i < vetor.length; i++) {
            vetor[i] = getRandomNumberRange(intervaloInicial, intervaloFinal);
            System.out.println(vetor[i]);
        }
        //BubbleSort
        System.out.println("--Bubblesort--");
        inicio = System.currentTimeMillis();
        bubbleSort(vetor);
        fim = System.currentTimeMillis();
        System.out.printf("%.3f ms%n", (fim - inicio) / 1000d);
    }

    private static int getRandomNumberRange(int min, int max) {
        Random r = new Random();
        return r.ints(min, (max + 1)).limit(1).findFirst().getAsInt();
    }

    public static void bubbleSort(int[] a){
        if(a == null){
            throw new NullPointerException("The array doesn't exist.");
        }
        for(int i = 0; i < a.length - 1; i++){
            for(int j = 0; j < a.length - i - 1; j++){
                if(a[j] > a[j + 1]){
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
    }
}

Sample output from your program:

65737
37487
85573
87395
...
68212
65816
25052
--Bubblesort--
23,910 ms
    
02.09.2018 / 16:47
0

You are not using the variable "time". Then it can be removed.

For sort, you can make your own bublle sort algorithm or use Arrays.sort () - that does not use bubble sort.

Bubble algorithm (if you want to reverse, just reverse the for):

private void bubbleSort(int[] vetor) {
    int aux;
    for(int i = 0; i<5; i++){
        for(int j = 0; j<4; j++){
            if(vetor[j] > vetor[j + 1]){
                aux = vetor[j];
                vetor[j] = vetor[j+1];
                vetor[j+1] = aux;
            }
        }
    }
}

Tailoring your code:

public class testaSort {

    public static void main(String[] args) {
        int i;
        long inicio;
        long fim;
        int[] vetor = new int[100000];
        int intervaloInicial = 0;
        int intervaloFinal = 100000;
        for (i = 0; i < vetor.length; i++) {
            vetor[i] = getRandomNumberRange(intervaloInicial, intervaloFinal);
            System.out.println(vetor[i]);
        }
        // BubbleSort
        System.out.println("--Bubblesort--");
        inicio = System.currentTimeMillis();
        // Arrays.sort(vetor);
        bubbleSort(vetor);
        fim = System.currentTimeMillis();
        System.out.printf("%.3f ms%n", (fim - inicio) / 1000d);
    }

    private void bubbleSort(int[] vetor) {
        int aux;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 4; j++) {
                if (vetor[j] > vetor[j + 1]) {
                    aux = vetor[j];
                    vetor[j] = vetor[j + 1];
                    vetor[j + 1] = aux;
                }
            }
        }
    }
}
    
16.05.2017 / 18:18