有人可以帮我添加一个线性搜索到我的排序数组吗?我希望用户能够输入值,然后显示要显示该值的行。非常感谢提前!
答案 0 :(得分:0)
在排序数组上,您可以添加线性搜索的二进制搜索内容,这要快得多。
线性搜索:
Console.Write("String to search:");
string search = Console.ReadLine();
for (int i = 0; i < TextFile.Length; i++)
{
if (TextFile[i] == search)
{
Console.WriteLine(search + " found at index " + i);
}
}
二进制搜索:
Console.Write("String to search:");
string search = Console.ReadLine();
int min = 0;
int max = TextFile.Length - 1;
while (min <= max)
{
int mid = (min + max) / 2;
if (search == TextFile[mid])
{
Console.WriteLine(search + " found at index " + mid);
break;
}
else if (string.Compare( search, TextFile[mid])<1)
{
max = mid - 1;
}
else
{
min = mid + 1;
}
}
答案 1 :(得分:0)
如果你有排序数组,你最好使用二进制搜索 Array.BinarySearch :.如果您坚持线性搜索,请尝试Array.IndexOf:
var b = 125;
var h = 115;
var rx = 7;
var ry = 4;
var e = 0;
function update() {
setInterval(function() {
e = (e + Math.PI / 360) % (Math.PI * 2);
rotate(e);
}, 10);
var lyd = new Audio("Vedlegg/skoytelyd.mp3");
lyd.play();
}
function rotate(e) {
var px = b + rx * Math.cos(e)*b/2;
var py = h + ry * Math.sin(e)*h/2;
document.getElementById("punkt").style.left = px + "px";
document.getElementById("punkt").style.top = py + "px";
}
</script>
<style>
div {
position: fixed;
}
#sentrum {
background: black;
left: 100px;
top: 50px;
}
#skoyteloper {
position: absolute;
top: 190px;
left: 450px;
width: 60px;
height: 60px;
}
</style>
</head>
<body>
<div id="sentrum"></div>
<img src="Vedlegg/bane.jpg" id="imgBane"></img>
</div>
<div id="punkt">
<img src="Vedlegg/skoyteloper.png" id="skoyteloper"></img>
</div>
<a href="animasjon.html">TILBAKE</a>
</body>