Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are implementing a <strong>set</strong> of different implementation types</p> <p><strong>map</strong> contain a key and value pair - you only have a key - so it is a <strong>set</strong> </p> <p><strong>hash</strong> means a different thing - they are unordered and use a hash function to select a bucket for a list of all items with the same hash, called collisions (some variants use other schemes to deal with collisions). This is implented in the STL as <code>std::unordered_set</code>, <code>std::unsordered_map</code>, ...</p> <p>For an array implementation that is sorted use an array that is kept in sorted array, and then do an insert that has to shift all items greater to maintain the sort order. Your lookup will then do a binary search</p> <p>The insert will use binary search to find where to place the item, and then shift all items greater than it.</p> <p>The binary search, starts at the middle and then repetively checks (low+mid)/2 if lower or (mid+highest)/2 if greater until the location is found</p> <p>Note you will have to distinguish between found and not found</p> <pre><code>int binsearch(int * array, int len, int value, bool * found) { // return lowest position greater or equal to value int lowp=0; int highp=len-1; int midp=(lowp+highp)/2; while (lowp &lt;=highp) { int mid=array[midp]; if (mid &gt; value) { highp=midp-1; } else if (value==mid) { *found=true; return midp; // found at this position } else { lowp=midp+1; } midp=(lowp+highp)/2; } *found=false; if (array[midp] &gt; value) { return midp; // only at position 0 for a head insert } return midp+1; // not found - position to start shift } int main(int, char**) { int array[]={2,4,6,8,10,12,14,16,18,20}; bool found; for (int search=1; search &lt; 22; ++search) { int a=binsearch(array, 10, search, &amp;found); std::cout &lt;&lt; search &lt;&lt; " at " &lt;&lt; a &lt;&lt; ":" &lt;&lt; found &lt;&lt; std::endl; } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload