Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int x = 100;
- int[] sortMe = new int[x]; //Array of 100 elements that we want to sort
- int fourTimes = 0;
- boolean incomplete = true;
- int startTime, endTime, diff;
- int eX = 10;
- int ran1, ran2;
- void setup () {
- size(400, 400);
- }
- void draw () {
- if (fourTimes <= 10) {
- createRandom();
- startTime = millis();
- bubbleSort(sortMe);
- endTime = millis();
- diff = (endTime - startTime);
- println(" ");
- println("It took a total of " + diff + " ms");
- println(" ");
- fill(0);
- ellipse(eX, 200 + -diff, 2, 2); //Draw plotpoint 200 down, so its in the middle
- eX += 5;
- fourTimes++;
- }
- }
- void createRandom () {
- incomplete = true;
- x += 10; //Adding 500 elements to the array should be enough to show the quadtric graph, if not try 1000
- sortMe = new int[x];//Randomize the elements in the array
- for (int s = 0; s < sortMe.length; s++) {
- sortMe[s] = floor(random(100)); //Round down and go from 0 to 100
- // print(sortMe[s] + "x "); //Display the random array
- }
- }
- int[] bubbleSort (int[] sortMe) {
- while (incomplete) {
- incomplete = false;
- for (int j = 0; j < sortMe.length -1; j++) {
- if (sortMe[j] > sortMe[j +1]) { //Check if prev element is greater then current element
- ran1 = floor(random(sortMe.length));
- ran2 = floor(random(sortMe.length));
- int stored = sortMe[ran1];
- sortMe[ran1] = sortMe[ran2];
- sortMe[ran2] = stored;
- //This line will sort two random numbers until the array is ordered.
- //This will likely never sort it in the proper order
- //This is terrible compared to the bubble sort
- //Times would be random and would likely not even show on the graph
- 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
- }
- }
- }
- for (int k = 1; k < sortMe.length; k++) {
- print(sortMe[k] + "-"); //Display the sorted array
- } //Showing the arrays will slow down our timer
- return sortMe;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement