Tổng hợp những đoạn code hay dùng cho giải thuật

Sắp xếp nhanh :

void QuickSort(long long A[], int Left, int Right)
{
int i = Left, j = Right;
int pivot = A[(Left + Right) / 2];
/* partition */
while (i <= j)
{
while (A[i] < pivot) i++; while (A[j] > pivot)
j–;
if (i <= j)
{
Swap(A[i],A[j]);
i++;
j–;
}
}
/* recursion */
if (Left < j)
QuickSort(A, Left, j);
if (i < Right)
QuickSort(A, i, Right);
}

/*đảo giá trị*/

void Swap(long long &a,long long &b)
{
int temp = a;
a = b;
b = temp;
}

/*UCLN*/

int USCLN(int a, int b)
{
if(a==0) return b
return USCLN(b%a,a);
}

Bình luận về bài viết này