An Efficient Cursor Search Algorithm

Table-based searching method is always used to improve the operating speed in embedded system measurement applications. Efficiency of the searching is a key factor in improving system performance. Based on the analysis of traditional search algorithms and characteristics of measured physical sensor signals, a simple and efficient cursor search algorithm is proposed, and is compared with traditional search algorithms. The simulation experiments were made, and the results proved that the cursor search algorithm, which the time complexity is ) 1 (  , is superior to the traditional search algorithms. The cursor search algorithm is suitable for sensor signal measurement of embedded system applications.


Introduction
In terms of the measurement of physical quantities, the efficiency of the table searching method affects the performance of system.Because of low efficiency or other reasons, the traditional search algorithms are not applicable.
As an information processing system embedded in particular object systems, embedded system is often closely related to specific physical processes (Peter, 2011).Processing time and cooperativity are main technical difficulties (Michael & Massa, 2006).As cyber-physical system (CPS), embedded system is an integration of computation with physical processes.It monitors and controls the physical processes, usually with feedback loops where physical processes affect computations and vice versa (Edward & Sanjit, 2011).
Typically, the relationship of the detection module's outputs and the measured physical quantities are non-linear.There are two main reasons contributing.One is that the structural principle of the sensor is non-linear.The other is the use of the non-linear circuits (George, 1989).Some circuits are used to solve the non-linear problems.But, this means high costs, complexity, debugging difficult, low accuracy and poor interoperability.A better solution is linearization during the processing of the programs by the microprocessor (George, 1989), which has the following advantages: a) Simplifying the hardware structure, cost and complexity b) Improving the linearization precision c) Being versatile and producing different linearization correction features Because of the nonlinearity of the sensor signals, the conversion formulas are very complicate.It's not suitable for embedded systems with limited resources.The common way is to quantify the theoretical formula to extract the standard data conversion table.And then, search the table to get the converted value (George, 1989).What needs to be done is to establish the conversion table between measured values and actual values, and find a certain search algorithm.
Linearization of the measured value and getting the actual value can be regarded as a process of searching table.For embedded system, the efficiency of the system depends on the speed of the search process.A simple, efficient searching algorithm becomes the focus of system design.
In computer science, there are several classical search algorithms.Linear search algorithm is the simplest one and its time complexity is ) (n O ."Begin at the beginning, and go on till you find the right key; then stop".This sequential procedure is the obvious way to search, and many of the more intricate algorithms are based on it.In later will see, the sequential search involves some very interesting ideas, in spite of its simplicity (Knuth, 1999).
For an ordered table, a binary search algorithm is particularly rapid.The probe tells which half of the table should be searched next, and the same procedure can be used again, comparing key value to the middle key of the selected half, etc.After at most N lg comparisons, the algorithm will find the key value or it will establish that it is not present (Knuth, 1999).Therefore, its time complexity is ) (log 2 N  . Hashing search is to avoid all the rummaging around by doing some arithmetical calculation on key value, computing a function h(k) that is the location of k.The time complexity of searching on a well-constructed hashing table is ) 1 ( O .But there are two issues required to address: a good hash function and a method for resolving hash conflicts (Thomas, 2001).
In this paper, based on the non-linear and time continuity of the sensor signals in embedded system, we propose a cursor search algorithm with simple, appropriate and efficient characteristics.In this field, its time complexity is ) This paper is organized as follows.In Section 2, we focus on research works associated with the search algorithms in computer science.Characteristics of embedded systems and sensor signals are described in the Section 3. In Section 4 we analyze the four kind of traditional search algorithms and summarize some of their characteristics.In Section 5 we present and elaborate the cursor search algorithm and analyze its average cost of searching.We compare it with traditional search algorithms through the simulation in Section 6. Section 7 provides concluding remarks.

Related Works
During the pre-computer era, many books of logarithm tables, trigonometry tables, etc., were compiled, so that mathematical calculations could be replaced by searching.With the development of calculating machine and random access memory, searching tables or information storage and retrieval issues have increasingly become an important aspect in computer science.
In the 50's, many computer scholar's have clearly represented the traditional search algorithm.The first surveys of the searching problem were published by Dumey (1956, pp. 6-9); Peterson (1957, pp. 130-146);Booth (1958, pp. 159-164);Douglas (1959, pp. 1-9).More extensive treatments were given later by Kenneth (1962, pp. 133-158), and by Werner (1963, pp. 86-111).During the early 1960s, a number of interesting new search procedures based on tree structures were introduced (Knuth, 1999).Now, research about search is still actively continuing.Its research and application have been gradually expanding to the most fields of computer science, such as the operating systems (Nicolescu, Ignat, Savaria, & Nicolescu, 2006), network communications (Behnamian & Fatemi, 2012), database systems, artificial intelligence (Balwinder et al., 2012), image processing, information security, etc.However, in the area of sensor signal detection in embedded systems, no one analyze its characteristics and propose an applicable search algorithm.This paper will do these and present a new and useful cursor search algorithm for sensor signal detection.

