Advertisement
Guest User

Untitled

a guest
May 27th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. int x = 100;
  2. int[] sortMe = new int[x]; //Array of 100 elements that we want to sort
  3. int fourTimes = 0;
  4. boolean incomplete = true;
  5. int startTime, endTime, diff;
  6. int eX = 10;
  7. int ran1, ran2;
  8. void setup () {
  9. size(400, 400);
  10. }
  11.  
  12. void draw () {
  13. if (fourTimes <= 10) {
  14. createRandom();
  15. startTime = millis();
  16.  
  17. bubbleSort(sortMe);
  18. endTime = millis();
  19.  
  20. diff = (endTime - startTime);
  21. println(" ");
  22. println("It took a total of " + diff + " ms");
  23. println(" ");
  24. fill(0);
  25. ellipse(eX, 200 + -diff, 2, 2); //Draw plotpoint 200 down, so its in the middle
  26. eX += 5;
  27. fourTimes++;
  28. }
  29. }
  30.  
  31. void createRandom () {
  32. incomplete = true;
  33. x += 10; //Adding 500 elements to the array should be enough to show the quadtric graph, if not try 1000
  34. sortMe = new int[x];//Randomize the elements in the array
  35. for (int s = 0; s < sortMe.length; s++) {
  36. sortMe[s] = floor(random(100)); //Round down and go from 0 to 100
  37. // print(sortMe[s] + "x "); //Display the random array
  38. }
  39. }
  40.  
  41. int[] bubbleSort (int[] sortMe) {
  42. while (incomplete) {
  43. incomplete = false;
  44. for (int j = 0; j < sortMe.length -1; j++) {
  45. if (sortMe[j] > sortMe[j +1]) { //Check if prev element is greater then current element
  46. ran1 = floor(random(sortMe.length));
  47. ran2 = floor(random(sortMe.length));
  48. int stored = sortMe[ran1];
  49. sortMe[ran1] = sortMe[ran2];
  50. sortMe[ran2] = stored;
  51. //This line will sort two random numbers until the array is ordered.
  52. //This will likely never sort it in the proper order
  53. //This is terrible compared to the bubble sort
  54. //Times would be random and would likely not even show on the graph
  55. incomplete = true; //This is always false unless a swap has been made, if a swap has been made, we know its incomplete and needs to keep going
  56. }
  57. }
  58. }
  59. for (int k = 1; k < sortMe.length; k++) {
  60. print(sortMe[k] + "-"); //Display the sorted array
  61. } //Showing the arrays will slow down our timer
  62. return sortMe;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement