/**
* 冒泡排序
* Bubble sort
*/
package com.PZHCCB.sort;public class BubbleSort
{
private long[] a;
private int nElems;
//-------------------------
public BubbleSort(int max)
{
1
a = new long[max];
nElems = 0;
}
//-------------------------
public void insert(long value){
a[nElems] = value;
nElems++;
}
//--------------------------
public void display()
{
for(int j = 0; j < nElems; j++)
2
{
System.out.print(a[j] + \" \");
}
System.out.println(\"\");
}
//--------------------------
public void bubbleSort()
{
int out, in;
for(out = nElems - 1; out > 0; out--)
{
for(in = 0; in < out; in++)
{
3
if(a[in] > a[in+1])
{
swap(in, in+1);
}
}
}
}
//---------------------------
private void swap(int one, int two)
{
long temp = a[one];
a[one] = a[two];
a[two] = temp;
4
}
}
class BubbleSortApp
{
public static void main(String[] args)
{
int maxSize = 100;
BubbleSort arr = new BubbleSort(maxSize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
5
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display();
arr.bubbleSort();
System.out.println(\"---------After sort----------\");
arr.display();
}
}
6
因篇幅问题不能全部显示,请点此查看更多更全内容