• javascript: Sorting Algorithms


    1. // Sorting Algorithms int JavaScript https://www.geeksforgeeks.org/sorting-algorithms/
    2. /**
    3. * file Sort.js
    4. * 1. Bubble Sort冒泡排序法
    5. * @param arry
    6. * @param nszie
    7. */
    8. function BubbleSort(arry, nszie)
    9. {
    10. var i, j, temp;
    11. var swapped;
    12. for (i = 0; i < nszie - 1; i++)
    13. {
    14. swapped = false;
    15. for (j = 0; j < nszie - i - 1; j++)
    16. {
    17. if (arry[j] > arry[j + 1])
    18. {
    19. // Swap arry[j] and arry[j+1]
    20. temp = arry[j];
    21. arry[j] = arry[j + 1];
    22. arry[j + 1] = temp;
    23. swapped = true;
    24. }
    25. }
    26. // IF no two elements were
    27. // swapped by inner loop, then break
    28. if (swapped == false)
    29. break;
    30. }
    31. return arry
    32. }
    33. /**
    34. *
    35. */
    36. function swap(arry,xp, yp)
    37. {
    38. var temp = arry[xp];
    39. arry[xp] = arry[yp];
    40. arry[yp] = temp;
    41. }
    42. /**
    43. * 2 选择排序 Selection Sort
    44. * @param arry
    45. * @param nszie
    46. */
    47. function SelectionSort(arry, nsize)
    48. {
    49. var i, j, min_idx;
    50. // One by one move boundary of unsorted subarray
    51. for (i = 0; i < nsize-1; i++)
    52. {
    53. // Find the minimum element in unsorted array
    54. min_idx = i;
    55. for (j = i + 1; j < nsize; j++)
    56. if (arry[j] < arry[min_idx])
    57. min_idx = j;
    58. // Swap the found minimum element with the first element
    59. swap(arry,min_idx, i);
    60. }
    61. return arry;
    62. }
    63. /**
    64. * 3 插入排序 Insertion Sort
    65. * @param arry
    66. * @param nszie
    67. */
    68. function InsertionSort(arry, nsize)
    69. {
    70. let i, key, j;
    71. for (i = 1; i < nsize; i++)
    72. {
    73. key = arry[i];
    74. j = i - 1;
    75. /* Move elements of arr[0..i-1], that are
    76. greater than key, to one position ahead
    77. of their current position */
    78. while (j >= 0 && arry[j] > key)
    79. {
    80. arry[j + 1] = arry[j];
    81. j = j - 1;
    82. }
    83. arry[j + 1] = key;
    84. }
    85. return arry;
    86. }
    87. /**
    88. *
    89. */
    90. function stringArray(arry, size)
    91. {
    92. var myStr=new Array();
    93. var i;
    94. for (i = 0; i < size; i++)
    95. {
    96. //getstr=getstr+arry[i].toString() + "
      ";
    97. myStr[i]=arry[i].toString();
    98. console.log(arry[i].toString());
    99. }
    100. console.log(myStr.join("
      "
      ));
    101. return myStr.join("
      "
      );
    102. //console.log(arry);
    103. /* var myStr=new Array();
    104. var getstr="";
    105. */
    106. /*
    107. var i;
    108. for (i = 0; i < size; i++)
    109. {
    110. getstr=getstr+arry[i] + " ";
    111. myStr[i]=arry[i].toString();
    112. console.log(myStr[i]);
    113. }
    114. return getstr;//myStr.join(" ");*/
    115. }
    116. /**
    117. */
    118. function printArray(arry, size)
    119. {
    120. var getstr="";
    121. var i;
    122. for (i = 0; i < size; i++)
    123. {
    124. console.log(arry[i] + " ");
    125. getstr=getstr+arry[i]+" ";
    126. }
    127. return getstr;
    128. }

    调用:

    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 duselect=SelectionSort(arry,nzie);
    19. console.log(duselect)
    20. var geovindu=BubbleSort(arry, nzie);
    21. console.log(geovindu);
    22. var inserttdu=InsertionSort(arry,nzie);
    23. var myStr=new Array();
    24. var i;
    25. for (i = 0; i < nzie; i++)
    26. {
    27. //getstr=getstr+arry[i].toString() + "
      ";
    28. myStr[i]=geovindu[i].toString();
    29. console.log(geovindu[i].toString());
    30. }
    31. console.log(myStr.join("
      "
      ));
    32. var du=stringArray(arry,nzie);
    33. var du2=stringArray(duselect,nzie);
    34. var du3=stringArray(inserttdu,nzie);
    35. console.log(du);
    36. console.log("Bubble Sorted array: ");
    37. var getstr=printArray(arry, nzie);
    38. console.log("str:"+getstr)
    39. $("#txtgeovindu").html(getstr);
    40. txtgeovindu.innerHTML = getstr;//stringArray(geovindu,nsize);
    41. $("#geovindu").html("1.泡冒泡排序Bubble Sorted:
      "
      +myStr.join("
      "
      ));
    42. $("#geovindu2").html(du);
    43. $("#geovindu3").html("2.选择排序Selection Sorted:
      "
      +du2);
    44. $("#geovindu4").html("3.插入排序Insertion Sorted:
      "
      +du3);
    45. });
    46. script>
    47. head>
    48. <body>
    49. <textarea id="txtgeovindu" class="geovindu" name="" cols="30" rows="10">textarea>
    50. <div id="geovindu">div>
    51. <div id="geovindu2">div>
    52. <div id="geovindu3">div>
    53. <div id="geovindu4">div>
    54. body>
    55. html>

  • 相关阅读:
    spring boot 实现一个 禁止重复请求
    6、设计模式之桥接模式
    靠着数套的Java刷题PDF,成功“混进”腾讯T3
    Linux多线程(读者写者问题与自旋锁)
    CCF领航计划第一期:英文论文写作
    解决注册Kaggle出现的“Captcha must be filled out”问题
    为什么放弃npm转向yarn了
    【面试题精讲】MySQL中覆盖索引是什么
    关于mysql存储过程中的拼接Sql语句写法
    Nodejs初体验
  • 原文地址:https://blog.csdn.net/geovindu/article/details/133486734