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

Asked

Viewed 1,013 times

0

I need to generate 100000 numbers, store them in an array and sort them using the Bubblesort algorithm, is appearing 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);
     }  
}

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 out of a method. Also, you have not implemented the Bubble Sort algorithm in your code and there are keys without closure.

I tidied up your code. The Bubble Sort 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;
                }
            }
        }
    }
}

Example of your program output:

65737
37487
85573
87395
...
68212
65816
25052
--Bubblesort--
23,910 ms

0

You are not using the "time" variable. So it can be ejected.

For Sort, you can make your own Bublle Sort algorithm or use Arrays.Sort() - this one doesn’t use Bubble Sort.

Bubble algorithm (if you want to sort the other way around, just reverse it):

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;
            }
        }
    }
}

Adapting 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;
                }
            }
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.