简体   繁体   English

气泡排序简单排行榜

[英]bubble sort for simple leaderboard

I am creating a simple video game leader board which will rank the user score against pre-set scores which will place their rank from highest to lowest depending. 我正在创建一个简单的视频游戏排行榜,该排行榜将用户分数与预设分数进行排名,从而将其排名从最高到最低。

<html>
  <!Foundation Page for building our Javascript programs>
  <head>
    <title>The Foundation Page </title>
    <script type="text/javascript">
      function leaderboard()
      {
        var leaderboardarray = new Array (5);
        var n, temp;
        ok=false;
        var score 
        var rank

        score = 150

        leaderboardarray[1] = 50;
        leaderboardarray[2] = 60;
        leaderboardarray[3] = 180;
        leaderboardarray[4] = 120;
        leaderboardarray[5] = score;

        while(!ok)
        {
          ok = true
          for (n=1; n<=5; n=n+1)
          { 
            if (leaderboardarray [n]<leaderboardarray [n-1])
            {
              leaderboardarray [n] = leaderboardarray [n-1];
              ok = false;
            }
          } 
        }

        for (n=5; n>=1; n=n-1) 
          document.write (leaderboardarray [n] + "<br>");
      }

    </script>
  </head>head>
  <body BGCOLOR="WHITE">
    <h2>The Foundation Page </h2>
    <hr>
    <script LANGUAGE="Javascript"> leaderboard() </script>

  </body>
</html>

Its outputting the arrays as normal, but I am stuck on how the array outputs a value from highest to lowest. 它正常输出数组,但是我对数组如何输出从最高到最低的值感到困惑。 When I place a value higher than any other value after it, it will only produce the same value. 当我在其后放置一个高于任何其他值的值时,它只会产生相同的值。 When I change one of the value to Would anyone suggest on what I should do to do so would be much appreciated. 当我将其中一个值更改为时,如果有人建议我应该这样做,将不胜感激。 I am still new to programming so sorry if I am doing anything silly. 我对编程还是很陌生,所以如果做任何愚蠢的事情,请对不起。 Thank you! 谢谢!

You should use Array.sort. 您应该使用Array.sort。

However, the problem with the code you posted is, you aren't actually swapping in your bubble sort. 但是,您发布的代码存在的问题是,您实际上并没有交换气泡排序。 You are just overwriting the smaller value with the larger one. 您只是用较大的值覆盖较小的值。

 function bubbleSort(arr) { var n = arr.length, swapped, tmp; do { swapped = false; for (var i = 1; i < n; i++) { if (arr[i-1] < arr[i]) { tmp = arr[i]; arr[i] = arr[i-1]; arr[i-1] = tmp; swapped = true; } } } while (swapped && n--) } a = [50, 60, 70, 80, 150, 120] bubbleSort(a); console.log(a); // [150, 120, 80, 70, 60, 50] 

Since you're using Javascript without prototype or jQuery, have you tried replacing your while statement with something like this: 由于您使用的Javascript没有原型或jQuery,您是否尝试过使用以下代码替换while语句:

leaderboardarray.sort(function(a, b){return b-a});

There is a great write-up of the Javascript sort function at w3schools. w3schools上有很多有关Javascript排序功能的文章。

<HTML>
<!--#Foundation Page for building our Javascript programs#-->
<HEAD>
<TITLE>The Foundation Page </TITLE>
<!--fixed this-->
<SCRIPT type="text/javascript">
    function leaderboard()
    {

            var n, temp;
            ok=false;
            var score 
            var rank

            var score = 150

            var leaderboardarray = new Array(5);
                leaderboardarray[0] = 50;
                leaderboardarray[1] = 60;
                leaderboardarray[2] = 180;
                leaderboardarray[3] = 120;
                leaderboardarray[4] = score;

            leaderboardarray.sort(function(a, b){return b-a});
            var myContent = '';
            for (var n=0;n<5;n++) 
                myContent += leaderboardarray[n] + "<br>";

            document.getElementById("leaderBoard").innerHTML = myContent;
    }

    //call the function here


</SCRIPT>
</HEAD>
<!--#Using CSS#-->
<BODY style="background-color:#FFF;">
        <H2>The Foundation Page </H2>
        <HR>
        <!--#This is a placeholder#-->
        <div id="leaderBoard"><script type="text/javascript">leaderboard();</script></div>

</BODY>
</HTML>

Few things about your code that needed to be addressed. 关于您的代码的一些事情需要解决。

  • You had two head tags. 您有两个头部标签。
  • You were sorting your array, then showing the output with a decrementing for statement. 您正在对数组进行排序,然后以减量for语句显示输出。 On this I'm sorting the array in descending order and then showing ascending output. 在此我按降序对数组进行排序,然后显示升序输出。
  • I also have started arrays at 0 (where they want to be), this allows for less math in the for statements. 我还从0(它们想要的位置)开始数组,这使得for语句中的数学运算更少。
  • I've replaced the bgcolor HTML tag with CSS 我已经用CSS替换了bgcolor HTML标记
  • Updated the script tag to use type instead of language . 更新了脚本标记以使用type而不是language
  • Rather than using document.write which can cause problems , I'm replacing the content of a placeholder div. 而不是使用document.write会导致问题 ,我要替换占位符div的内容。

Here is a fiddle. 这是一个小提琴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM