Нужно отсортировать до 10000 номеров, но я ограничен до 743 - PullRequest
0 голосов
/ 25 сентября 2019

, так что мой код, который я вставлю ниже, может сортировать 743 числа, в то время как мне нужно иметь возможность сортировать до 10000 чисел, мой текущий код отображает, какие числа были выбраны случайным образом, и сколько времени понадобилось для их сортировки, но я понятия не имею, почему его застрял и предел 743 даже знаю, что мой размер массива не имеет фиксированного размера, и это зависит от пользователя.

import java.util.Random;
import java.util.Scanner;
// Program to calculate execution time or elapsed time in Java
class Main
{
private static Scanner scan;

public static void main(String[] args) throws InterruptedException {

    long startTime = System.nanoTime();

    // ... the code being measured starts ...

    scan = new Scanner(System.in);
    Random rand = new Random();
    int size;
    int num;
    int values[];  
    System.out.println("What is the size of the array?");
    size = scan.nextInt();

    values = new int[size];
    System.out.println("The " + size + " random numbers are:");

    for(int c = 0; c < size; c++)
    {  
       num = rand.nextInt(10000);
       System.out.print((values[c] = num) + " ");  
    }
    for (int count = size - 1; count < values.length; count++) {
        bubbleSort(values);
    }

     long endTime = System.nanoTime();
     long timeElapsed = endTime - startTime;

     System.out.println("\n\nTime elapsed to sort "+ size + " numbers is - " + timeElapsed  + " milliseconds");
 }  

 public static void bubbleSort(int[] arr)
 {
    boolean swap;
    do
    {
       swap = false;
       int temp;
       for (int count = 0; count < arr.length - 1; count++)
       if (arr[count] > arr[count+1])
       {
          temp = arr[count];
          arr[count] = arr[count+1];
          arr[count+1] = temp;
          swap = true;


       }
     } while (swap);
       System.out.println("\n\nIn order:");
       for (int count = 0; count < arr.length; count++)
              System.out.print(arr[count]+ " ");
 }

}

...