标签:demo play ++ blog ring initial end temp nts
/*
* insertSort.java
* demonstrates insertion sort
*/
public class ArrayIns {
//creat array
private long[] a;
private int nElems;
//constructor
public ArrayIns(int max) {
a = new long[max];
nElems = 0;
}
//put elements into array initially
public void insert(long value) {
a[nElems] = value;
nElems++;
}
//display array
public void display() {
for (int j = 0; j < nElems; j++) {
System.out.print(a[j] + "\t");
}
System.out.println("");
}
//function for insertionSort
public void insertionSort() {
int in, out;
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;
} //end for
} //end insertionSort
} //end class ArrayIns
public class InsertSortApp {
public static void main(String[] args) {
int maxSize = 100;
ArrayIns arr;
arr = new ArrayIns(maxSize);
arr.insert(100);
arr.insert(5);
arr.insert(55);
arr.insert(44);
arr.insert(32);
arr.insert(87);
arr.insert(23);
arr.insert(1);
arr.display();
arr.insertionSort();
arr.display();
} //end main
} //end class InsertSortApp
标签:demo play ++ blog ring initial end temp nts
原文地址:http://www.cnblogs.com/mmsumz/p/7865405.html