About using the module operator

this is part of the code for the quicksort algorithm, but really I don’t know why it uses rand() %n, please help me thanks

Swap(V,0,rand() %n)  // move pivot elem to V[0]
+3
source share
2 answers

Used to randomize Quick Sort to achieve nlgn average time complexity.

To quote from Wikipedia:

What makes random anchor points a good choice?

, , . ; 25% , 25% . , 2log2n 1, .

+2

O (nlog (n)), - n ^ 2 ( ). , O (nlog (n)), , rand()% n 0 n-1.

0

Source: https://habr.com/ru/post/1748965/


All Articles