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>

  • 相关阅读:
    HDU 5937 Equation
    HDU 5936 Difference
    hdu 4348 To the moon
    SPOJ QTREE Query on a tree
    HDU 3966 Aragorn's Story
    Codeforces 733F Drivers Dissatisfaction
    道良心题
    dp小总结
    数据结构
    数学相关(偏数学向题目的集中地)
  • 原文地址:https://www.cnblogs.com/rick168/p/3084339.html
Copyright © 2011-2022 走看看