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

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>利用 js 实现 冒泡排序</title>
    </head>
    <!--使用 js 代码实现冒泡排序-->
    <script type="text/javascript">
        //声明两个数组 :不带 var 关键字,默认为全局变量
        arrayInt = new Array(38,55,99,66,88,36,6,8);
        arrayString = new Array("Bob","emily","hanna","lisa","cindy","Alice");
    //排序前的输出方法
    function SortIS(){    
        //输出排序前的数组内容
        document.write("排序前的内容是(数字):"+arrayInt);
        document.write("<br/>排序前的内容是(英文名):"+arrayString+"<br/>");
        //排序算法(降序)
        document.write("<br/><hr/>排序后的内容是(数字):");    
        var temp ;        //声明一个中间变量
        var i=0;
        for(i = 0;i<arrayInt.length;i++){
            for(var j = i;j<arrayInt.length;j++){            
                if(arrayInt[i]<arrayInt[j]){
                    temp = arrayInt[i];
                    arrayInt[i] = arrayInt[j];
                    arrayInt[j] = temp;
                }        
            }
        }
        //输出排序后的结果
        document.write(arrayInt);
        //使用 js 中的升序为 arrayString 排序
        document.write("<br/>排序后的内容是(英文名):"+arrayString.sort());
    }
    </script>
    <body bgcolor="#669933">
        <h1 align="center"><i>测试页面不太好看,请不要见怪哦!</i></h1>
        <input onclick="SortIS()" type="button" value="按我排序!" style=" 150px; color:#339900;"/>
    </body>
    </html>

  • 相关阅读:
    Linux虚拟机的安装(使用Centos6.3)
    【转载】接口测试用例的设计原则
    Oracle PLSQL游标、游标变量的使用
    利用shell脚本将Oracle服务器中数据定时增量刷新到ftp服务器中
    源码安装rlwrap 0.43(为了方便使用linux下的sqlplus)
    Oracle自定义脱敏函数
    Oracle分析函数FIRST_VALUE、LAST_VALUE
    MYSQL性能测试工具SYSBENCH
    OEL7.6源码安装MYSQL5.7
    OEL7.6安装Oracle Database 19C(VERSION 19.3.0.0)
  • 原文地址:https://www.cnblogs.com/rick168/p/3084339.html
Copyright © 2011-2022 走看看