Measurement in Embedded System
Embedded system with a principal role of interacting with the physical world must, of necessity, acquire some properties of the physical world.Quantities of the physical world tend to have two significant characteristics: continuity and locality.
The science of computation has systematically abstracted away the physical world.Embedded systems, however, engage the physical world.Time, concurrency, liveness, robustness, continuum, reactivity, and resource management must be remarried to computation (Edward, 2002).Time constraints: Almost every task is associated with some time constraints (Vijaykrishnan & Yuan, 2006).One very common form of time constraints is deadlines associated with tasks (Hermann & Gunther, 2003).It is a responsibility of the embedded system to ensure that all tasks meet their respective time constraints (Rajib, 2007).
Resource constraints: Embedded system is used to accomplish a specific task and embedded in specific objects (Peter, 2011).Therefore, embedded systems is constrained for their size, power capacity (may be battery operated), limited memory capacity (especially RAM size), CPU speed and function capacity (Kai, David, & Li, 2009).Interactivity: Embedded system is closely linked with the physical world, or even part of the physical world.Its principal role is not the transformation of data, but rather the interaction with the physical world.It executes on machines that are not computers.They are cars, airplanes, telephones, audio equipment, robots, appliances, toys, security systems, pacemakers, heart monitors, weapons, television sets, printers, scanners, climate control systems, manufacturing systems, and so on.It's necessary to acquire some properties of the physical world.Consequently, the designer of that system should be the person who best understands that physical world (Edward, 2002).Only in this way, can really proceed from the view of the physical world, choose the appropriate method, and design a practical system.
From the interactive features of the embedded system, we must understand the physical world in the field, in order to propose a suitable method.With limited computing resources and storage resources, real-time embedded systems need a simple and efficient search algorithm.
Quantities of the physical world tend to have two significant characteristics: continuity and locality (Boi & Peter, 1992;Brain, 1984;Jan, 2005).
Physical quantities are measured values detected by sensors.Continuity refers to that physical quantities vary slowly.Therefore, compared with the last, the newly measured value changes only a little.In other words, if represented in a curve, the curve is derivable.Locality refers to that the possible measured values within a certain time are only in a small range.Figure 1 shows the 24-hours temperature curve of a city in north China.

Summary of Traditional Search Algorithms
Although the linear search algorithm is very simple, it has some interesting advantages: a) In linear search process, the location of the next element to be compared with is always known.And it is only to get partial block of the table in several comparisons.b) If the searching probabilities of the elements in the table are learned and elements are placed in the right locations in the table, search efficiency will significantly increase (Knuth, 1999).

Given a table of records
, the algorithm searches for a given K.In a general situation, key i K will occur with probability i p , where The time required to do a successful search is essentially proportional to the number of comparisons, C, the average value of which is ; we have already derived this.If we have the option of putting the records into the table in any desired order, this quantity N C is smallest when that is, when the most frequently used records appear near the beginning.Suppose, on the other hand, that Then ; the average number of comparison is less than two, for this distribution, if the records appear in the proper order within the table (Knuth, 1999).
So, if we find the probability distribution of sequential searching, there will be a much reduce of cost.
Binary search algorithm is a logarithmic algorithm for searching in ordered table.After at most N lg comparisons, we will have found the key or we will have established that it is not present (Knuth, 1999).
In practical use, binary search has some inherent disadvantages: a) In some platforms, tail recursion is not eliminated and the recursive version of binary search algorithm requires more stack space or even causes a memory overflow.b) Binary search can interact poorly with the memory hierarchy (i.e.caching), because of its random-access nature.For external searching, care must be taken of that each of the first several probes will lead to disk seeks.
Search in hash table is fast and the time complexity is ) 1 (  .But it's hard to achieve in microprocessors, because of its obvious shortcomings (Thomas, 2001): a) The cost of a good hash function can be significantly higher than the inner loop of the search algorithm for a sequential list or search tree.
b) The hash tables are not effective when the number of entries is very small.c) The cost of a single operation may be quite high.In particular, if the hash table uses dynamic resizing, an insertion or deletion operation may occasionally take time proportional to the number of entries.This is a serious drawback in real-time or interactive applications.d) Hash tables in general exhibit poor locality of reference-that is, the data to be accessed is distributed at random in memory.This can trigger microprocessor cache misses that cause long delays.

Cursor Search Algorithm
The pros and cons of an algorithm are closely related to its application environments.An algorithm may be theoretically good, but tends to ignore a lot of factors.The fact is that a match to the requirements of embedded systems is the best.
According to these, a cursor search algorithm is proposed.It is simple, efficient and suitable for the measurement of physical quantities.
For the first search, cursor search algorithm gets the element corresponding to the key value, and the cursor is positioned for the first time.For future searches, according to the continuity and locality of the measured value, as well as the characteristics of linear search method, the algorithm compares the searched value with the key value at cursor position.If the searched value is smaller than the key value of cursor position element, cursor left shifts, until it is not.Conversely, if the searched value is greater than the key value of cursor position element, the cursor right shifts, until it is not.After the process, if the cursor is not out of bounds, the algorithm will get the actual value corresponding to the measured value.Or if the cursor is out of bounds, mark that the next search is a first search and return "not found".

