• 冒泡排序(交换)


    JavaScript

    function bubbleSort(ary) {
        var i, j, temp, len = ary.length;
        for(i=1; i<len; i++) {
            for(j=len-1; j>=i; j--) {
                temp = ary[j];
                if(temp < ary[j-1]) {
                    ary[j] = ary[j-1];
                    ary[j-1] = temp;
                }
            }
        }
        return ary;
    }
    var ary = [5,4,3,2,1];
    console.log(bubbleSort(ary));
    

     

    Java

    public class Test {
    	
    	public static void bubbleSort(int[] ary) {
    		int i, j, temp;
    		int len = ary.length;
    		
    		for(i=1; i<len; i++) {
    			for(j=len-1; j>=i; j--) {
    				temp = ary[j];
    				if(ary[j] < ary[j-1]) {
    					ary[j] = ary[j-1];
    					ary[j-1] = temp;
    				}
    			}
    		}
    		
    	}
    	
    	public static void main(String[] args) {
    		int[] ary = {5,4,3,2,1};
    		Test.bubbleSort(ary);
    		for(int it : ary) {
    			System.out.println(it);
    		}
    
    	}	
    }
    

     

    #include <stdio.h>
    
    void bubbleSort(int ary[], int len) {
    	int i, j, temp;
    	
    	for(i=1; i<len; i++) {
    		for(j=len-1; j>=i; j--) {
    			temp = ary[j];
    			ary[j] = ary[j-1];
    			ary[j-1] = temp;
    		}
    	}
    }
    
    main() {
    	int i;
    	int ary[]  = {5,4,3,2,1};
    	bubbleSort(ary, 5);
    	for(i=0; i<5; i++) {
    		printf("%d", ary[i]);
    	}
    	
    }
    

      

  • 相关阅读:
    sshpass连接主机以及执行命令
    elk集群配置并破解x-pack
    socket实现简单通信会话
    docker容器跨宿主机通信
    docker运行wordpress
    centos7 利用docker运行nginx项目
    docker容器基础命令
    docker镜像基础命令
    vue中使用延时加载
    less
  • 原文地址:https://www.cnblogs.com/snandy/p/2205899.html
Copyright © 2020-2023  润新知