Fastest way to search for an element in unsorted array

后端 未结 10 1768
北恋
北恋 2020-12-14 08:13

I just bumped on to this question today and was trying for a solution that is better than O(N) but could not come up with one.

Searched through SO but couldn\'t find

相关标签:
10条回答
  • 2020-12-14 08:52

    If it's not sorted, you have to inspect each element.

    0 讨论(0)
  • 2020-12-14 08:52

    You can search an element with O(1) using this approach.

    Just create a MAP . When you insert a value just then for that key assign value to '1', and to search it again just check if that array is present or not .

    Below is the code:-

    #include<bits/stdc++.h>
    
    using namespace std;
    
    int main(){
        int n;
        cin>>n;
        map<int,int> map;
        for(int i=0;i<n;i++){
            int k;
            cin>>k;
            map[k]=1;
        }
        int num;
        cin>>num;
    
        if(map[num]){
            cout<<"FOUND"<<endl;
        }else{
            cout<<"NOT FOUND"<<endl;
        }
    
        return 0;
    }
    
    
    
    Input: 
    5    // *no. of elements*
    6 4 7 3 2  //*elements* 
    3    // *number to find*
    

    Output :FOUND

    0 讨论(0)
  • 2020-12-14 08:58

    If you are not doing parallel search, then you can insert key to the end of array as sentinel value and do search with only 'n' comparisons instead of 2n comparisons.

    For more details, refer the following question: What's the point of using linear search with sentinel?

    0 讨论(0)
  • 2020-12-14 09:02

    Yet,there is another logic...

    (Even numbers are stored in even address)

    • First check whether the search element is odd or even

    • If the search element is"even",then perform search only for even adress(Create loop increment to skip odd address)

    • Half the element can be skipped from searching by this logic

    For example:

    • If there are 100 element stored in unordered way and search element is 98.... since the search number is even...u can skip all odd address(so 50 elements are skipped) Now the search is done only for rest 50 even address....

    You can divide the element and search in parallel or Use "pivot key" to sort to rest 50 elements or any other search method

    0 讨论(0)
  • 2020-12-14 09:03

    What will be the efficiency of algorithm that makes use of partition approach applied during quick-sort as follows?

    1. Randomly select some value (let us call it v) in the list.

    2. Partition the entire list into 2 parts. Left part contains all elements that are less than v. Right part contains all elements that are greater than v.

    3. Repeat the steps 2, 3 until you determine whether the element exists or does not exist.

    I am not sure about the complexity of above algorithm, but looks like it will be definitely less than the complexity of quick-sort algorithm: (n log n).

    0 讨论(0)
  • 2020-12-14 09:06

    You're right, the fastest way is to simply iterate through the array and look for it. Without further information, there is nothing better you can do.

    Unless you have a quantum computer, that is.

    0 讨论(0)
提交回复
热议问题