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

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

C语言代码:

#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <time.h>

void init(int *array, int count)
{
    int n = 0;
    srand((unsigned int)time(NULL));

    for (n=0; n<count; n++)
    {
        array[n] = rand()%100 + 1;
    }
}

void output(int *array, int count)
{
    int n = 0;
    for (n=0; n<count; n++)
    {
        printf("%5d", array[n]);
    }

    printf("\n");
}

void bubblesort(int *array, int count, int type)
{  
    int i = 0;
    int j = 0;
    int temp = 0;  
    if (type == 0)  //从小排到大  
    {
        for (i = 0; i < count - 1; i++)   
        {  
            //将相邻两个数进行比较,较大的数下沉,较小的数上升  
            for (j = 0; j < count - i-1; j++)   
            {  
                if (array[j] > array[j + 1])   
                {  
                    temp = array[j];  
                    array[j] = array[j+1];  
                    array[j+1] = temp;
                }  
            }  
        }  
    }   
    else //倒排序,从大排到小  
    {   
        for (i =0; i < count-1; i++)   
        {  
            //将相邻两个数进行比较,较大的数往后冒泡  
            for (j = 0; j < count - i-1; j++)   
            {  
                if (array[j] <array[j + 1])   
                {
                    //交换相邻两个数  
                    temp = array[j];  
                    array[j] = array[j+1];  
                    array[j+1] = temp;
                }
            }  
        }  
    }  
} 

int main()
{
    const int count = 10;
    int array[count];

    memset(array, 0, sizeof(int)*count);

    init(array, count);
    
    printf("data before sort:      ");
    output(array, count);

    bubblesort(array, count, 0);
    printf("data after sort(asc):  ");
    output(array, count);

    bubblesort(array, count, 1);
    printf("data after sort(desc): ");
    output(array, count);
    
    return 0;
}
运行结果如下:
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代码:

import java.util.Random;

/*
 * 冒泡排序----交换排序的一种
 * 方法:相邻两元素进行比较,如有需要则进行交换,每完成一次循环就将最大元素排在最后(如从小到大排序),
 * 下一次循环是将其他的数进行类似操作。
 */

public class Sort 
{
	/*
	 * 输出数组中的数据
	 */
    public static void OutputArray(int[] array) 
    {
        for (int data : array) 
        {
            System.out.print(data + "\t");
        }
        
        System.out.println();
    }
    
    /*
     * 生成需要排序的数组
     */
    public static int[] createArray(int count) 
    {
        int array[] = new int[count];
        Random r = new Random();
        
        for (int i = 0; i < count; i++) 
        {
            array[i] = r.nextInt(100);
        }
        
        System.out.println("");
        System.out.print("data before sort:\t");
        
        OutputArray(array);
        
        return array;
    }
    
    /*
     * 冒泡排序
     */
    public static void bubbleSort(int[] array, String sortType) 
    {
        int temp;
        if (sortType.equals("asc"))  //从小排到大
        {   
            for (int i = 0; i < array.length - 1; i++) 
            {
                //将相邻两个数进行比较,较大的数下沉,较小的数上升
                for (int j = 0; j < array.length - i-1; j++) 
                {
                    if (array[j] > array[j + 1]) 
                    {
                        temp=array[j];
                        array[j]=array[j+1];
                        array[j+1]=temp;
                    }
                }
            }
        } 
        else if (sortType.equals("desc"))  //倒排序,从大排到小
        { 
            for (int i =0; i <array.length-1; i++) 
            {
                //将相邻两个数进行比较,较大的数往后冒泡
                for (int j = 0; j <array.length - i-1; j++) 
                {
                    if (array[j] <array[j + 1]) 
                    {
                        //交换相邻两个数
                        temp=array[j];
                        array[j]=array[j+1];
                        array[j+1]=temp;
                    }
                }
            }
        } 
        else 
        {
            System.out.println("您输入的排序类型错误!");
        }
    }
    
    public static void main(String[] args) 
    {
        int[] arr1=createArray(10);
        
        System.out.print("data after sort(asc):\t");
        bubbleSort(arr1, "asc");
        OutputArray(arr1);
        
        System.out.print("data after sort(desc):\t");
        bubbleSort(arr1, "desc");
        OutputArray(arr1);
    }
}

运行结果如下:
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  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值