• javascript: Bubble Sort


    1. // Sorting Algorithms int JavaScript
    2. /**
    3. * file Sort.js
    4. * 1. Bubble Sort冒泡排序法
    5. */
    6. function BubbleSort(arry, nszie)
    7. {
    8. var i, j, temp;
    9. var swapped;
    10. for (i = 0; i < nszie - 1; i++)
    11. {
    12. swapped = false;
    13. for (j = 0; j < nszie - i - 1; j++)
    14. {
    15. if (arry[j] > arry[j + 1])
    16. {
    17. // Swap arry[j] and arry[j+1]
    18. temp = arry[j];
    19. arry[j] = arry[j + 1];
    20. arry[j + 1] = temp;
    21. swapped = true;
    22. }
    23. }
    24. // IF no two elements were
    25. // swapped by inner loop, then break
    26. if (swapped == false)
    27. break;
    28. }
    29. return arry
    30. }
    31. function stringArray(arry, size)
    32. {
    33. //console.log(arry);
    34. var myStr=new Array();
    35. var getstr="";
    36. /**/
    37. var i;
    38. for (i = 0; i < size; i++)
    39. {
    40. getstr=getstr+arry[i].toString() + "
      "
      ;
    41. myStr[i]=arry[i].toString();
    42. console.log(myStr[i]);
    43. }
    44. return getstr;//myStr.join(" ");
    45. }
    46. /**
    47. */
    48. function printArray(arry, size)
    49. {
    50. var getstr="";
    51. var i;
    52. for (i = 0; i < size; i++)
    53. {
    54. console.log(arry[i] + " ");
    55. getstr=getstr+arry[i]+" ";
    56. }
    57. return getstr;
    58. }

    调用:

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>成长开始,geovindu,涂聚文,Geovin Dutitle>
    8. <meta name="Description" content="geovindu"/>
    9. <meta name="Keywords" content="geovindu"/>
    10. <meta name="author" content="geovindu"/>
    11. <script src="js/jquery-3.6.0.js">script>
    12. <script src="js/SortAlgorithm/Sort.js">script>
    13. <script type="text/javascript">
    14. $(document).ready(function () {
    15. // 1. Bubble Sort冒泡排序法
    16. var arry = [ 64, 34, 25, 112, 220, 11, 90 ];
    17. var nzie= arry.length;
    18. var geovindu=BubbleSort(arry, nzie);
    19. console.log(geovindu)
    20. var myStr=new Array();
    21. var i;
    22. for (i = 0; i < nzie; i++)
    23. {
    24. //getstr=getstr+arry[i].toString() + "
      ";
    25. myStr[i]=geovindu[i].toString();
    26. console.log(geovindu[i].toString());
    27. }
    28. console.log(myStr.join("
      "
      ));
    29. //stringArray(arry,nsize)
    30. console.log("Bubble Sorted array: ");
    31. var getstr=printArray(arry, nzie);
    32. console.log("str:"+getstr)
    33. $("#txtgeovindu").html(getstr);
    34. txtgeovindu.innerHTML = getstr;//stringArray(geovindu,nsize);
    35. $("#geovindu").html("1.泡冒泡排序Bubble Sorted:
      "
      +myStr.join("
      "
      ));
    36. });
    37. script>
    38. head>
    39. <body>
    40. <textarea id="txtgeovindu" class="geovindu" name="" cols="30" rows="10">textarea>
    41. <div id="geovindu">div>
    42. body>
    43. html>

    输出:

  • 相关阅读:
    为什么你的敏捷总是不成功?
    libevent
    垃圾收集器G1&ZGC
    libuv初学者学习笔记
    什么是FSDM的九大数据概念
    点餐小程序实战教程03-用户注册
    linux shell脚本修改ini配置文件[session]下的键值
    大二下个人发展小结
    [附源码]Python计算机毕业设计Django宁财二手物品交易网站
    AI 与大模型引新安全威胁?亚马逊云科技与领创集团的探索和实践
  • 原文地址:https://blog.csdn.net/geovindu/article/details/133470037