zoukankan      html  css  js  c++  java
  • 冒泡排序

    冒泡排序在一组需要排序的数组中,对两两数据顺序与要求顺序相反时,交换数据,使大的数据往后移,每趟排序将最大的数放在最后的位置上,数据的变化像冒泡一样往上升的。

    #include <stdio.h>
    
    
    void printArray(int arr[], int n)
    {
        int i;
        for(i = 0; i < n; i++)
        {
            printf("%d ", arr[i]);
        }
        printf("
    ");
    }
    
    
    void bubbleSort(int arr[], int n)
    {
        int i, j, tmp;
    
        for(i = 0; i < n - 1; i++)
        {
            for(j = 1; j < n; j++)
            {
                if(arr[j] < arr[j - 1])
                {
                    tmp = arr[j];
                    arr[j] = arr[j - 1];
                    arr[j - 1] = tmp;
                }
    			
    			printf("the inner %d,%d sort: ",i+1,j);
    			printArray(arr, 10);			
            }
    		
    		printf("the %d sort: ",i+1);
    		printArray(arr, 10);
        }
    }
    
    
    
    
    int main()
    {
        int arr[10] = {2,5,6,4,3,7,9,8,1,0};
        
        printArray(arr, 10);
    	printf("start to sort=========
    ");
        bubbleSort(arr, 10);
    	printf("after sorting=========
    ");
        printArray(arr, 10);
        
    	return 0;
    }
    

    运行结果是:

    C:UsersAdministratorDesktopsort>bubbleSort.exe
    2 5 6 4 3 7 9 8 1 0
    start to sort=========
    the inner 1,1 sort: 2 5 6 4 3 7 9 8 1 0
    the inner 1,2 sort: 2 5 6 4 3 7 9 8 1 0
    the inner 1,3 sort: 2 5 4 6 3 7 9 8 1 0
    the inner 1,4 sort: 2 5 4 3 6 7 9 8 1 0
    the inner 1,5 sort: 2 5 4 3 6 7 9 8 1 0
    the inner 1,6 sort: 2 5 4 3 6 7 9 8 1 0
    the inner 1,7 sort: 2 5 4 3 6 7 8 9 1 0
    the inner 1,8 sort: 2 5 4 3 6 7 8 1 9 0
    the inner 1,9 sort: 2 5 4 3 6 7 8 1 0 9
    the 1 sort: 2 5 4 3 6 7 8 1 0 9
    the inner 2,1 sort: 2 5 4 3 6 7 8 1 0 9
    the inner 2,2 sort: 2 4 5 3 6 7 8 1 0 9
    the inner 2,3 sort: 2 4 3 5 6 7 8 1 0 9
    the inner 2,4 sort: 2 4 3 5 6 7 8 1 0 9
    the inner 2,5 sort: 2 4 3 5 6 7 8 1 0 9
    the inner 2,6 sort: 2 4 3 5 6 7 8 1 0 9
    the inner 2,7 sort: 2 4 3 5 6 7 1 8 0 9
    the inner 2,8 sort: 2 4 3 5 6 7 1 0 8 9
    the inner 2,9 sort: 2 4 3 5 6 7 1 0 8 9
    the 2 sort: 2 4 3 5 6 7 1 0 8 9
    the inner 3,1 sort: 2 4 3 5 6 7 1 0 8 9
    the inner 3,2 sort: 2 3 4 5 6 7 1 0 8 9
    the inner 3,3 sort: 2 3 4 5 6 7 1 0 8 9
    the inner 3,4 sort: 2 3 4 5 6 7 1 0 8 9
    the inner 3,5 sort: 2 3 4 5 6 7 1 0 8 9
    the inner 3,6 sort: 2 3 4 5 6 1 7 0 8 9
    the inner 3,7 sort: 2 3 4 5 6 1 0 7 8 9
    the inner 3,8 sort: 2 3 4 5 6 1 0 7 8 9
    the inner 3,9 sort: 2 3 4 5 6 1 0 7 8 9
    the 3 sort: 2 3 4 5 6 1 0 7 8 9
    the inner 4,1 sort: 2 3 4 5 6 1 0 7 8 9
    the inner 4,2 sort: 2 3 4 5 6 1 0 7 8 9
    the inner 4,3 sort: 2 3 4 5 6 1 0 7 8 9
    the inner 4,4 sort: 2 3 4 5 6 1 0 7 8 9
    the inner 4,5 sort: 2 3 4 5 1 6 0 7 8 9
    the inner 4,6 sort: 2 3 4 5 1 0 6 7 8 9
    the inner 4,7 sort: 2 3 4 5 1 0 6 7 8 9
    the inner 4,8 sort: 2 3 4 5 1 0 6 7 8 9
    the inner 4,9 sort: 2 3 4 5 1 0 6 7 8 9
    the 4 sort: 2 3 4 5 1 0 6 7 8 9
    the inner 5,1 sort: 2 3 4 5 1 0 6 7 8 9
    the inner 5,2 sort: 2 3 4 5 1 0 6 7 8 9
    the inner 5,3 sort: 2 3 4 5 1 0 6 7 8 9
    the inner 5,4 sort: 2 3 4 1 5 0 6 7 8 9
    the inner 5,5 sort: 2 3 4 1 0 5 6 7 8 9
    the inner 5,6 sort: 2 3 4 1 0 5 6 7 8 9
    the inner 5,7 sort: 2 3 4 1 0 5 6 7 8 9
    the inner 5,8 sort: 2 3 4 1 0 5 6 7 8 9
    the inner 5,9 sort: 2 3 4 1 0 5 6 7 8 9
    the 5 sort: 2 3 4 1 0 5 6 7 8 9
    the inner 6,1 sort: 2 3 4 1 0 5 6 7 8 9
    the inner 6,2 sort: 2 3 4 1 0 5 6 7 8 9
    the inner 6,3 sort: 2 3 1 4 0 5 6 7 8 9
    the inner 6,4 sort: 2 3 1 0 4 5 6 7 8 9
    the inner 6,5 sort: 2 3 1 0 4 5 6 7 8 9
    the inner 6,6 sort: 2 3 1 0 4 5 6 7 8 9
    the inner 6,7 sort: 2 3 1 0 4 5 6 7 8 9
    the inner 6,8 sort: 2 3 1 0 4 5 6 7 8 9
    the inner 6,9 sort: 2 3 1 0 4 5 6 7 8 9
    the 6 sort: 2 3 1 0 4 5 6 7 8 9
    the inner 7,1 sort: 2 3 1 0 4 5 6 7 8 9
    the inner 7,2 sort: 2 1 3 0 4 5 6 7 8 9
    the inner 7,3 sort: 2 1 0 3 4 5 6 7 8 9
    the inner 7,4 sort: 2 1 0 3 4 5 6 7 8 9
    the inner 7,5 sort: 2 1 0 3 4 5 6 7 8 9
    the inner 7,6 sort: 2 1 0 3 4 5 6 7 8 9
    the inner 7,7 sort: 2 1 0 3 4 5 6 7 8 9
    the inner 7,8 sort: 2 1 0 3 4 5 6 7 8 9
    the inner 7,9 sort: 2 1 0 3 4 5 6 7 8 9
    the 7 sort: 2 1 0 3 4 5 6 7 8 9
    the inner 8,1 sort: 1 2 0 3 4 5 6 7 8 9
    the inner 8,2 sort: 1 0 2 3 4 5 6 7 8 9
    the inner 8,3 sort: 1 0 2 3 4 5 6 7 8 9
    the inner 8,4 sort: 1 0 2 3 4 5 6 7 8 9
    the inner 8,5 sort: 1 0 2 3 4 5 6 7 8 9
    the inner 8,6 sort: 1 0 2 3 4 5 6 7 8 9
    the inner 8,7 sort: 1 0 2 3 4 5 6 7 8 9
    the inner 8,8 sort: 1 0 2 3 4 5 6 7 8 9
    the inner 8,9 sort: 1 0 2 3 4 5 6 7 8 9
    the 8 sort: 1 0 2 3 4 5 6 7 8 9
    the inner 9,1 sort: 0 1 2 3 4 5 6 7 8 9
    the inner 9,2 sort: 0 1 2 3 4 5 6 7 8 9
    the inner 9,3 sort: 0 1 2 3 4 5 6 7 8 9
    the inner 9,4 sort: 0 1 2 3 4 5 6 7 8 9
    the inner 9,5 sort: 0 1 2 3 4 5 6 7 8 9
    the inner 9,6 sort: 0 1 2 3 4 5 6 7 8 9
    the inner 9,7 sort: 0 1 2 3 4 5 6 7 8 9
    the inner 9,8 sort: 0 1 2 3 4 5 6 7 8 9
    the inner 9,9 sort: 0 1 2 3 4 5 6 7 8 9
    the 9 sort: 0 1 2 3 4 5 6 7 8 9
    after sorting=========
    0 1 2 3 4 5 6 7 8 9
    
    C:UsersAdministratorDesktopsort>
  • 相关阅读:
    判断安卓或是IOS
    安卓、IOS内嵌H5的相互通信
    原生js tab选项卡粗略封装
    关于抓包
    vue学习的第一天——vue-router的相关使用
    关于jQuery插件封装的总结
    mvc生成静态页
    C#调用java代码
    汉字转16进制,汉字转10进制
    Model 类型为dynamic或者list<dynamic>
  • 原文地址:https://www.cnblogs.com/CodeWorkerLiMing/p/12007448.html
Copyright © 2011-2022 走看看