排序

1.

 1 public class Test3 {
 2    /**
 3     * 冒泡算法
 4     */
 5     public static void BubbleSort(int arr[]){
 6         int i,j,temp;
 7         int flag = 0; 
 8         for (j = arr.length-1; j >=0; j--) {   //j的循环
 9             for(i=0;i<j;i++){  //一趟冒泡
10                 if(arr[i]>arr[i+1]){
11                     temp = arr[i];
12                     arr[i]= arr[i+1];
13                     arr[i+1] = temp;
14                     flag = 1; //表示发生了交换
15                 }
16             }
17             if(flag==0) break; 
18         }
19         
20     }
21     
22     public static void main(String[] args) {
23         int i =0;
24         int a[]={5,4,9,8,7,6,0,1,3,2};
25         BubbleSort(a);
26         for (i = 0; i < a.length; i++) {
27             System.out.print(a[i]+" ");
28         }
29 
30     }
31 }

2.

 1 public class Test4 {
 2 
 3     /**
 4      * 插入排序:初始时假设第一个记录自成一个有序序列,其余序列为无序序列。
 5      * 接着从第二个记录开始,按照记录的大小依次将当前处理的记录插入到其之前的有序序列中,
 6      * 直至最后一个记录插入到有序序列中为止。
 7      */
 8     public static void InsertSort(int a[]){
 9         int i,j,temp;
10         for(i=1;i<a.length;i++){  //从1开始,认为第一个记录是有序的
11             temp = a[i];
12             for(j=i;j>0&&temp<a[j-1];j--){  //这个for循环是为了移出空位
13                     a[j] = a[j-1];  
14                 }
15                 a[j] = temp;
16             }
17             
18         }
19     
20     public static void main(String[] args) {
21         int i =0;
22         int a[]={5,4,9,8,7,6,0,1,3,2};
23         InsertSort(a);
24         for (i = 0; i < a.length; i++) {
25             System.out.print(a[i]+" ");
26         }
27 
28     }
29 
30 }

3.

 1 public class Test {
 2     /**
 3      * 选择排序
 4      */
 5     public static void selectSort(int a[]){
 6         int i,j,len=a.length,temp=0,flag=0;
 7         for (i = 0; i < len; i++) {
 8             temp=a[i];
 9             flag=i;
10             for (j = i+1; j < len; j++) {
11                 if(a[j]<temp){
12                     temp=a[j];
13                     flag=j;
14                 }                
15             }
16             if(flag!=i){
17                 a[flag]=a[i];
18                 a[i]=temp;
19             }
20         }
21     }
22     
23     public static void main(String[] args){
24         int i =0;
25         int a[]={5,4,9,8,7,6,0,1,3,2};
26         selectSort(a);
27         for (i = 0; i < a.length; i++) {
28             System.out.print(a[i]+" ");
29         }
30     }
31 }

4.

 1 public class Test2 {
 2 
 3     /* 归并排序
 4      * arr[]待排序的数组
 5      * p:最左边的位置
 6      * q:最右边的位置
 7      * center:中间的位置
 8      */
 9     public static void Merge(int arr[],int p,int center,int q){
10         int i,j,n1,n2,k;
11         n1=center-p+1;
12         n2=q-center;
13         int[] L=new int[n1];
14         int[] R=new int[n2];
15         for(i=0,k=p;i<n1;i++,k++){
16             L[i] = arr[k];
17         }
18         for(i=0,k=center+1;i<n2;i++,k++){
19             R[i] = arr[k];
20         }
21         for(k=p,i=0,j=0;i<n1&&j<n2;k++){
22             if(L[i]<R[j]){
23                 arr[k]=L[i];
24                 i++;
25             }else{
26                 arr[k]=R[j];
27                 j++;
28             }
29         }
30         
31         if(i<n1){
32             for(j=i;j<n1;j++,k++){
33                 arr[k]=L[j];
34             }
35         }
36         if(j<n2){
37             for(i=j;i<n2;i++,k++){
38                 arr[k]=R[i];
39             }
40         }
41     }
42     
43     public static void MergeSort(int arr[],int p,int q){
44         if(p<q){
45             int center = (p+q)/2; 
46             MergeSort(arr,p,center);
47             MergeSort(arr,center+1,q);
48             Merge(arr,p,center,q);
49         }
50     }
51     
52     public static void main(String[] args) {
53         int i =0;
54         int a[]={5,4,9,8,7,6,0,1,3,2};
55         MergeSort(a,0,a.length-1);
56         for (i = 0; i < a.length; i++) {
57             System.out.print(a[i]+" ");
58         }
59 
60     }
61 
62 }

 

posted @ 2016-08-10 15:35  destinyruru  阅读(188)  评论(0编辑  收藏  举报