0

in this formula and continue this question I think if we assume n equal 3 so we have array like A={4,5,7}

and with the formula, we get 8 from n=3 and this means is 8 average compare for array with length 3 and this so weird!

  1. what is exactly happening in quickSort to get average compare 8 form array so short! and occurs many compare and that is so bad! is true?

I think if we compare the array with 4 step is so faster than use quickSort!

Michael
  • 51
  • 1
  • 8
  • Please write your question in a much more clear way. Also, please don't use the title of the post as part of the description. – JohanC Nov 03 '19 at 22:45
  • 1
    Also try to make your question understandable on its own. Referring to other sources is a problem when your question is totally incomprehensible without them. – JohanC Nov 03 '19 at 22:47
  • what do you meaning more clear?! ... I ask why the result is very big number than length array and this is so weird! the number is so big than the name of the algorithm(QuickSort)! now the formula is average compare or not?! – Michael Nov 03 '19 at 22:52
  • and I'm sorry because English is not my native lan! – Michael Nov 03 '19 at 22:57
  • Your question seems to be about quicksort and arrays of length 3. Take into account that thinking about expected values are only useful for large arrays. You want to compare running times between algorithms for really large arrays (e.g. 1 million elements) – JohanC Nov 03 '19 at 23:04
  • yes about quicksort :))))))) ... from the title and the content is really really clear this is about quicksort and array of length 3!!!! but thank you very very much ... so expected values aren't appropriate for an array of small! that is true? – Michael Nov 03 '19 at 23:12
  • if you can explain why expected values aren't appropriate for small array(len) is so awesome ! you can send as an answer and I accept to answer the question! – Michael Nov 03 '19 at 23:15
  • @James Reinstate Monica Polk : what do you meaning? you wanna say the result(formula) has nothing about compare!! – Michael Nov 04 '19 at 10:03

1 Answers1

1

So your question seems to be about the formula

E[X] = E[sum(i, 1, n-1, sum(j, i+1, n, pi,j))]

in this image you uploaded in the other question.

Here E[X] means the expected value. In simpler terms: the value X will get on average if you do the experiment (sorting a random array) many many times.

pi,j is the chance that item i and item j are compared to each other during the run of the algorithm. For a good algorithm, it happens that item i is compared to item k and item k to item j, which often makes it unnecessary to really compare item i and item j. Therefore, the better the algorithm, the lower this probability pi,j.

In the worst case, for example when n is only 3, you can have pi,j = 1. If you then calculate the formula, you get that for n=3, E[X]=3 because there are only three combinations for (i,j) : (1,2), (1,3) and (2,3). This means that for n=3 always 3 comparisons are done. This is equal for all good algorithms, because you can not take advantage of item_i < item_k < item_j.

For larger n, there are many chances to take advantage of not needing to explicitly test all item_i versus all item_j. A detailed analysis is for example available in the Khan Academy article.

JohanC
  • 71,591
  • 8
  • 33
  • 66