When to Use Which Data Structure And Algorithm........

Revision en2, by puneet.tiwari9039, 2020-04-25 12:03:28

We often get confused which data structure or Algorithm we should use while in competition and sometimes if we doesn't use good Data Structure while in competition, it may lead to not able to solve a simple question easily..

So here is my blog which will help you to Use agood Data Structure during a contest...

  1. Set..

  2. We use Set when we want to deal with only unique elements..

  3. If we try to Insert a value already present in a Set it will automatically discard it..

  4. Insertion, Deletion, Searching in Set take place in log(n) time...

  5. In Set, Elements are arranged in Sorted Order....

5.If You only want Unique element in Set then you can use Unordered_Set in place of Set...Just replace set with unordered_set....

:- To insert a element in Set...

: #include<bits/stdc++.h> using namespace std;

    int main()
    {
       set<int>S;
       S.insert(5);
    }

<------------------------------------------------------------------------------------>

:- To delete a element in Set..


    #include<bits/stdc++.h>
    using namespace std;

    int main()
    {
       set<int>S;
       S.erase(5);
    }

<------------------------------------------------------------------------------------>

:- To Search an element in array..


    if(S.find(Element_To _Searhc)!=S.end())
    {
       //Element Found
    }
    else
    {
       //Element Not Found..
    }

<------------------------------------------------------------------------------------> :-To iterate in Set...

#include<bits/stdc++.h>
    using namespace std;

    int main()
    {
       set<int>S;
       for (auto x:S)
         cout<<x<<" ";
    }

<################################################################################################################>

2.Priority Queue =

1. We use priority_queue when we want largest or smallest element  from the given set of  elements in 
       constant time...

2.It uses Concept of Min-Heap and Max-Heap...

3.Insertion and Deletion take place in logn time..

4.When you want to get largest or smallest element frequently you should use priority_queue()..


:- To insert element in priority_queue()

    int main()
    {
       priority_queue<int>p;

       for(int x:{2,45,12,3,1})
         p.push(x);


    }

<------------------------------------------------------------------------------------>

:- To delete element in priority_queue()

    p.pop();

<------------------------------------------------------------------------------------>

:-To traverse in priority_queue()

    int main()
    {
       priority_queue<int>p;

       for(int x:{2,45,12,3,1})
         p.push(x);


       while(!p.empty())
       {
         cout<<p.top()<<" ";
         p.pop();
       }


    }

<################################################################################################################>

  1. Map
1. We use Map when we want to form a key value pair..

2. It is same as Dictionary in python...



    int main()
    {

       map<int, int>m;

       m.insert({2,4});       //First Way

       m[3] = 45;                              // Second Way
    }

<#####################################################################################>

  1. Binary Search.
You should always use Binary search when you want to perform Searching operation on an array or vector..

Note : You can only use Binary search only when You have Elements in sorted order...



bool Binary_Search(int arr[], int n, int key)
{
    int low = 0;
    int high = n;

    while(low<=high)
    {
       int mid = (low+high)/2;

       if(arr[mid]==key)
         return true;

       else if(arr[mid] > key)
         high = mid-1;
       else
         low = mid+1;
    }
    return false;
}

int main()
{
    int arr[] = {1,23,45,67,68,80};

    int n = sizeof(arr)/sizeof(arr[0]);

    if(Binary_Search(arr,n, 1))
    {
       cout<<"Element Present\n";
    }
    else
       cout<<"Element Not Present\n";
}

<################################################################################################################>

Thank You for Reading Post......

Tags #data structure, #algorithms, #binary search, priority queue, map vs unordered_map, map

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English puneet.tiwari9039 2020-04-25 12:03:28 153
en1 English puneet.tiwari9039 2020-04-25 11:59:19 4424 Initial revision (published)