package chapter3;
public class InsertSort {
/**
* @param args
*/
public static void main(String[] args) {
int maxSize = 100;
ArrayInsert arr = new ArrayInsert(maxSize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display();
arr.InsertSort();
arr.display();
}
}
class ArrayInsert{
private int[] a;
private int nElems;
public ArrayInsert(int nElems){
a = new int[nElems];
nElems=0;
}
public void insert(int value){
a[nElems] = value;
nElems++;
}
public void display(){
for(int i=0;i<nElems;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
public void InsertSort(){
int in,out;
for(out=1;out<nElems;out++){
int temp = a[out];
in = out;
while(in>0 && a[in-1]>=temp){
a[in] = a[in-1];
in--;
}
a[in] = temp;
}
}
}