JavaScript算法实现排序

时间:2023-09-08 18:23:56
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>IFE JavaScript Task 01</title>
</head>
<body> <h3>污染城市列表</h3>
<ul id="aqi-list">
<!--
<li>第一名:福州(样例),10</li>
<li>第二名:福州(样例),10</li> -->
</ul> <script type="text/javascript"> var apiDate = [
["北京", 90],
["上海", 50],
["福州", 10],
["广州", 50],
["成都", 90],
["西安", 100]
];
var j=0,i;
while(j<5)
{
i=j+1;
while(i<6)
{
if(apiDate[j][1]<apiDate[i][1])
{var temp=apiDate[j];apiDate[j]=apiDate[i];apiDate[i]=temp;}i++;
};j++;
}; var i=0;
while(i<6)
{
if(apiDate[i][1]>60)
{i++;
var para=document.createElement("li");para.innerHTML="第"+i+"名"+apiDate[i-1];i--;
var father=document.getElementById("aqi-list");
father.appendChild(para); }
i++; /*在注释下方编写代码
遍历读取aqiData中各个城市的数据
将空气质量指数大于60的城市显示到aqi-list的列表中*/ }; </script>
</body>
</html>