Suppose an array
, with probability k p that record k A will be sought.For the first search, the algorithm define that the cursor is i using binary search method or a linear search method.
Because of the static characteristic of the cursor variable, cursor is still i until the next search.Near the cursor, the searched value (assuming it's j A ) have a greater probability, according to the continuity and locality of the measured value.When j i  is increasing, j p is getting smaller, as Figure 2 shows.In other words, the probabilities of measured value meet Consider a simple case, supposing The average comparison times of the algorithm can be expressed as .4 Figure 3 shows the relationship of N C and i p when N is 100.N C is the average comparison times in Formula 9. i p is probability of measured value based on the last value.And N is the size of array.Here, based on temperature detection system, use cursor search algorithm to search in a table and get the actual temperature value (an approximate value is OK).Assume the table is array A, with each element is a data structure consist of a measured value and an actual temperature value.Array A is sorted by measured values.The variable "cursor" is must be declared as a static or a global variable.The parameter "key" is the measured value to be searched.Figure 4 shows the flowchart of the algorithm.(ii)Array A is t the table, with each element is an instance of the structure {MeasuredValue m; TemperatureValue t;}c; (iii)Variable maxLength is the size of A.

Cursor_Search(MeasuredValue key){ if isFirst = true then cursor := First_Search(key) isFirst := false return cursor end if key < A[cursor].m then do cursor := cursor -1 while key < A[cursor].m and cursor is not out of bound else if key > A[cursor].m then do cursor := cursor + 1 while key > A[cursor].m and cursor is not out of bound end if cursor is not out of bounds return cursor else isFirst := true return not found end }
The First_Search function is used to position the cursor for the first time, and it can be one of these: a. a linear searching from the middle of table b. a linear searching from the head of table c. a binary searching Compared with linear search algorithm, cursor search algorithm makes the searches to be catenated using a static cursor.Because of the features of the physical quantities, the probability of an element to be sought is maximum when it's near the cursor.The cost is significantly reduced by using a static cursor.
Compared with binary search algorithm which disregards the features, cursor search algorithm produces a better performance with a cost of ) 1 (  in theory.In addition, the random-access of binary search algorithm sometimes causes negative effects.
Compared with hash table search method, cursor search algorithm is also ) 1 (  .But the hash table search method is a random-accessed and has difficulties in application.First, a good hash function is complex to compute, and is too costly for the microprocessors in embedded systems.Second, hash table need to be dynamically maintained at run time, which also increase the response time of the programs.
Theoretically, in the field of sensor detection, cursor algorithm is superior to other traditional algorithms.It makes use of the probability characteristics and locality.It is a catenated search algorithm, which begins where the last one left off.Its time complexity is ) 1 (  .Furthermore, because of the locality of the algorithm, it has a good performance in memory hierarchy.

Simulation and Comparison
A simulation experiment is performed to prove its superiority.
In the simulation, a temperature detection process is constructed.Pt100 is a most common resistance temperature detector (RTD) used in industry, which have a nominal resistance of 100 ohms at 0 °C.Its resistance changes with temperature.So the temperature can be measured according to the change of its resistance.The resistance is measured by a microprocessor.And then, the resistance value needs to be converted into temperature value.Therefore, a temperature-resistance table is needed to seek the right temperature by the microprocessor.In simulation, only a part of them is extracted as the table, as shows in Table 1.A collection of resistance data is detected in a day of a city.And the number of it is 63, as Figure 5 shows.In simulation, these data is the input of the cursor search algorithm, linear search algorithm and binary search algorithm.Through simulation, temperature curve is generated by the three algorithms, as Figure 6 shows.Figure 7 shows the number of comparisons of each searching by linear search algorithm in the simulation.Figure 8 shows the cumulative number of comparisons.Because most of the data is gathered in the front of the table, its actual efficiency is better than theoretical.The total number of comparisons remains 724 and the numbers of each searching is not stable.

Figure 1 .
Figure 1.A 24-hours temperature curve of a city

Figure 2 .
Figure 2. Probabilities of measured value base on the last searching

Figure 3 .
Figure 3.The relationship of N C and i p

Figure 4 .
Figure 4.The flowchart of cursor search algorithm

Figure 5 .
Figure 5.A collection of resistance data detected by Pt100

Figure 6 .
Figure 6.Temperature curve achieved by algorithms

Figure 7 .
Figure 7.The curve of comparison times of each searching by linear search algorithm

Figure 9 .
Figure 9.The curve of comparison times of each searching by binary search algorithm

Figure 11 .
Figure 11.The curve of comparison times of each searching by cursor search algorithm

Table 1 .
The temperature-resistance table of Pt100