java - 简单排序

冒泡排序

View Code
 1 package testSort;
 2 /**
 3  * 10-7 冒泡排序
 4  * 五个数,每轮选出一个最大的数,一轮选一个数,四轮之后,还有一个数就是最小的不需要选了,需要四轮,即array.length-1
 5  * 第一轮有五个数,需要比较四次
 6  * 第二轮有四个数,需要比较三次
 7  * 第三轮有三个数,需要比较两次
 8  * 第四轮有两个数,需要比较一次
 9  * 外层变量控制比较轮数,内层控制比较次数,例如5个数的数组,比较5轮,第一轮比较4次,第二轮比较3次。。。
10  * @author Administrator
11  *
12  */
13 public class BubbleSort {
14     public static void main(String[] args) {
15         int[] array = {5,8,3,0,7};
16         
17         for(int i = 0; i < array.length-1; i++) {
18             for(int j = 0; j < array.length-1-i; j++) {
19                 int temp;
20                 /**从小排到大,假设前面的数比后面的都大,如果不这样假设,那岂不是不用排序了(即前面的都比后面的小),实际当中没人
21                 *知道数组里面的大小,这样的话如果前面的都比后米的小,就不用重新赋值,反之则重新赋值
22                 */
23                 if(array[j] > array[j+1]) {
24                     temp = array[j];
25                     array[j] = array[j+1];
26                     array[j+1] = temp;
27                 }
28             }
29         }
30         
31         for(int k = 0; k < array.length; k++) {
32             System.out.print(array[k]);
33         }
34     }
35 }

快速排序

View Code
 1 package testSort;
 2 /**
 3  * 2011-10-09 快速排序
 4  * @author Administrator
 5  *
 6  */
 7 public class QuickSort3 {
 8     static int times = 0;//排了几次
 9     //从小到大的排序方法
10     public static void qucikSort(int[] array, int low, int high) {
11         int i = low,j = high;
12         if(low < high) {
13             int refVal = array[i];//调用此方法只有一开始执行了,后面的代码都是while里的代码在执行,所以refval一直都是37没变,假定的中间值
14             while(i != j) {
15                 times++;
16                 /**
17                  * 从后向前寻找较小值
18                  */
19                 while (i < j && array[j] >= refVal) {
20                     j--;
21                 }
22                 
23                 if (i < j) {
24                     array[i] = array[j];
25                     i++;
26                 }
27                 
28                 /**
29                  * 从前向后寻找较大值
30                  */
31                 while (i < j && array[i] < refVal) {
32                     i++;
33                 }
34                 if (i < j) {
35                     array[j] = array[i];
36                     j--;
37                 }
38                 System.out.print("第" + times + "次交换后的数据:");
39                 print(array);
40             }
41             
42             //此时i=j,且空出一个位置,将基准值传给array[i]
43             array[i] = refVal;
44             qucikSort(array, low, j-1);
45             qucikSort(array, i+1, high);
46             
47         }
48     }
49     
50     public static void print(int[] array) {
51         for(int i = 0; i < array.length; i++) {
52             System.out.print(array[i] + "\t");
53         }
54         System.out.println();
55     }
56     
57     public static void main(String[] args) {
58         int[] array = {37,45,4,9,333,66,73,3,0};
59         System.out.print("最初的数据:");
60         print(array);
61         
62         qucikSort(array, 0, array.length-1);
63         System.out.print("排序的数据:");
64         print(array);
65     }
66 }
View Code
 1 /**
 2  * 
 3  */
 4 package testSort;
 5 
 6 /**
 7  * @description
 8  *                    此类是 百度百科中的快速排序
 9  * @package name      
10  *                    testSort.QuickSortByNet.java
11  * @author            
12  *                    lj
13  * @since              
14  *                    2012-4-29下午04:52:16
15  */
16 public class QuickSortByNet {
17     public static void sort(Comparable[] data,int low,int high) {
18         // 枢纽元,一般以第一个元素为基准进行划分
19         int i = low;
20         int j = high;
21         if (low < high) {
22             // 从数组两端交替地向中间扫描
23             Comparable pivotKey = data[low];
24             // 进行扫描的指针i,j;i从左边开始,j从右边开始
25             while (i < j) {
26                 while (i < j && data[j].compareTo(pivotKey) > 0) {
27                     j--;
28                 }// end while
29                 if (i < j) {
30                     // 比枢纽元素小的移动到左边
31                     data[i] = data[j];
32                     i++;
33                 }// end if
34                 while (i < j && data[i].compareTo(pivotKey) < 0) {
35                     i++;
36                 }// end while
37                 if (i < j) {
38                     // 比枢纽元素大的移动到右边
39                     data[j] = data[i];
40                     j--;
41                 }// end if
42             }// end while
43             // 枢纽元素移动到正确位置
44             data[i] = pivotKey;
45             // 前半个子表递归排序
46             sort(data,low,i - 1);
47             // 后半个子表递归排序
48             sort(data,i + 1,high);
49         }// end if
50     }// end sort
51     public static void main(String[] args) {
52         // 在JDK1.5版本以上,基本数据类型可以自动装箱
53         // int,double等基本类型的包装类已实现了Comparable接口
54         Comparable[] c = { 4,9,23,1,45,27,5,2 };
55         sort(c,0,c.length - 1);
56         for (Comparable data : c) {
57             System.out.println(data);
58         }
59     } 
60 }

选择排序

View Code
 1 package testSort;
 2 /**
 3  * 2011-10-8 选择排序
 4  * @author Administrator
 5  *
 6  */
 7 public class SelectSort {
 8     public static int[] array = {1,8,3,4,7};
 9     
10     public static void main(String[] args) {
11         select1();
12         System.out.println();
13         select2();
14         System.out.println();
15         select3();
16         System.out.println();
17         /*select2();
18         System.out.println();*/
19     }
20     
21     private static void select1() {
22         for(int i = 0; i < array.length; i++) {//选出一个数
23             for(int j=i+1; j < array.length; j++) {//与选出之后的每一个数做比较
24                 int temp;
25                 if(array[i] > array[j]) {
26                     temp = array[i];
27                     array[i] = array[j];
28                     array[j] = temp;
29                 }
30             }
31             System.out.print(array[i]);
32         }
33     }
34     
35     private static void select2() {
36         for(int i = 0; i < array.length; i++) {
37             int min = i;//假设最小的数就在i这个索引这里
38             for(int j=min+1; j < array.length; j++) {//与选出的最小的数后面的数做比较
39                 if(array[min] > array[j]) { //如果后面的数还比前面的数小
40                     min = j;                //就将此数的索引传给MIN
41                 }
42             }
43             if(min != i) {
44                 int temp = array[i];
45                 array[i] = array[min];
46                 array[min] = temp;
47             }
48             System.out.print(array[min]);
49         }
50     }
51     /**
52      * 将变量的定义位置作出调整,减少内存的占用
53      */
54     private static void select3() {
55         int min,temp; 
56         for(int i = 0; i < array.length; i++) {
57             min = i;//假设最小的数就在i这个索引这里
58             for(int j=min+1; j < array.length; j++) {//与选出的最小的数后面的数做比较
59                 if(array[min] > array[j]) { //如果后面的数还比前面的数小
60                     min = j;                //就将此数的索引传给MIN
61                 }
62             }
63             if(min != i) {
64                 temp = array[i];
65                 array[i] = array[min];
66                 array[min] = temp;
67             }
68             System.out.print(array[min]);
69         }
70     }
71 }

posted on 2012-04-29 17:03  lovebeauty  阅读(227)  评论(0编辑  收藏  举报

导航