码迷,mamicode.com
首页 > 编程语言 > 详细

排序算法

时间:2016-09-03 12:16:25      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

 1     public static void sort(int[] a,int left,int right) {     //快速排序
 2          if(left < right){
 3              int key = a[left];
 4              int low = left;
 5              int high = right;
 6              while(low < high){
 7                      while(low < high && a[high] > key){
 8                              high--;
 9                      }
10                      
11                      while(low < high && a[low] < key){
12                              low++;
13                      }
14                      
15                      a[low] = a[low]^a[high];
16                      a[high] = a[high]^a[low];
17                      a[low] = a[low]^a[high];                    
18              }
19              a[low] = key;
20              sort(a,left,low-1);
21              sort(a,low+1,right);
22              }
23     }

 

 1     public static void sort(int[] a) {   //冒泡排序
 2          for(int i=0;i<a.length;i++) {
 3              for(int j=0;j<a.length-1;j++) {
 4                  if(a[j]>a[j+1]) {
 5                      a[j] = a[j]^a[j+1];
 6                      a[j+1] = a[j+1]^a[j];
 7                      a[j] = a[j]^a[j+1];
 8                  }
 9              }
10          }
11     }

 

 1     public static void sort(int[] a) {    //选择排序
 2     
 3         for(int i=0;i<a.length;i++) {
 4             int flag = i;
 5             for(int j=i;j<a.length-1;j++) {                
 6                 if(a[j+1]<a[flag]) {
 7                     flag = j+1;
 8                 }
 9             }
10             if(flag!=i) {
11             a[i] = a[i]^a[flag];
12             a[flag] = a[flag]^a[i];
13             a[i] = a[i]^a[flag];
14             }
15         }
16     }

 

 1     public static void sort(int[] a) {  //插入排序
 2         int flag,j;
 3         for(int i=1;i<a.length;i++) {
 4             flag = a[i];
 5             for(j=i-1;j>=0 && flag<a[j];j--) {
 6                 a[j+1] = a[j];
 7             }
 8             a[j+1] = flag;
 9         }
10     }

 

 1     public static int[] sort(int[] a, int low, int high) {    //归并排序
 2         int mid = (low + high) / 2;  
 3         if (low < high) {  
 4             // 左 
 5             sort(a, low, mid);  
 6             // 右
 7             sort(a, mid + 1, high);  
 8             // 合并
 9             merge(a, low, mid, high);  
10         }  
11         return a;  
12     } 
13      public static void merge(int[] a, int low, int mid, int high) {  
14             int[] flag = new int[high - low + 1];  
15             int i = low;
16             int j = mid + 1;
17             int k = 0;  
18       
19             
20             while (i <= mid && j <= high) {  
21                 if (a[i] < a[j]) {  
22                     flag[k++] = a[i++];  
23                 } else {  
24                     flag[k++] = a[j++];  
25                 }  
26             }  
27       
28            //剩余
29             while (i <= mid) {  
30                 flag[k++] = a[i++];  
31             }  
32       
33             //剩余
34             while (j <= high) {  
35                 flag[k++] = a[j++];  
36             }  
37       
38             
39             for (int k2 = 0; k2 < flag.length; k2++) {  
40                 a[k2 + low] = flag[k2];  
41             }  
42         }  

 

排序算法

标签:

原文地址:http://www.cnblogs.com/yzdd/p/5833193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!