Javaバブルソートコード整理
Javaバブルソートコード整理:
package boke.sort;
/**
*
*
* @since jdk1.5
* @author
* @version 1.0
* @date 2010.05.24
*
*/
public class BubbleSort {
/**
* @param args
*/
public static void main(String[] args) {
int maxSize = 100;
BubbleSort bs = new BubbleSort(maxSize);
bs.insert(77);
bs.insert(66);
bs.insert(22);
bs.insert(99);
bs.insert(85);
bs.insert(37);
bs.insert(75);
bs.insert(64);
bs.insert(15);
bs.insert(35);
bs.output(); //
bs.bubbleSort(); //
bs.output(); //
}
private long[] a; //
private int nElems; //
/**
*
*
* @param maxSize
*/
public BubbleSort(int maxSize) {
a = new long[maxSize];
nElems = 0;
}
/**
*
*
* @param value
*/
public void insert(long value) {
a[nElems++] = value;
}
/**
*
*/
public void output() {
for (int j = 0; j < nElems; j++) {
System.out.print(a[j] + " ");
}
System.out.println("");
}
/**
*
*/
public void bubbleSort() {
int out, in;
for (out = nElems - 1; out > 1; out--) {
for (in = 0; in < out; in++) {
if (a[in] > a[in + 1]) {
swap(in, in + 1);
}
}
}
}
/**
*
*
* @param in
* @param i
*/
private void swap(int one, int two) {
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
}