 public class SortDemo {
private int[] sortArray;

 public SortDemo() {
sortArray=new int[8];
}

 public void swap(int i,int j) {
int t=sortArray[i];
sortArray[i]=sortArray[j];
sortArray[j]=t;
}

 public void insertArray(int pos,int val) {
sortArray[pos]=val;
}

 public void selectSort() {
int t,tt;
 for(int j=0;j<sortArray.length-1;j++) {

 for(int i=j+1;i<sortArray.length;i++) {

if(sortArray[i]<sortArray[j]) swap(i,j);
}
}
}


 public void bubbleSort() {
int exchange=sortArray.length;
int bound;
 while(exchange!=0) {
bound=exchange-1;
exchange=0;
 for(int i=0;i<bound;i++) {
 if(sortArray[i]>sortArray[i+1]) {
swap(i,i+1);
exchange=i+1;
}
}
}

}

 public void insertSort() {
 for(int i=1;i<sortArray.length;i++) {
int temp=sortArray[i];
int j=i;
 while(j>0&&temp<sortArray[j-1]) {
sortArray[j]=sortArray[j-1];
j--;
}
sortArray[j]=temp;
}

}

 public void showResult() {
for(int i =0;i<sortArray.length;i++)
System.out.print(sortArray[i]+" ");
System.out.println("");
}

 public static void main(String[] args) {

SortDemo so=new SortDemo();
so.insertArray(0,2);
so.insertArray(1,23);
so.insertArray(2,22);
so.insertArray(3,12);
so.insertArray(4,42);
so.insertArray(5,32);
so.insertArray(6,62);
so.insertArray(7,52);
so.showResult();
so.selectSort();
so.showResult();
so.bubbleSort();
so.showResult();
so.insertSort();
so.showResult();

}
}
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|
常用链接
留言簿(1)
随笔分类
随笔档案
搜索
最新评论

阅读排行榜
评论排行榜
|
|