Lỗi exited with code -1073741571 khi chạy code quicksort

#include <iostream>
using namespace std;
#define MAX 100
void  Input(int a[], int& n)
{
	cout << " moi ban nhap so luong phan tu :";
	cin >> n;
	cout << " moi ban nhap mang :" << endl;
	for (int i = 0; i < n; i++)
	{
		cout << "[a" << i << "]:";
		cin >> a[i];
	}
}
void Output(int a[], int n)
{
	cout << "Mang hien tai la :" << endl;
	for (int i = 0; i < n; i++)
	{
		cout << a[i] << "\t";
	}
}
void QuickSort(int a[], int l,int r)
{
	int x = a[(r - l) / 2];
	int i = l;
	int j = r;
	while (i <= j)
	{
		while (a[i] < x)
		{
			i++;
		}
		while (a[j] > x)
		{
			j--;
		}
		if (i <= j)
		{
			swap(a[i], a[j]);
			i++;
			j--;
		}
		if (l < j)
			QuickSort(a, l, j);
		if (i < r)
			QuickSort(a, i, r);
	}
}
int main()
{
	int a[MAX];
	int n;
	Input(a, n);
	QuickSort(a,0,n-1);
	cout << " Mang Sau Khi sap xep la :";
	Output(a, n);
	system("pause");
	system("cls");
}

em code bài này ôn thi xong nó hiện lỗi (process 15016) exited with code -1073741571.
em xài visual 2019 ạ. a biết giúp e cách fix với . E cám ơn

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?