I improve the quixort using the median of three. First, I found the median in this way: I put the left, middle and right elements of the sequence into an array of three elements, sorted this array by inserts, and selected the middle element. But this is a very non-optimal algorithm. Now I am looking for a median using the conditions:
/*Процедура Partition, модифицированная методом медианы из трех*/ int MedianPartition(int *a, int p, int r){ int left, mid, right, mediana; left = p; mid = (p + r) / 2; right = r; if(left > mid){ if(left < right){ mediana = left; }else mediana = right; }else if(mid > right){ mediana = right; }else mediana = mid; std::swap(mediana, a[r]); return partition(a, p, r); } I am confused about the logic of the calculations of the median; it seems to me that it is even more complicated than the quicksort itself. What am I doing wrong? Here is an example of the incorrect operation of this algorithm. https://ideone.com/QuNjjd
Supplement . Wrote this function to calculate the median. https://ideone.com/WLqc5R It can be seen that it works in a strange way, and if you use this method of finding the median in the Partition procedure, the sorting will loop.
Addition 2 . As I just did not torment this function in the last 1.5 hours. Here is the last option, and it doesn't work either. https://ideone.com/7VuMdd And before that there was a terrible jumble of ternary operators.