Реализация быстрой сортировки

На Паскале

procedure Sort(L, R : Longint); var i, j, x, t : longint; begin i := L; j := R; x := a[(i + j)div 2]; repeat while a[i] < x do inc(i); while a[j] > x do dec(j); if i <= j then begin t := a[i]; a[i] := a[j]; a[j] := t; inc(i); dec(j); end; until i > j; if i < R then Sort(i, R); if j > L then Sort(L, j); end;

На C++

void Sort(int L, R) { int i, j, x, t; i = L; j = R; x = a[(i + j)/ 2]; while ( i <= j) { while (a[i] < x) i++; while (a[j] > x) j--; if (i <= j) { swap(a[i], a[j]); i++; j--; } } if (i < R) Sort(i, R); if (j > L) Sort(L, j); }
Последнее изменение: Суббота, 15 Август 2020, 02:34