I am getting a segmentation error even if after reducing the size to 10^5

#include
using namespace std;

void countsort(int *a, int n)
{
int *frequency= new int [1000000]{0};
for(int i=0 ; i<n ; i++)
{
frequency[a[i]]++;
}

for(int i=0 ; i<1000001 ; i++ )
{
while(frequency[i]>0)
{
a[i]=i;
frequency[i]–;
}
}
delete[] a;
}

int main() {
int n,a[1000000];
cin>>n;
for(int i=0 ; i<n ; i++)
{
cin>>a[i];
}
countsort(a,n);

for(int i=0 ; i<n ; i++)
{
cout<<a[i]<<" ";
}

return 0;

}

while appointing values to a[i] i think u need to increment i as well. Because in ur code ur just giving same value to same a[i] every time.