package binary;
/**
* 基礎資料型別 (Elementary Data Type)的簡單排序
* @author tfq
*
*/
public class EasySortArray {
private long[] arr;
private int nItems;
public EasySortArray(int maxLength){
this.arr=new long[maxLength];
this.nItems=0;
}
/*
* put element into array
*/
public void insert(int element){
//insert it
arr[nItems]=element;
//increment size
nItems++;
}
/**
* sort array element from small to big
*
*/
public void easySort(){
int out,in;
for(out=0;out<nItems;out++){
for(in=out;in<nItems;in++){
if(arr[out]>arr[in]){
long temp=arr[out];
arr[out]=arr[in];
arr[in]=temp;
}
}
}
}
/**
* change array[one]'s value into array[two]'s value
* if add the code to easySort contributing to save sort time
* @param one
* @param two
*/
public void swap(int one,int two){
long temp=arr[one];
arr[one]=arr[two];
arr[two]=temp;
}
public void display(){
for(int i=0;i<nItems;i++){
System.out.print(arr[i]+" ");
}
}
public static void main(String[] args) {
int maxLength=10;
EasySortArray esArray=new EasySortArray(maxLength);
esArray.insert(1);
esArray.insert(3);
esArray.insert(5);
esArray.insert(7);
esArray.insert(2);
esArray.insert(4);
esArray.insert(6);
esArray.insert(8);
esArray.insert(10);
esArray.display();
System.out.println("-------");
esArray.easySort();
esArray.display();
}
}