package date0609;
/**
*@author TonyJ
*@time 2011-6-9 05:45:16
*/
public class Test02 {
private long[] a;
private int nElems;
public Test02(int max) {
a = new long[max];
nElems = 0;
}
public int size() {
return nElems;
}
public void add(long data) {
a[nElems++] = data;
}
public int find(long desData) {//
int low = 0;
int high = nElems - 1;
int cur;
while (true) {
cur = (low + high) / 2;
if (a[cur] == desData) {
return cur;
} else if (low > high) {
return -1;
} else {
if (a[cur] < desData) {
low = cur + 1;
} else {
high = cur - 1;
}
}
}
}
public long get(int index) { //
if (index > nElems) {
System.out.print(" ");
return -1;
} else {
return a[index];
}
}
public boolean delete(long desData) {//
int j = find(desData);
if (j == -1) {
return false;
} else {
for (int i = j; i < nElems - 1; i++) {
a[i] = a[i + 1];
}
nElems--;
return true;
}
}
public void display() {
for (int i = 0; i < nElems; i++) {
System.out.print(a[i] + ",");
}
System.out.println();
}
public void bubbleSort() {//
for (int i = 1; i < nElems; i++) {
for (int j = 0; j < nElems; j++) {
if (a[i] < a[j]) {
swap(i, j);
}
}
}
}
public void selectSort() {//
int out, in, min;
for (out = 0; out < nElems - 1; out++) {
min = out;
for (in = out + 1; in < nElems; in++) {
if (a[in] < a[min]) {
min = in;
}
}
swap(out, min);
}
}
public void insertSort() {//
int out, in;
long temp;
for (out = 1; out < nElems; out++) {
temp = a[out];
in = out;
while (in > 0 && a[in - 1] >= temp) {
a[in] = a[in - 1];
--in;
}
a[in] = temp;
}
}
private void swap(int one, int two) {
long temp;
temp = a[one];
a[one] = a[two];
a[two] = temp;
}
public static void main(String[] args) {
Test02 t = new Test02(6);
t.add(3);
t.add(4);
t.add(5);
t.add(1);
t.add(2);
t.add(6);
t.insertSort();
System.out.println(t.get(4));
System.out.println(t.find(2));
// t.bubbleSort();
// t.selectSort();
t.display();
t.delete(3);
t.display();
}
}