package aa;
class InsertSort{
private long[] a;
private int nElems;
//构造方法
public InsertSort(int max){
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++)
{
System.out.println(a[j] + " ");
}
System.out.println("");
}
//排序方法
public long[] insertionSort(){
int out,in;
for(out = 1; out < nElems; out++)
{
long temp = a[out];
in = out;
while(in > 0 && a[in-1] >= temp)
{
a[in] = a[in - 1];
in --;
}
a[in] = temp;
}
return a;
}
//去重方法
public void noDups(long[] arr){
for(int i = 1; i < nElems - 1; i++)//遍历数组元素,从第二项开始
{
for(int j = 0; j < i ; j++)//每一轮比较的次数
{
if(a[j] == a[i]) //如果遍历项和之前某一项相等
{
int k = i;//取出遍历项的索引
while(k+1 < nElems){//遍历 遍历项之后的元素项
a[k] = a[k+1];//将遍历项之后的元素项前移一位
k++;
}
nElems --;//数组长度减一
i--;//因为遍历项后一项前移到遍历项,如果i不减一则少比较一项
}
}
}
}
}
public class InsertSortApp {
public static void main(String[] args){
int maxSize = 100;
InsertSort arr = new InsertSort(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(77);
arr.insert(77);
arr.display();
long[] sortarr = arr.insertionSort();
arr.display();
arr.noDups(sortarr);
arr.display();
}
}