面试老考这个,都背熟了
1
package
ch01;
2![](http://helloworlda.iteye.com/Images/OutliningIndicators/None.gif)
3
public
class
BubbleSort
4![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
5
6
7![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args)
{
8![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int[] values =
{
9
3,1,6,2,9,0,7,4,5
10
};
11
12
sort(values);
13![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(int i=0; i < values.length; ++i)
{
14
15
System.out.println("Index: " + i + "Value: " + values[i]);
16
17
}
18
19
20
}
21
22
23![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void sort(int[] values)
{
24
25
26
int temp;
27
28![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(int i=0 ; i < values.length ; ++i)
{
29
30![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(int j=0; j <values.length - i - 1; ++j)
{
31
32![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(values[j] > values[j + 1])
{
33
temp = values[j];
34
values[j] = values[j + 1];
35
values[j + 1] = temp;
36
37
38
39
}
40
41
42
43
}
44
45
46
}
47
}
48![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
49
}
50
![](http://helloworlda.iteye.com/Images/OutliningIndicators/None.gif)
2
![](http://helloworlda.iteye.com/Images/OutliningIndicators/None.gif)
3
![](http://helloworlda.iteye.com/Images/OutliningIndicators/None.gif)
4
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.blogjava.net/Images/dot.gif)
5
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
6
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
7
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.blogjava.net/Images/dot.gif)
8
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.blogjava.net/Images/dot.gif)
9
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
13
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.blogjava.net/Images/dot.gif)
14
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
16
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
20
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
22
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
23
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.blogjava.net/Images/dot.gif)
24
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
26
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
28
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.blogjava.net/Images/dot.gif)
29
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
30
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.blogjava.net/Images/dot.gif)
31
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
32
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.blogjava.net/Images/dot.gif)
33
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
34
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
35
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
36
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
37
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
38
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
39
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
41
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
42
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
43
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
44
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
45
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
46
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
47
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
48
![](http://helloworlda.iteye.com/Images/OutliningIndicators/InBlock.gif)
49
![](http://helloworlda.iteye.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
50
![](http://helloworlda.iteye.com/Images/OutliningIndicators/None.gif)