fork download
  1. public class Main {
  2. private int[] theArray;
  3. private int arraySize;
  4. private int itemsInArray = 0;
  5. static long startTime;
  6. static long endTime;
  7.  
  8. public static void main(String[] args) {
  9. Main testBubbleSortAlgo = new Main(10000);
  10. testBubbleSortAlgo.generateRandomArray();
  11.  
  12. Main testBubbleSortAlgoAgain = new Main(90000);
  13. testBubbleSortAlgoAgain.generateRandomArray();
  14.  
  15. testBubbleSortAlgo.bubbleSort();
  16. testBubbleSortAlgoAgain.bubbleSort();
  17.  
  18. }
  19.  
  20. Main(int size) {
  21. arraySize = size;
  22. theArray = new int[size];
  23. }
  24.  
  25. public void bubbleSort() {
  26.  
  27. startTime = System.currentTimeMillis();
  28.  
  29. for (int i = arraySize - 1; i > 1; i--) {
  30.  
  31. for (int j = 0; j < i; j++) {
  32.  
  33. if (theArray[j] > theArray[j + 1]) {
  34.  
  35. int temp = theArray[j];
  36. theArray[j] = theArray[j+1];
  37. theArray[j+1] = temp;
  38.  
  39. }
  40. }
  41. }
  42.  
  43. endTime = System.currentTimeMillis();
  44.  
  45. System.out.println("Bubble Sort Took " + (endTime - startTime));
  46. }
  47.  
  48. public void generateRandomArray() {
  49.  
  50. for (int i = 0; i < arraySize; i++) {
  51. theArray[i] = (int) (Math.random() * 1000) + 10;
  52. }
  53.  
  54. itemsInArray = arraySize - 1;
  55.  
  56. }
  57. }
  58.  
Time limit exceeded #stdin #stdout 5s 974336KB
stdin
Standard input is empty
stdout
Bubble Sort Took 190