posts - 14, comments - 15, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

java 排序算法四例

Posted on 2009-06-11 11:10 析木 阅读(234) 评论(0)  编辑  收藏 所属分类: JAVA
class weijySort {
  
3    public static void main (String[] args) {
  
4        int gohome[] = new int[]{12,7,54,21,1,4,65,76,34,9,3,6};    
  
5        System.out.println("插入排序算法");
  
6//        InsertionSort(gohome);
  7        System.out.println("-------------------------------------------");
  
8        System.out.println("选择排序算法");
  
9//        SelectSort(gohome);
 10        System.out.println("-------------------------------------------");
 
11        System.out.println("冒泡排序算法");
 
12//        BubbleSort(gohome);
 13        System.out.println("-------------------------------------------");
 
14        gohome =QuickSort(gohome);
 
15        
 
16        for (int t=0; t<gohome.length;t++)
 
17        {
 
18            System.out.print(gohome[t]+"--");
 
19        }
 
 
20    }

 
21    
 
22    //插入排序算法
 23    public static void InsertionSort(int[] goal)
 
24    {    
 
25        for (int i = 1; i<goal.length; i++)
 
26        {    int now = i;
 
27            int frank = goal[i];            
 
28            while (now>0 && goal[now-1<= frank)
 
29            {
 
30                goal[now]=goal[now-1];
 
31                now--;            
 
32            }

 
33            goal[now]=frank;
 
34            
 
35        
 
36        }
    
 
37        
 
38            
 
39        for (int t=0; t<goal.length;t++)
 
40        {
 
41            System.out.print(goal[t]+"--");
 
42        }

 
43    }

 
44    
 
45    //选择排序算法    
 46    public static void SelectSort(int[] goal)
 
47    {        
 
48        int max;
 
49        int stmp;    
 
50        for (int i = 0; i<goal.length-1; i++)
 
51        {
 
52            max=i;
 
53            for (int j = i+1; j<goal.length; j++)
 
54                if(goal[j]>goal[max])
 
55                    max=j;
 
56                                    
 
57            stmp = goal[i];
 
58            goal[i]=goal[max];
 
59            goal[max]=stmp;            
 
60        
 
61        }
    
 
62        for (int t=0; t<goal.length;t++)
 
63        {
 
64            System.out.print(goal[t]+"--");
 
65        }

 
66            
 
67    
 
68    }

 
69    
 
70    //冒泡排序算法    
 71    public static void BubbleSort(int[] goal)
 
72    {    int stmp;
 
73        for (int i = 1; i< goal.length; i++)
 
74        {
 
75            for(int j=0; j<i;j++)
 
76            {
 
77                if(goal[i]>goal[j])
 
78                {
 
79                    stmp=goal[i];
 
80                    goal[i]=goal[j];
 
81                    goal[j]=stmp;    
 
82                }
    
 
83            }
    
 
84    
 
85        }

 
86        for (int t=0; t<goal.length;t++)
 
87        {
 
88            System.out.print(goal[t]+"--");
 
89        }

 
90    }

 
91    
 
92    //快速排序算法
 93    public static int[] QuickSort(int[] number) {
 
94      QuickSort(number, 0, number.length-1);
 
95      return number ;
 
96      }

 
97    private static void QuickSort(int[] number,int left, int right) {
 
98        int stmp;
 
99      if(left < right) {
100          System.out.println(left+" | "+right+" | "+(left+right)/2);
101          int s = number[(left+right)/2];
102          int i = left - 1;
103          int j = right + 1;
104          while(true{
105                // 向右找
106                while(number[++i] > s) ;
107                  // 向左找
108                while(number[--j] < s) ;
109                  if(i >= j)
110                      break;             
111                stmp = number[i];
112                  number[i] = number[j];
113                  number[j] = stmp;              
114              }

115          QuickSort(number, left, i-1); // 对左边进行递回
116          QuickSort(number, j+1, right); // 对右边进行递回
117      }
     
118      }

119 }


只有注册用户登录后才能发表评论。


网站导航:
 
我要啦免费统计