【问题标题】:"selection sort" using javascript?“选择排序”使用javascript?
【发布时间】:2019-09-23 13:49:20
【问题描述】:

如何获取数组中最小元素的位置,并使用 javascript 将该值与“选择排序”的相邻值交换?

我的 HTML 是

<!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=utf-8" />
    <title>Untitled Document</title>
    <script type="text/javascript" src="js/script.js"></script>
    <script src="js/jquery-latest.min.js"></script>
    <link type="text/css" href="style.css" rel="stylesheet" />
</head>

<body>
    <form onsubmit="store(this); return false">
        1:<input type="text" id="one" /><br />
        2:<input type="text" id="two" /><br />
        3:<input type="text" id="three" /><br />
        4:<input type="text" id="four" /><br />
        5:<input type="text" id="five" /><br />
        6:<input type="text" id="six" /><br />
        <input type="button" value="Click Me" onclick="getInput(); output();" />
    </form><br />
    <p id="in">Input</p><br />
    <div id="qstnDiv"></div><br />
    <p id="out">Output</p><br />
    <div id="anwrDiv"></div>

</body>
</html>

我的 Javascript 是

var list = [];
Math.max.apply(Math,list);//maximum
Math.min.apply(Math,list); //minimum 


function getInput() {
    var str_one = document.getElementById("one").value;
    var str_two = document.getElementById("two").value;
    var str_three = document.getElementById("three").value;
    var str_four = document.getElementById("four").value;
    var str_five = document.getElementById("five").value;
    var str_six = document.getElementById("six").value;
    list.push(str_one,str_two,str_three,str_four,str_five,str_six);
    $('#qstnDiv').html(list+',');
}

function output(){

}

我想使用选择排序对文本框中的数字进行排序,所以请帮助我

【问题讨论】:

  • 为什么需要选择排序? Javascript 已经提供了一个足够简单的.sort() 函数
  • 其实我想一步一步做排序功能,这就是为什么...请帮我找到解决方案。
  • 你能把选择排序的 Javascript 代码发给我吗?
  • bhb 链接了一个选择排序的伪代码,现在用JS重写应该不是很难。

标签: javascript html sorting


【解决方案1】:

有一个特定的功能。

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

这是上升的。如果你改变“a”和“b”的顺序,它就会变成disdisending。 如果你需要访问一个对象,你必须编写

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

【讨论】:

    【解决方案2】:

    非常明确的选择排序!

    function selectionSort(arr){
        for (let i = 0; i < arr.length; i++){
            let indexOfMin = i // set index of min to the 
            let j = i // set the index to run the while loop for calculating min
            let min = arr[i] // set the initial value of minimum
    
            // find the minimum from i to end of array
            while (j < arr.length){
                if (arr[j] < min){
                    min = arr[j]
                    indexOfMin = j
                }
                j++
            }
    
            if (indexOfMin !== i){ // only swap if the index of minimum and curr item is different
                let tmp = arr[i]
                arr[i] = arr[indexOfMin]
                arr[indexOfMin] = tmp
                console.log(arr)
            }
        }
        return arr
    }
    
    sortedList = selectionSort([5,3,4,1,-3,2])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      相关资源
      最近更新 更多