简体   繁体   中英

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.

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.

ii)Compare it to the last element(end),if yes,abort.

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. Conditions with middle element:

a)If equals,abort.

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.

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.

2.)If the data is not sorted sorted already,Linear Search is the way to go. Algorithm: i.)Compare the variable to the first element.If yes,abort. 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.

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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