基本知识(01) -- 冒泡排序和选择排序

冒泡排序

  基本思路: 冒泡排序会利用双层循环对需要排序的数列进行若干次遍历,每次遍历都会从前往后依次的比较相邻两个数的大小;如果前者比后者大,则交换它们的位置;这样,经过第一轮比较排序后便可把最大(或者最小)的元素排好,然后再用相同的方法把剩下的元素逐个进行比较,就得到了所要的排序。

下面直接来看看具体的代码:

  1 /**
  2  * 演示冒泡排序
  3  * @author Administrator
  4  */
  5 public class BubbleSort {
  6     /**
  7      * 第一种冒泡排序:每轮遍历将要排序的元素放置到后面 
  8      * @param array
  9      */
 10     public void bubbleSort1(int[] array){
 11         //    'i' 用来控制比较的次数
 12         for(int i = array.length-1;i > 0;i--){
 13             // 下面的循环完成,表示一轮冒泡的结束
 14             for(int j = 0;j < i;j++){
 15                 if(array[j] > array[j+1]){
 16                     //如果前面的数据比后面的大,则进行交互
 17                     swap(array,j,j+1);
 18                 }
 19             }
 20         }
 21     }
 22     
 23     /**
 24      * 第一种冒泡排序改进:利用一个flag标记
 25      * @param array
 26      */
 27     public void bubbleSort1_1(int[] array){
 28         //    'i' 用来控制比较的次数
 29         for(int i = array.length-1;i > 0;i--){
 30             int flag = 0;
 31             // 下面的循环完成,表示一轮冒泡的结束
 32             for(int j = 0;j < i;j++){
 33                 if(array[j] > array[j+1]){
 34                     //如果前面的数据比后面的大,则进行交互
 35                     swap(array,j,j+1);
 36                     
 37                     flag = 1;//如果有哪一轮遍历时没有进行值进行交互位置,则说明这个数列已经是排好序了
 38                 }
 39             }
 40             if(flag == 0){
 41                 break;
 42             }
 43         }
 44     }
 45     
 46     /**
 47      * 第二种冒泡排序:每轮遍历将要排序的元素的放置在前面
 48      * @param array
 49      */
 50     public void bubbleSort2(int[] array){
 51         //外层取一个用来比较的数据位置
 52         for(int i = 0;i < array.length-1;i++){
 53             //内层循环依次取出需要用来比较的数据
 54             for(int j = i+1; j < array.length; j++){
 55                 if(array[i] > array[j]){
 56                     swap(array,i,j);
 57                 }
 58             }
 59         }
 60     }
 61     
 62     /**
 63      * 对数组中的两个位置上的值进行交互
 64      * @param array
 65      * @param aIndex
 66      * @param bIndex
 67      */
 68     private void swap(int[] array,int aIndex,int bIndex){
 69         int temp = array[aIndex];
 70         array[aIndex] = array[bIndex];
 71         array[bIndex] = temp;
 72     }
 73     
 74     public static void main(String[] args) {
 75         int[] num = new int[10];
 76         
 77         /*随机生成10个int值*/
 78         for(int i = 0;i < 10;i++){
 79             num[i] = (int)(Math.random() * 1000);
 80             System.out.print(num[i] + " ");
 81         }
 82         
 83         BubbleSort bubbleSort = new BubbleSort();
 84         
 85         System.out.println("");
 86         System.out.println("---------------------------");
 87         bubbleSort.bubbleSort1(num);//第一种排序方式
 88         for(int n : num){
 89             System.out.print(n + " ");
 90         }
 91         
 92         System.out.println("");
 93         System.out.println("---------------------------");
 94         bubbleSort.bubbleSort1_1(num);//第一种排序方式
 95         for(int n : num){
 96             System.out.print(n + " ");
 97         }
 98         
 99         
100         System.out.println("");
101         System.out.println("---------------------------");
102         bubbleSort.bubbleSort2(num);//第二种排序方式
103         for(int n : num){
104             System.out.print(n + " ");
105         }
106     }
107 }
View Code

选择排序(对冒泡排序的一种改进,减少元素的交换次数)

  基本思路:第一轮遍历从所有元素中选择一个最小元素a[i]放在a[0](即让最小元素a[i]与a[0]交换)

       第二轮是从a[1]开始到最后元素中选择一个最小的元素,放在a[1]中

       ...依次类推

看看具体的代码实现:

 1 /**
 2  * 演示选择排序算法
 3  * @author Administrator
 4  *
 5  */
 6 public class SelectSort {
 7     public void selectSort(int[] array){
 8         int min = 0;
 9         for(int i = 0;i < array.length-1;i++){
10             //设置初始的最小的位置
11             min = i;
12             //内层循环:每轮遍历都只是找出要要交互的位置,并没有进行交互
13             for(int j = i+1; j < array.length;j++){
14                 if(array[min] > array[j]){
15                     min = j;
16                 }
17             }
18             swap(array,min,i);
19         }
20     }
21     
22     private void swap(int[] array,int aIndex,int bIndex){
23         int temp = array[aIndex];
24         array[aIndex] = array[bIndex];
25         array[bIndex] = temp;
26     }
27     
28     public static void main(String[] args) {
29         int[] num = new int[10];
30         
31         /*随机生成10个int值*/
32         for(int i = 0;i < 10;i++){
33             num[i] = (int)(Math.random() * 1000);
34             System.out.print(num[i] + " ");
35         }
36         
37         SelectSort selectSort = new SelectSort();
38         
39         System.out.println("");
40         System.out.println("---------------------------");
41         selectSort.selectSort(num);//第一种排序方式
42         for(int n : num){
43             System.out.print(n + " ");
44         }
45     }
46 }
View Code

 

posted @ 2016-12-31 16:03  火爆泡菜  阅读(221)  评论(0编辑  收藏  举报