常用排序算法(冒泡排序)

冒泡排序(BubbleSort)的基本概念是:依次比较相邻的两个数,将小数放在前面,大数放在后面。即在第一趟:首先比较第1个和第2个数,将小数放前,大数放后。然后比较第2个数和第3个数,将小数放前,大数放后,如此继续,直至比较最后两个数,将小数放前,大数放后。至此第一趟结束,将最大的数放到了最后。在第二趟:仍从第一对数开始比较(因为可能由于第2个数和第3个数的交换,使得第1个数不再小于第2个数),将小数放前,大数放后,一直比较到倒数第二个数(倒数第一的位置上已经是最大的),第二趟结束,在倒数第二的位置上得到一个新的最大数(其实在整个数列中是第二大的数)。如此下去,重复以上过程,直至最终完成排序。
由于在排序过程中总是小数往前放,大数往后放,相当于气泡往上升,所以称作冒泡排序。
更多信息请参考:http://baike.baidu.com/view/254413.htm

C语言代码:

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 #include <memory.h>
 4 #include <time.h>
 5 
 6 void init(int *array, int count)
 7 {
 8     int n = 0;
 9     srand((unsigned int)time(NULL));
10 
11     for (n=0; n<count; n++)
12     {
13         array[n] = rand()%100 + 1;
14     }
15 }
16 
17 void output(int *array, int count)
18 {
19     int n = 0;
20     for (n=0; n<count; n++)
21     {
22         printf("%5d", array[n]);
23     }
24 
25     printf("\n");
26 }
27 
28 void bubblesort(int *array, int count, int type)
29 {  
30     int i = 0;
31     int j = 0;
32     int temp = 0;  
33     if (type == 0)  //从小排到大  
34     {
35         for (i = 0; i < count - 1; i++)   
36         {  
37             //将相邻两个数进行比较,较大的数下沉,较小的数上升  
38             for (j = 0; j < count - i-1; j++)   
39             {  
40                 if (array[j] > array[j + 1])   
41                 {  
42                     temp = array[j];  
43                     array[j] = array[j+1];  
44                     array[j+1] = temp;
45                 }  
46             }  
47         }  
48     }   
49     else //倒排序,从大排到小  
50     {   
51         for (i =0; i < count-1; i++)   
52         {  
53             //将相邻两个数进行比较,较大的数往后冒泡  
54             for (j = 0; j < count - i-1; j++)   
55             {  
56                 if (array[j] <array[j + 1])   
57                 {
58                     //交换相邻两个数  
59                     temp = array[j];  
60                     array[j] = array[j+1];  
61                     array[j+1] = temp;
62                 }
63             }  
64         }  
65     }  
66 } 
67 
68 int main()
69 {
70     const int count = 10;
71     int array[count];
72 
73     memset(array, 0, sizeof(int)*count);
74 
75     init(array, count);
76     
77     printf("data before sort:      ");
78     output(array, count);
79 
80     bubblesort(array, count, 0);
81     printf("data after sort(asc):  ");
82     output(array, count);
83 
84     bubblesort(array, count, 1);
85     printf("data after sort(desc): ");
86     output(array, count);
87     
88     return 0;
89 }

运行结果如下:
data before sort:         33   24   13   90   72   43    6   76    7   16
data after sort(asc):      6    7   13   16   24   33   43   72   76   90
data after sort(desc):    90   76   72   43   33   24   16   13    7    6


Java代码:

  1     import java.util.Random;  
  2       
  3     /* 
  4      * 冒泡排序----交换排序的一种 
  5      * 方法:相邻两元素进行比较,如有需要则进行交换,每完成一次循环就将最大元素排在最后(如从小到大排序), 
  6      * 下一次循环是将其他的数进行类似操作。 
  7      */  
  8       
  9     public class Sort   
 10     {  
 11         /* 
 12          * 输出数组中的数据 
 13          */  
 14         public static void OutputArray(int[] array)   
 15         {  
 16             for (int data : array)   
 17             {  
 18                 System.out.print(data + "\t");  
 19             }  
 20               
 21             System.out.println();  
 22         }  
 23           
 24         /* 
 25          * 生成需要排序的数组 
 26          */  
 27         public static int[] createArray(int count)   
 28         {  
 29             int array[] = new int[count];  
 30             Random r = new Random();  
 31               
 32             for (int i = 0; i < count; i++)   
 33             {  
 34                 array[i] = r.nextInt(100);  
 35             }  
 36               
 37             System.out.println("");  
 38             System.out.print("data before sort:\t");  
 39               
 40             OutputArray(array);  
 41               
 42             return array;  
 43         }  
 44           
 45         /* 
 46          * 冒泡排序 
 47          */  
 48         public static void bubbleSort(int[] array, String sortType)   
 49         {  
 50             int temp;  
 51             if (sortType.equals("asc"))  //从小排到大  
 52             {     
 53                 for (int i = 0; i < array.length - 1; i++)   
 54                 {  
 55                     //将相邻两个数进行比较,较大的数下沉,较小的数上升  
 56                     for (int j = 0; j < array.length - i-1; j++)   
 57                     {  
 58                         if (array[j] > array[j + 1])   
 59                         {  
 60                             temp=array[j];  
 61                             array[j]=array[j+1];  
 62                             array[j+1]=temp;  
 63                         }  
 64                     }  
 65                 }  
 66             }   
 67             else if (sortType.equals("desc"))  //倒排序,从大排到小  
 68             {   
 69                 for (int i =0; i <array.length-1; i++)   
 70                 {  
 71                     //将相邻两个数进行比较,较大的数往后冒泡  
 72                     for (int j = 0; j <array.length - i-1; j++)   
 73                     {  
 74                         if (array[j] <array[j + 1])   
 75                         {  
 76                             //交换相邻两个数  
 77                             temp=array[j];  
 78                             array[j]=array[j+1];  
 79                             array[j+1]=temp;  
 80                         }  
 81                     }  
 82                 }  
 83             }   
 84             else   
 85             {  
 86                 System.out.println("您输入的排序类型错误!");  
 87             }  
 88         }  
 89           
 90         public static void main(String[] args)   
 91         {  
 92             int[] arr1=createArray(10);  
 93               
 94             System.out.print("data after sort(asc):\t");  
 95             bubbleSort(arr1, "asc");  
 96             OutputArray(arr1);  
 97               
 98             System.out.print("data after sort(desc):\t");  
 99             bubbleSort(arr1, "desc");  
100             OutputArray(arr1);  
101         }  
102     }  

运行结果如下:
data before sort:    7    7    80    62    77    14    5    62    94    70    
data after sort(asc):    5    7    7    14    62    62    70    77    80    94    
data after sort(desc):    94    80    77    70    62    62    14    7    7    5  

 

转载于:https://www.cnblogs.com/yuanping/archive/2012/12/23/2829763.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值