Javascript中的搜索和冒泡排序数组
Search and Bubble Sort Array in Javascript
好吧,所以我在课堂上的这个实验室真的很麻烦。问题是:
初始化:随机初始化一个大小为200、整数值在0到100之间的列表。第1部分:搜索您要实现一个函数,该函数在列表中搜索某个值的出现。它不应该依赖于正在进行预排序的列表。
搜索章节规范注释INPUT:list,value初始化的列表计算:
Loop over all elements in list.
If current element equals value, store as index.
If value not found, ensure index is -1.
返回:如果找不到值,则索引-1
Prompt the user once for an element (an integer from 0 to 100) to search for.
Call your search function with the number to search for and the list.
Display whether the number was found, and if found, a location where it can be found within the list.
第2部分:排序您要实现一个函数,该函数按升序(0,1,…(对列表进行排序。您不允许使用JavaScript的sort((方法。有很多方法可以对列表进行排序,您可以实现任何您认为合适的方法,只要它按升序排序即可。下面介绍了Bubble Sort,这是最直接的排序方法之一。
排序章节规范注释INPUT:list初始化的列表其他变量:交换n表示是否发生交换。在列表中搜索多远。计算:
Set n to size of list - 1.
Set swap to FALSE.
Loop over element 0 through element n in the list.
If current element > next element
Swap current element and next element.
Set swap to TRUE.
If swap is TRUE, repeat from step 2. n -= 1.
If swap is FALSE, return the now sorted list.
Gradually sorts a list.
第n个项目放置正确。返回:列出
Call your sort function for your list. You are not permitted to call Javascript's sort() method.
Display the (sorted) list.
我不是要你做作业,但你能给我指一个正确的方向吗?我想好了如何进行冒泡排序,但搜索部分是我遇到的主要问题。
function search(array, value)
{
for (var i = 0; i < array.length; i++)
if (array[i] === value)
return i;
return -1;
}
对于Bubble Sort的实现,请阅读本文。
此外,您可以使用此解决方案:
function search(array, value)
{
return array.indexOf(value);
}
相关文章:
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- 在数组的 2/3 上调用自身的排序算法
- 数组值的排序以匹配另一个数组ES6
- 按不同项目对对象数组进行排序
- 根据Javascript中的字符串值对数组的数组进行排序
- 在不同的javascript数组中对json响应进行排序
- 对对象数组中的数组进行排序
- 按特定键对对象数组进行排序
- 使用jquery根据数组对表行进行排序
- 尝试对对象数组进行排序但拼接不是一个函数
- 对javascript中某些键的对象数组进行排序
- Javascript按数字顺序排序()数组
- Javascript排序索引链接数组
- 在jquery中对时间数组进行排序
- 如何在具有相同 ID 的 Javascript 中按排序函数对关联数组进行排序
- Javascript中的多维数组排序索引问题
- 如何在mongodb中根据数组内容的差异进行排序
- 在Javascript中对具有特定异常的对象数组进行排序
- 数组数据排序类似mysql查询
- 在小于O(n)时间内找到(排序)数组中的重复元素