• 改进冒泡排序


    Java版本

    /*************************************************************************
        > File Name: BubbleSort.java
        > Author: lxm
        > Created Time: 2016年04月27日 星期三 19时24分50秒
     ************************************************************************/
    
    public class BubbleSort
    {
        public static final int N = 10;
        public static void main(String[] args)
        {
            int[] a = {6,1,2,7,9,3,4,5,10,8};
            bubbleSort(a);
            printArray(a);
        }
    
        public static void printArray(int[] a)
        {
            for(int item : a)
            {
                System.out.printf("%d	",item);
            }
            System.out.println();
        }
    
        public static void bubbleSort(int[] a)
        {
            int len = a.length;
            boolean flag = true;
            for(int i=0;i<len-1 && flag;++i)
            {
                flag = false;
                for(int j=0;j<len-i-1;++j)
                {
                    if(a[j]>a[j+1])
                    {
                        swap(a,j,j+1);
                        flag = true;
                    }
                }
            }
        }
    
        public static void swap(int[] a,int m, int n)
        {
            a[m] ^= a[n];
            a[n] ^= a[m];
            a[m] ^= a[n];
    
        }
    }
    
    

    C版本

    /*************************************************************************
        > File Name: bubbleSort.c
        > Author: lxm
        > Created Time: 2016年04月27日 星期三 19时10分36秒
     ************************************************************************/
    
    #include<stdio.h>
    #define N 10
    void bubbleSort(int* a, int n);
    void printArray(int* a, int n);
    void swap(int *a,int *b);
    int main(void)
    {
        int a[] = {6,1,2,7,9,3,4,5,10,8};
        bubbleSort(a,N);
        printArray(a,N);
    }
    void bubbleSort(int* a, int n)
    {
        int i,j;
        int flag = 1;
        for(i=0;flag==1 && i<n-1;i++)
        {
            flag = 0;
            for(j=0;j<n-1-i;++j)
            {
                if(a[j]>a[j+1])
                {
                    swap(&a[j],&a[j+1]);
                    flag = 1;
    
    
                }
            }
        }
    
    }
    void printArray(int* a, int n)
    {
        int i;
        for(i=0;i<n;++i)
        {
            printf("%d	",a[i]);
        }
        printf("
    ");
    }
    
    void swap(int *a,int *b)
    {
        *a ^= *b;
        *b ^= *a;
        *a ^= *b;
    }
  • 相关阅读:
    常用head标签
    php自定义配置文件简单写法
    sublimeText常用插件
    addslashes,htmlspecialchars,htmlentities转换或者转义php特殊字符防止xss攻击以及sql注入
    服务器安装node全教程
    Sublime Text 3 Build 3176 License
    [转]Zend Studio 文件头和方法注释设置
    给php代码添加规范的注释phpDocumentor
    [转]php 在各种web服务器的运行模式
    .htaccess文件url重写小记
  • 原文地址:https://www.cnblogs.com/yldf/p/6249910.html
Copyright © 2020-2023  润新知