21 мая 2015 г.

Быстрая сортировка

template <typename T>
void quickSortR(T a[], int B, int E)
{
long i = B, j = E;
T temp, p;
p = a[(B+E)/2];
do
{
while ( a[i] < p ) i++;
while ( a[j] > p ) j--;
if (i <= j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
}while ( i<=j );
if(B<j)quickSortR(a, B, j);
if(i<E)quickSortR(a, i, E);
}

void main()
{
srand(time(NULL));
const long SIZE=10;
int ar[SIZE];
// до сортировки
for(int i=0;i<SIZE;i++){
ar[i]=rand()%100;
cout<<ar[i]<<"\t";
}
cout<<"\n\n";
quickSortR(ar,0,SIZE-1);
// после сортировки
for(int i=0;i<SIZE;i++){
cout<<ar[i]<<"\t";
}
cout<<"\n\n";
}

Комментариев нет:

Отправить комментарий