-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleSort.java
85 lines (63 loc) · 2.12 KB
/
BubbleSort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bubblesort;
import java.util.Random;
/**
*
* @author Keevah
*/
public class BubbleSort {
private int[] theArray;
static int r= 2;
private int arraySize;
static long startTime;
static long endTime;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int [] n = {2,10,100,1000,2000,3000,10000,100000};
int [] m= {2,10,100,1000,2000,10000,100000};
for(int i=0; i<n.length;i++){
BubbleSort testAlgo2 = new BubbleSort(n[i]);
//System.out.println("n is: "+n[i]);
for(int j=0;j<m.length;j++){
startTime = System.nanoTime();
for (int x = 0; x < r; x++) {
testAlgo2.generateRandomArray(m[j]);
testAlgo2.bubbleSort();
}
endTime = System.nanoTime();
System.out.println("AVG RUN TIME " + (endTime - startTime)/r);
}
}
}
public void bubbleSort() {
for(int i=0; i<arraySize - 1; i++){
for(int j=arraySize - 1; j>i; j--){
if(theArray[j] < theArray[j-1]){
swapValues(j, j-1);
}
}
}
}
public void swapValues(int indexOne, int indexTwo) {
int temp = theArray[indexOne];
theArray[indexOne] = theArray[indexTwo];
theArray[indexTwo] = temp;
}
BubbleSort(int size) {
arraySize = size;
theArray = new int[size];
}
public void generateRandomArray(int m) {
for (int i = 0; i < arraySize; i++) {
Random rand = new Random();
theArray[i] = rand.nextInt(m);
}
}
}