简体   繁体   English

高效搜索表格式结构的行的算法

[英]Efficient algorithm for searching a row which a tabular structure

在表格结构中搜索行时,哪种算法最有效。

It completely depends on the way the tabular structure is stored in the memory: 这完全取决于表格结构在内存中的存储方式:

1.)If the data in the structure is already sorted in one particular oder(ascending or descending): Binary Search would be the most efficient. 1.)如果结构中的数据已经按一种特定的顺序(升序或降序)进行了排序:二进制搜索将是最有效的。

Lets assume they are stored in ascending order already then the required steps are: 让我们假设它们已经以升序存储,那么所需的步骤是:

i)Take the data of the variable using which the data is sorted and compare it first to the first element(fir) in the table,if yes,abort. i)提取用于对数据进行排序的变量的数据,然后将其首先与表中的第一个元素(冷杉)进行比较,如果是,则中止。

ii)Compare it to the last element(end),if yes,abort. ii)比较最后一个元素(结束),如果是,则中止。

iii)If not,compare it to the middle(mid) element of the table using which the table is sorted and check for a relation whether it is equal,greater or lesser then the middle element. iii)如果不是,则将其与用于对表进行排序的表的middle(mid)元素进行比较,并检查与中间元素是否相等,更大或更小的关系。 Conditions with middle element: 具有中间元素的条件:

a)If equals,abort. a)如果相等,则中止。

b)If greater,use the element next to mid(mid+1) as the first element(fir) and calculate the new mid by using the formula mid=fir+end/2 and repeat the first three steps. b)如果更大,则使用mid(mid + 1)旁边的元素作为第一个元素(fir),并使用公式mid = fir + end / 2计算新的mid,然后重复前三个步骤。

c)If lesser,use the element before mid as the end and calculate new mid by using mid=fir+end/2,and thus repeat the first three steps. c)如果较小,请使用mid之前的元素作为结尾,并使用mid = fir + end / 2计算新的mid,然后重复前三步。

2.)If the data is not sorted sorted already,Linear Search is the way to go. 2)如果尚未对数据进行排序,则可以使用线性搜索。 Algorithm: i.)Compare the variable to the first element.If yes,abort. 算法:i。)将变量与第一个元素进行比较。如果是,则中止。 ii)If no,compare the variable to the next element,If yes,abort.If no,keep on repeating the procedure until the element is found. ii)如果否,则将变量与下一个元素进行比较,如果是,则中止。如果否,则继续重复该过程直到找到该元素。

Hope it helps! 希望能帮助到你!

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

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