Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>O(nlogn)</code> is "faster" then <code>O(n^2)</code>, but let's recall what the big O notation mean.</p> <p>It means that if algorithm A has complexity of <code>O(nlogn)</code>, for some constants <code>N_1</code>, and <code>c1</code>, for each <code>n&gt;N</code> - the algorithm is "faster" then <code>c1*n*log(n)</code>. If algorithm B has <code>O(n^2)</code>, there are some constants <code>N_2</code>,<code>c2</code> such that the algorithm is "faster" then <code>c2 * n^2 * log(n)</code> for <code>n &gt; N_2</code>.</p> <p>However - what happens before this <code>N</code>? and what is this constant <code>C</code>? We do not know. <strong>Algorithm <code>B</code> could still be "faster" then algorithm <code>A</code> for small inputs</strong>, but for large inputs - indeed, <code>A</code> will be faster (the asymptotic bound is better).</p> <p>For example, let's take the case where algorithm <code>A</code> runs in <code>T_1(n) = 10* nlog(n)</code> ops, while algorithm <code>B</code> runs in <code>T_2(n) = n^2</code>. for <code>n=3</code> - we get that <code>T_1(3) = 10*3*2 = 60</code> (we tool the ceil for <code>log(n)</code>), and <code>T_2(3) = 9</code> - thus algorithm <code>B</code>, though <code>O(n^2)</code> is faster then <code>A</code> for this input.</p> <p><strong>Regarding quick sort and insertion sort:</strong> <br>Quick sort is usually extremely fast, and decays into quadric time in very rare cases (the probability of this happening is slim if we chose a random element as a pivot).</p> <p>However, the constant behind the big O notation in quicksort is greater then insertion sort. Thus - <strong>a possible optimization is: use quicksort until you get to a certain threshold</strong> (say 30 elements), and then sort this subarray with insertion sort rather then quicksort. <a href="https://stackoverflow.com/questions/12622015/why-should-insertion-sort-be-used-after-threshold-crossover-in-merge-sort">This post</a> discusses this optimization.</p> <p>Bubble sort is (empirically) terrible for random arrays, but could be good if the array is almost sorted and the "out of place" elements are in its beginning.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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