-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSortApp.java
More file actions
67 lines (57 loc) · 1016 Bytes
/
BubbleSortApp.java
File metadata and controls
67 lines (57 loc) · 1016 Bytes
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
class BubbleSort
{
public long[] mArray;
public int nElems;
public BubbleSort(int max)
{
mArray = new long[max];
nElems = 0;
}
public void insert(long value)
{
mArray[nElems] = value;
nElems++;
}
public void display()
{
for(int i=0;i<nElems;i++)
{
System.out.print(mArray[i] + " ");
}
System.out.println(" ");
}
public void BubbleSortMethod()
{
int i,j;
for(j=nElems-1;j>0;j--)
{
for(i=0;i<j;i++)
{
if(mArray[i] > mArray[i+1])
Swap(i,i+1);
System.out.println(i+" "+j+" ");
}
}
}
private void Swap(int value1, int value2)
{
long temp = mArray[value1];
mArray[value1] = mArray[value2];
mArray[value2] = temp;
}
}
class BubbleSortApp
{
public static void main(String args[])
{
BubbleSort arr;
arr = new BubbleSort(100);
for(int j = 0;j<100;j++)
{
arr.insert((long)(Math.random()*1000.0));
}
arr.display();
arr.BubbleSortMethod();
arr.display();
}
}