How fast can you make linear search?

前端 未结 20 1854
死守一世寂寞
死守一世寂寞 2020-12-23 21:46

I\'m looking to optimize this linear search:

static int
linear (const int *arr, int n, int key)
{
        int i = 0;
        while (i < n) {
                      


        
相关标签:
20条回答
  • 2020-12-23 22:25

    loop backwards, this might be translated...

    // loop backward
    
    for (int i = arraySize - 1; i >=0; --i)
    

    ...to this( "could be" faster ):

        mov cx, arraySize - 1
    detectionHere:
        ...
        loop detectionHere   
    

    Other than that, only binary search can make searching faster

    0 讨论(0)
  • 2020-12-23 22:26

    Well, you could use pointers...

    static int linear(const int *array, int arraySize, int key) {
        int i;
    
        for(i = 0; i < arraySize; ++i) {
            if(*array >= key) {
                return i;
            }
    
            ++array;
        }
    
        return arraySize;
    }
    
    0 讨论(0)
提交回复
热议问题